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