Components and supplies
Pushbutton Switch, Momentary
Wemos D1 Mini
WeMos D1 battery module
Resistor 10k ohm
Li-Ion Battery 1000mAh
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Thingspeak code
arduino
Use this sketch for Thingspeak
1/* 2 Data grab and deep sleep with button 3 4 Created on: 10/10/2019 5 6*/ 7#include <NeoPixelBus.h> 8#include <Arduino.h> 9#include <ESP8266WiFi.h> 10#include <ESP8266WiFiMulti.h> 11#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal 12#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal 13#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic 14#include <ESP8266HTTPClient.h> 15#include <Ticker.h> 16Ticker ticker; 17const uint16_t PixelCount = 2; // 18const uint8_t PixelPin = 3; // 19#define colorSaturation 128 20String mostSignificantDigit; 21// Declare our NeoPixel strip object: 22NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount); 23//Declare he colours 24RgbColor red(colorSaturation, 0, 0); 25RgbColor green(0, colorSaturation, 0); 26RgbColor blue(0, 0, colorSaturation); 27RgbColor white(colorSaturation); 28RgbColor black(0); 29RgbColor purple(246, 22, 246); 30RgbColor orange(255, 137, 0); 31RgbColor yellow(255, 239, 0); 32RgbColor darkpurple(137,36,142); 33RgbColor darkmag(158,6,2); 34 35ESP8266WiFiMulti WiFiMulti; 36int number_airindex ; 37int numLedsToLight; 38 39 40 41void tick() 42{ 43 int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin 44 digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state 45} 46 47//sleep time 48const int sleepSeconds_show = 1000 * 10; // 10 seconds 49 50 51void setup() { 52 Serial.begin(115200); 53 strip.Begin(); 54 // strip.setBrightness(40); 55 strip.Show(); // Initialize all pixels to 'off' 56 (red ); strip.Show(); 57 pinMode(BUILTIN_LED, OUTPUT); 58 ticker.attach(0.5, tick); 59 WiFiManager wifiManager; 60 wifiManager.autoConnect(); 61 // Serial.setDebugOutput(true); 62 Serial.setTimeout(2000); 63 while (!Serial) { } 64 Serial.println(); 65 Serial.println(); 66 Serial.println(); 67 68 for (uint8_t t = 4; t > 0; t--) { 69 70 Serial.printf("[SETUP] WAIT %d...", t); 71 Serial.flush(); 72 strip.ClearTo(red ); strip.Show(); 73 delay(500); 74 strip.ClearTo(black ); strip.Show(); 75 (red ); strip.Show(); 76 77 } 78 // WiFiMulti.addAP("Learning_Lounge", "Z@q12wsx"); 79 //WiFiMulti.addAP("simon", "binh12345"); 80 // wait for WiFi connection 81 strip.ClearTo(red); strip.Show(); 82 delay(1000); 83 if ((WiFiMulti.run() == WL_CONNECTED)) { 84 strip.ClearTo(white ); strip.Show(); 85 delay(1000); 86 HTTPClient http; 87 88 Serial.print("[HTTP] begin..."); 89 // configure traged server and url 90 // get the AQI value 91 //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey 92 http.begin("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey"); //HTTP 93 Serial.println("[HTTP] begin..."); 94 Serial.println("[HTTP] GET..."); 95 // start connection and send HTTP header 96 //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey 97 Serial.print("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey"); 98 int httpCode = http.GET(); 99 100 // httpCode will be negative on error 101 if (httpCode > 0) { 102 // HTTP header has been send and Server response header has been handled 103 Serial.printf("[HTTP] GET... code: %d", httpCode); 104 strip.ClearTo(purple); strip.Show(); 105 // file found at server 106 if (httpCode == HTTP_CODE_OK) { 107 String payload = http.getString(); 108 // Print the string value from Air visual 109 Serial.println("Payload "); 110 Serial.println(payload); 111 // Convert string to integer so that we can utilise the number 112 number_airindex = payload.toInt(); 113 Serial.println(number_airindex); 114 Serial.println(); 115 116 //Light the LEDs 117 if (number_airindex >= 300) { 118 // do Thing A 119 strip.ClearTo(darkmag); strip.Show(); 120 } 121 else if (number_airindex >= 200) { 122 // do Thing B 123 strip.ClearTo(darkpurple); strip.Show(); 124 } 125 else if (number_airindex >= 150) { 126 // do Thing B 127 strip.ClearTo(red); strip.Show(); 128 } 129 else if (number_airindex >= 100) { 130 // do Thing B 131 strip.ClearTo(orange); strip.Show(); 132 } 133 else if (number_airindex >= 50) { 134 // do Thing B 135 strip.ClearTo(yellow); strip.Show(); 136 } 137 else { 138 // do Thing C 139 strip.ClearTo(green); strip.Show(); 140 } 141 } 142 143 } else { 144 Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpCode).c_str()); 145 strip.ClearTo(purple); strip.Show(); 146 } 147 148 http.end(); 149 150 151 152 Serial.println("Here"); 153 delay(sleepSeconds_show); 154 strip.ClearTo(black); strip.Show(); 155 number_airindex = 0; 156 Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal"); 157 //WiFi.disconnect(true); 158 ESP.deepSleep(0); 159 } 160} 161void loop() { 162}
Air Visual sketch
arduino
Use this if you are using the Air Visual free API
1/* 2 Data grab and deep sleep with button using Air Visual API 3 4 Created on: 10/10/2019 5 6*/ 7#include <NeoPixelBrightnessBus.h> 8#include <Arduino.h> 9#include <ESP8266WiFi.h> 10#include <ESP8266WiFiMulti.h> 11#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal 12#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal 13#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic 14#include <ESP8266HTTPClient.h> 15#include <Ticker.h> 16#include <jsonlib.h> 17Ticker ticker; 18const uint16_t PixelCount = 2; // 19const uint8_t PixelPin = 3; // 20#define colorSaturation 128 21String mostSignificantDigit; 22// Declare our NeoPixel strip object: 23NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount); 24//Declare the colours 25RgbColor red(colorSaturation, 0, 0); 26RgbColor green(0, colorSaturation, 0); 27RgbColor blue(0, 0, colorSaturation); 28RgbColor white(colorSaturation); 29RgbColor black(0); 30RgbColor purple(246, 22, 246); 31RgbColor orange(255, 137, 0); 32RgbColor yellow(255, 239, 0); 33RgbColor darkpurple(137, 36, 142); 34RgbColor darkmag(158, 6, 2); 35 36 37ESP8266WiFiMulti WiFiMulti; 38int number_airindex ; 39int numLedsToLight; 40//sleep time 41const int sleepSeconds_show = 1000 * 10; // 10 seconds 42void tick() 43{ 44 int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin 45 digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state 46} 47void setup() { 48 Serial.begin(115200); 49 strip.Begin(); 50 strip.SetBrightness(120); 51 52 strip.Show(); // Initialize all pixels to 'off' 53 strip.ClearTo(white ); strip.Show(); 54 pinMode(BUILTIN_LED, OUTPUT); 55 ticker.attach(0.5, tick); 56 //WiFiManager wifiManager; 57 //wifiManager.autoConnect(); 58 // Serial.setDebugOutput(true); 59 Serial.setTimeout(2000); 60 while (!Serial) { } 61 Serial.println(); 62 Serial.println(); 63 Serial.println(); 64 65 for (uint8_t t = 4; t > 0; t--) { 66 Serial.printf("[SETUP] WAIT %d...", t); 67 Serial.flush(); 68 69 delay(1000); 70 } 71 72 //WiFiMulti.addAP("SSID", "password"); 73 // wait for WiFi connection 74 if ((WiFiMulti.run() == WL_CONNECTED)) { 75 76 HTTPClient http; 77 78 Serial.print("[HTTP] begin..."); 79 // configure traged server and url 80 // get the entry_id from AirVisual 81 http://api.airvisual.com/v2/nearest_city?key={Your secret key}// 82 http.begin("http://api.airvisual.com/v2/nearest_city?key={Your secret key}"); //HTTP 83 Serial.print("[HTTP] begin..."); 84 Serial.println("[HTTP] GET..."); 85 // start connection and send HTTP header 86 Serial.print("http://api.airvisual.com/v2/nearest_city?key={Your secret key}"); 87 int httpCode = http.GET(); 88 89 // httpCode will be negative on error 90 if (httpCode > 0) { 91 // HTTP header has been send and Server response header has been handled 92 Serial.printf("[HTTP] GET... code: %d", httpCode); 93 94 // file found at server 95 if (httpCode == HTTP_CODE_OK) { 96 String payload = http.getString(); 97 String pollution_list = jsonExtract(payload, "data"); 98 String air_pollution_list = jsonExtract(payload, "pollution"); 99 int number_airindex = jsonExtract(air_pollution_list, "aqius").toFloat(); 100 101 102 Serial.print("AQI index is.."); 103 Serial.println(number_airindex); //air pollution idex 104 Serial.println(); 105 //Light the LEDs 106 Serial.println("Switch on LED"); 107 //Light the LEDs 108 if (number_airindex >= 300) { 109 // do Thing A 110 strip.ClearTo(darkmag); strip.Show(); 111 } 112 else if (number_airindex >= 200) { 113 // do Thing B 114 strip.ClearTo(darkpurple); strip.Show(); 115 } 116 else if (number_airindex >= 150) { 117 // do Thing B 118 strip.ClearTo(red); strip.Show(); 119 } 120 else if (number_airindex >= 100) { 121 // do Thing B 122 strip.ClearTo(orange); strip.Show(); 123 } 124 else if (number_airindex >= 50) { 125 // do Thing B 126 strip.ClearTo(yellow); strip.Show(); 127 } 128 else { 129 // do Thing C 130 strip.ClearTo(green); strip.Show(); 131 } 132 } 133 134 } else { 135 Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpCode).c_str()); 136 strip.ClearTo(purple); strip.Show(); 137 } 138 http.end(); 139 } 140 141 Serial.println("Here"); 142 delay(sleepSeconds_show); 143 //Turn the LEDs to black 144 strip.ClearTo(black ); strip.Show(); 145 Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal"); 146 ESP.deepSleep(0); 147 148} 149 150void loop() { 151}
Thingspeak code
arduino
Use this sketch for Thingspeak
1/* 2 Data grab and deep sleep with button 3 4 Created on: 10/10/2019 5 6*/ 7#include 8 <NeoPixelBus.h> 9#include <Arduino.h> 10#include <ESP8266WiFi.h> 11#include 12 <ESP8266WiFiMulti.h> 13#include <DNSServer.h> //Local DNS Server used 14 for redirecting all requests to the configuration portal 15#include <ESP8266WebServer.h> 16 //Local WebServer used to serve the configuration portal 17#include <WiFiManager.h> 18 //https://github.com/tzapu/WiFiManager WiFi Configuration Magic 19#include 20 <ESP8266HTTPClient.h> 21#include <Ticker.h> 22Ticker ticker; 23const uint16_t 24 PixelCount = 2; // 25const uint8_t PixelPin = 3; // 26#define colorSaturation 27 128 28String mostSignificantDigit; 29// Declare our NeoPixel strip object: 30NeoPixelBus<NeoGrbFeature, 31 NeoEsp8266Dma800KbpsMethod> strip(PixelCount); 32//Declare he colours 33RgbColor 34 red(colorSaturation, 0, 0); 35RgbColor green(0, colorSaturation, 0); 36RgbColor 37 blue(0, 0, colorSaturation); 38RgbColor white(colorSaturation); 39RgbColor black(0); 40RgbColor 41 purple(246, 22, 246); 42RgbColor orange(255, 137, 0); 43RgbColor yellow(255, 239, 44 0); 45RgbColor darkpurple(137,36,142); 46RgbColor darkmag(158,6,2); 47 48ESP8266WiFiMulti 49 WiFiMulti; 50int number_airindex ; 51int numLedsToLight; 52 53 54 55void tick() 56{ 57 58 int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin 59 60 digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state 61} 62 63//sleep 64 time 65const int sleepSeconds_show = 1000 * 10; // 10 seconds 66 67 68void setup() 69 { 70 Serial.begin(115200); 71 strip.Begin(); 72 // strip.setBrightness(40); 73 74 strip.Show(); // Initialize all pixels to 'off' 75 (red ); strip.Show(); 76 77 pinMode(BUILTIN_LED, OUTPUT); 78 ticker.attach(0.5, tick); 79 WiFiManager 80 wifiManager; 81 wifiManager.autoConnect(); 82 // Serial.setDebugOutput(true); 83 84 Serial.setTimeout(2000); 85 while (!Serial) { } 86 Serial.println(); 87 88 Serial.println(); 89 Serial.println(); 90 91 for (uint8_t t = 4; t > 0; t--) 92 { 93 94 Serial.printf("[SETUP] WAIT %d...", t); 95 Serial.flush(); 96 97 strip.ClearTo(red ); strip.Show(); 98 delay(500); 99 strip.ClearTo(black 100 ); strip.Show(); 101 (red ); strip.Show(); 102 103 } 104 // WiFiMulti.addAP("Learning_Lounge", 105 "Z@q12wsx"); 106 //WiFiMulti.addAP("simon", "binh12345"); 107 // wait for 108 WiFi connection 109 strip.ClearTo(red); strip.Show(); 110 delay(1000); 111 if 112 ((WiFiMulti.run() == WL_CONNECTED)) { 113 strip.ClearTo(white ); strip.Show(); 114 115 delay(1000); 116 HTTPClient http; 117 118 Serial.print("[HTTP] begin..."); 119 120 // configure traged server and url 121 // get the AQI value 122 //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey 123 124 http.begin("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey"); 125 //HTTP 126 Serial.println("[HTTP] begin..."); 127 Serial.println("[HTTP] 128 GET..."); 129 // start connection and send HTTP header 130 //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey 131 132 Serial.print("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey"); 133 134 int httpCode = http.GET(); 135 136 // httpCode will be negative on error 137 138 if (httpCode > 0) { 139 // HTTP header has been send and Server response 140 header has been handled 141 Serial.printf("[HTTP] GET... code: %d", httpCode); 142 143 strip.ClearTo(purple); strip.Show(); 144 // file found at server 145 146 if (httpCode == HTTP_CODE_OK) { 147 String payload = http.getString(); 148 149 // Print the string value from Air visual 150 Serial.println("Payload 151 "); 152 Serial.println(payload); 153 // Convert string to integer 154 so that we can utilise the number 155 number_airindex = payload.toInt(); 156 157 Serial.println(number_airindex); 158 Serial.println(); 159 160 //Light 161 the LEDs 162 if (number_airindex >= 300) { 163 // do Thing A 164 165 strip.ClearTo(darkmag); strip.Show(); 166 } 167 else if 168 (number_airindex >= 200) { 169 // do Thing B 170 strip.ClearTo(darkpurple); 171 strip.Show(); 172 } 173 else if (number_airindex >= 150) { 174 // 175 do Thing B 176 strip.ClearTo(red); strip.Show(); 177 } 178 else 179 if (number_airindex >= 100) { 180 // do Thing B 181 strip.ClearTo(orange); 182 strip.Show(); 183 } 184 else if (number_airindex >= 50) { 185 // 186 do Thing B 187 strip.ClearTo(yellow); strip.Show(); 188 } 189 else 190 { 191 // do Thing C 192 strip.ClearTo(green); strip.Show(); 193 194 } 195 } 196 197 } else { 198 Serial.printf("[HTTP] GET... 199 failed, error: %s", http.errorToString(httpCode).c_str()); 200 strip.ClearTo(purple); 201 strip.Show(); 202 } 203 204 http.end(); 205 206 207 208 Serial.println("Here"); 209 210 delay(sleepSeconds_show); 211 strip.ClearTo(black); strip.Show(); 212 number_airindex 213 = 0; 214 Serial.println("I'm awake, but I'm going into deep sleep mode until 215 RESET pin is connected to a LOW signal"); 216 //WiFi.disconnect(true); 217 ESP.deepSleep(0); 218 219 } 220} 221void loop() { 222}
Air Visual sketch
arduino
Use this if you are using the Air Visual free API
1/* 2 Data grab and deep sleep with button using Air Visual API 3 4 Created on: 10/10/2019 5 6*/ 7#include <NeoPixelBrightnessBus.h> 8#include <Arduino.h> 9#include <ESP8266WiFi.h> 10#include <ESP8266WiFiMulti.h> 11#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal 12#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal 13#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic 14#include <ESP8266HTTPClient.h> 15#include <Ticker.h> 16#include <jsonlib.h> 17Ticker ticker; 18const uint16_t PixelCount = 2; // 19const uint8_t PixelPin = 3; // 20#define colorSaturation 128 21String mostSignificantDigit; 22// Declare our NeoPixel strip object: 23NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount); 24//Declare the colours 25RgbColor red(colorSaturation, 0, 0); 26RgbColor green(0, colorSaturation, 0); 27RgbColor blue(0, 0, colorSaturation); 28RgbColor white(colorSaturation); 29RgbColor black(0); 30RgbColor purple(246, 22, 246); 31RgbColor orange(255, 137, 0); 32RgbColor yellow(255, 239, 0); 33RgbColor darkpurple(137, 36, 142); 34RgbColor darkmag(158, 6, 2); 35 36 37ESP8266WiFiMulti WiFiMulti; 38int number_airindex ; 39int numLedsToLight; 40//sleep time 41const int sleepSeconds_show = 1000 * 10; // 10 seconds 42void tick() 43{ 44 int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin 45 digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state 46} 47void setup() { 48 Serial.begin(115200); 49 strip.Begin(); 50 strip.SetBrightness(120); 51 52 strip.Show(); // Initialize all pixels to 'off' 53 strip.ClearTo(white ); strip.Show(); 54 pinMode(BUILTIN_LED, OUTPUT); 55 ticker.attach(0.5, tick); 56 //WiFiManager wifiManager; 57 //wifiManager.autoConnect(); 58 // Serial.setDebugOutput(true); 59 Serial.setTimeout(2000); 60 while (!Serial) { } 61 Serial.println(); 62 Serial.println(); 63 Serial.println(); 64 65 for (uint8_t t = 4; t > 0; t--) { 66 Serial.printf("[SETUP] WAIT %d...", t); 67 Serial.flush(); 68 69 delay(1000); 70 } 71 72 //WiFiMulti.addAP("SSID", "password"); 73 // wait for WiFi connection 74 if ((WiFiMulti.run() == WL_CONNECTED)) { 75 76 HTTPClient http; 77 78 Serial.print("[HTTP] begin..."); 79 // configure traged server and url 80 // get the entry_id from AirVisual 81 http://api.airvisual.com/v2/nearest_city?key={Your secret key}// 82 http.begin("http://api.airvisual.com/v2/nearest_city?key={Your secret key}"); //HTTP 83 Serial.print("[HTTP] begin..."); 84 Serial.println("[HTTP] GET..."); 85 // start connection and send HTTP header 86 Serial.print("http://api.airvisual.com/v2/nearest_city?key={Your secret key}"); 87 int httpCode = http.GET(); 88 89 // httpCode will be negative on error 90 if (httpCode > 0) { 91 // HTTP header has been send and Server response header has been handled 92 Serial.printf("[HTTP] GET... code: %d", httpCode); 93 94 // file found at server 95 if (httpCode == HTTP_CODE_OK) { 96 String payload = http.getString(); 97 String pollution_list = jsonExtract(payload, "data"); 98 String air_pollution_list = jsonExtract(payload, "pollution"); 99 int number_airindex = jsonExtract(air_pollution_list, "aqius").toFloat(); 100 101 102 Serial.print("AQI index is.."); 103 Serial.println(number_airindex); //air pollution idex 104 Serial.println(); 105 //Light the LEDs 106 Serial.println("Switch on LED"); 107 //Light the LEDs 108 if (number_airindex >= 300) { 109 // do Thing A 110 strip.ClearTo(darkmag); strip.Show(); 111 } 112 else if (number_airindex >= 200) { 113 // do Thing B 114 strip.ClearTo(darkpurple); strip.Show(); 115 } 116 else if (number_airindex >= 150) { 117 // do Thing B 118 strip.ClearTo(red); strip.Show(); 119 } 120 else if (number_airindex >= 100) { 121 // do Thing B 122 strip.ClearTo(orange); strip.Show(); 123 } 124 else if (number_airindex >= 50) { 125 // do Thing B 126 strip.ClearTo(yellow); strip.Show(); 127 } 128 else { 129 // do Thing C 130 strip.ClearTo(green); strip.Show(); 131 } 132 } 133 134 } else { 135 Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpCode).c_str()); 136 strip.ClearTo(purple); strip.Show(); 137 } 138 http.end(); 139 } 140 141 Serial.println("Here"); 142 delay(sleepSeconds_show); 143 //Turn the LEDs to black 144 strip.ClearTo(black ); strip.Show(); 145 Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal"); 146 ESP.deepSleep(0); 147 148} 149 150void loop() { 151}
Downloadable files
I couldnt work out Fritzing
Here are the connections for the reset button
I couldnt work out Fritzing
Comments
Only logged in users can leave comments