· 6 years ago · Nov 02, 2019, 02:46 PM
1// documentation: https://www.arduino.cc/en/Reference/WiFi (WiFi methods under "WiFi class" section)
2#include <WiFi.h>
3
4// documentation: https://links2004.github.io/Arduino/dd/d8d/class_h_t_t_p_client.html#a24fc03fd795d0dbabbdb17f88908b17b
5#include <HTTPClient.h>
6
7// documentation: https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use
8#include <Adafruit_NeoPixel.h>
9
10// documentation: https://arduinojson.org/v6/doc/
11#include <ArduinoJson.h>
12
13
14// ssid and password for WiFi
15const char* ssid = "CaseGuest";
16const char* password = "";
17
18// initialize what pin will be the ledoutput pin and how many LEDs are in the strip
19const int LED_PIN = 16;
20const int LED_COUNT = 1;
21
22// initializes the strip of LEDs. The third input can varry depending on what brand of LEDs are being used. se documentation for more details
23Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN);
24
25// allocates 800 bytes of space for json object. figure out how many bytes to allocate based on certain json here: https://arduinojson.org/v6/assistant/
26DynamicJsonDocument jsonDoc(800);
27
28// previous temperature for LED effect purposes
29float previous_temp = 0;
30
31// http request formatted according to openweathermap api (Note: format will varry depending on API)
32const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=Cleveland,us&APPID=";
33// API key
34const String key = "e8261c7e92b33b8d83971999d77a9421";
35
36void setup() {
37 Serial.begin(115200);
38
39 // join wifi
40 setup_WiFi();
41
42 // prepair LED strip
43 init_LED_off();
44}
45
46// reads temperature value from API every 10 seconds and changed LED color based on the temperature value
47void loop() {
48 // if esp is connected to the WiFi
49 if(WiFi.status() == WL_CONNECTED) {
50
51 // httpclient that will make the API calls
52 HTTPClient http;
53
54 // call to API
55 http.begin(endpoint + key);
56
57 // status of http request (will be > 0 if http call was successful)
58 int httpCode = http.GET();
59
60 // if http request was successful
61 if(httpCode > 0){
62 // data from API in string form (json formatting)
63 String payload = http.getString();
64
65 // turn payload into deserialized json
66 DeserializationError error = deserializeJson(jsonDoc,payload);
67
68 // if there was an error deserializing the json then print that error
69 if (error) {
70 Serial.print(F("deserializeJson() failed: "));
71 Serial.println(error.c_str());
72 }
73
74 // grab temerature (in kelven) from json and convert it to fahrenheit
75 float temp_F = ((float)(jsonDoc["main"]["temp"]) - 273.15) * 9.0/5.0 + 32;
76
77 // print temperature to serial
78 Serial.println(temp_F);
79
80 // change color of LED based on temperature
81 color_from_temp(temp_F);
82
83 previous_temp = temp_F;
84 }
85 // print error message if failed http request
86 else{
87 Serial.println("Error on HTTP Request");
88 }
89 http.end();
90 }
91 delay(10000);
92}
93
94// connect to WiFi
95void setup_WiFi() {
96 WiFi.begin(ssid, password);
97
98 while(WiFi.status() != WL_CONNECTED) {
99 delay(1000);
100 Serial.println("Connecting to the WiFi");
101 }
102 Serial.println("Connected to the WiFi");
103}
104
105// initialize LED strip as off
106void init_LED_off() {
107
108 // initialize LED
109 strip.begin(); // strip.begin(): prepair data pin for neopixel output
110 strip.show(); // strip.show(): takes the color that each LED in the strip is currently set to and displayes it on the LEDs
111
112 // turns off LED
113 strip.clear(); // sets all LEDs in strip to off (RGB value 0, 0, 0)
114 strip.show(); // must have strip.show() after every time the pixel colors are changed otherwise LEDs will not display the set color
115}
116
117// how the LED behaves based on different temp values
118void color_from_temp(float temp) {
119 strip.clear();
120 strip.show();
121 // warm and cold temperature values (in fahrenheit, change based on preference)
122 float warm_temp = 75;
123 float cold_temp = 60;
124
125 // if the temperature increased since last time then blink red
126 if (temp > previous_temp) {
127 blinkRed();
128 }
129 // if the temperature decreased since last time then blink blue
130 else if(temp < previous_temp) {
131 blinkBlue();
132 }
133
134 strip.clear();
135 strip.show();
136
137 // if the temperature is higher then warm temperature then setPixelColor to red
138 if(temp > warm_temp) {
139 strip.setPixelColor(0,255,0,0);
140 }
141 // if the temperature is a medium temperature then setPixelColor to green
142 else if(temp < warm_temp && temp > cold_temp) {
143 strip.setPixelColor(0,0,255,0);
144 }
145 // if the temperature is a cold temperature then setPixelColor to blue
146 else {
147 strip.setPixelColor(0,0,0,255);
148 }
149 strip.show();
150}
151
152// make LED blink red
153void blinkRed() {
154 // turn off LED
155 strip.show();
156 strip.clear();
157 strip.show();
158
159 delay(500);
160
161 // set LED to red
162 strip.setPixelColor(0,255,0,0); // strip.setPixelColor(pixel (0 to [length of strip - 1]), red value (0 to 255), green value (0 to 255), blue value (0 to 255))
163 strip.show();
164
165 delay(1000);
166
167 // turn off LED
168 strip.clear();
169 strip.show();
170
171 delay(500);
172}
173
174// make LED blink blue
175void blinkBlue() {
176 // turn off LED
177 strip.show();
178 strip.clear();
179 strip.show();
180
181 delay(500);
182
183 // set LED to blue
184 strip.setPixelColor(0,0,0,255);
185 strip.show();
186
187 delay(1000);
188
189 // turn off LED
190 strip.clear();
191 strip.show();
192
193 delay(500);
194}