· 5 years ago · Jan 16, 2021, 05:34 PM
1#include <WiFi.h>
2#include <ESPmDNS.h>
3#include <WebServer.h>
4#include <ArduinoOTA.h>
5#include <Arduino_JSON.h>
6#include <stdio.h>
7#include <HTTPClient.h>
8
9const char* ssid = "...";
10const char* password = "...";
11
12WebServer server(80);
13
14const char* www_username = "admin";
15const char* www_password = "esp32";
16
17unsigned long lastMillis = 0;
18unsigned long updateMillis = 0;
19
20struct forecast
21{
22 float temperature;
23 float humidity;
24 float windSpeed;
25 const char* weather;
26 const char* loc;
27};
28const char* url = "http://api.openweathermap.org/data/2.5/weather?q={location}&appid={your api key}";
29
30JSONVar Data = null;
31forecast WeatherData;
32String response = "";
33
34unsigned long request_timeout = 900000;
35unsigned long update_timeout = 60000;
36
37void setup() {
38 Serial.begin(115200);
39 delay(4000);
40
41 WiFi.mode(WIFI_STA);
42 WiFi.begin(ssid, password);
43 if (WiFi.waitForConnectResult() != WL_CONNECTED) {
44 Serial.println("WiFi Connect Failed! Rebooting...");
45 delay(1000);
46 ESP.restart();
47 }
48 ArduinoOTA.begin();
49
50 Serial.println("Connected to the WiFi network");
51 response = GetResponse(url);
52
53 if(response == "error")
54 {
55 Serial.println("Error");
56 }
57 else if(response == "network error")
58 {
59 Serial.println("Network error");
60 }
61 else
62 {
63
64 Data = JSON.parse(response);
65 WeatherData.temperature = (float)((double)Data["main"]["temp"] - 273.15);
66 WeatherData.weather = Data["weather"][0]["main"];
67 WeatherData.humidity = (float)((double)Data["main"]["humidity"] - 0.00);
68 WeatherData.windSpeed = (float)((double)Data["wind"]["speed"] - 0.00);
69 WeatherData.loc = Data["name"];
70
71 Serial.println(WeatherData.temperature);
72 Serial.println(WeatherData.weather);
73 Serial.println(WeatherData.humidity);
74 Serial.println(WeatherData.windSpeed);
75 Serial.println(WeatherData.loc);
76 }
77 server.on("/", []() {
78 if (!server.authenticate(www_username, www_password)) {
79 return server.requestAuthentication();
80 }
81 server.send(200, "text/html", html(WeatherData));
82 });
83 server.begin();
84
85 Serial.print("http://");
86 Serial.println(WiFi.localIP());
87 lastMillis = millis();
88 updateMillis = millis();
89}
90
91void loop()
92{
93 ArduinoOTA.handle();
94 server.handleClient();
95 if((millis() - lastMillis) >= request_timeout)
96 {
97 response = GetResponse(url);
98 if(response == "error")
99 {
100 Serial.println("Error");
101 }
102 else if(response == "network error")
103 {
104 Serial.println("Network error");
105 }
106 else
107 {
108 Data = JSON.parse(response);
109 }
110 lastMillis = millis();
111 }
112 if((millis() - updateMillis) >= update_timeout)
113 {
114 Data = JSON.parse(response);
115 WeatherData.temperature = (float)((double)Data["main"]["temp"] - 273.15);
116 WeatherData.weather = Data["weather"][0]["main"];
117 WeatherData.humidity = (float)((double)Data["main"]["humidity"] - 0.00);
118 WeatherData.windSpeed = (float)((double)Data["wind"]["speed"] - 0.00);
119 WeatherData.loc = Data["name"];
120
121 Serial.println(WeatherData.temperature);
122 Serial.println(WeatherData.weather);
123 Serial.println(WeatherData.humidity);
124 Serial.println(WeatherData.windSpeed);
125 Serial.println(WeatherData.loc);
126 updateMillis = millis();
127 }
128}
129
130String GetResponse(String url)
131{
132 String payload = "";
133 if ((WiFi.status() == WL_CONNECTED)) {
134 HTTPClient http;
135 http.begin(url);
136 int httpCode = http.GET();
137 if (httpCode > 0)
138 {
139 payload = http.getString();
140 Serial.print("Response code: ");
141 Serial.println(httpCode);
142 }
143 else
144 {
145 Serial.println("Error on HTTP request");
146 payload = "error";
147 }
148 http.end();
149 }
150 else payload = "network error";
151 return payload;
152}
153String html(forecast &weather)
154{
155 String text = "<!DOCTYPE html>\n";
156 text+= "<html>\n";
157 text+= "<head>\n";
158 text+= "<meta charset=\"UTF-8\">\n";
159 text+= "<meta name=\"description\" content=\"test\">\n";
160 text+= "<title>esp32</title>\n";
161 text+= "</head>\n";
162 text+= "<body>\n";
163 text+= "<h1>Forecast</h1>\n";
164 text+= "<p>Location: ";
165 text+= weather.loc;
166 text+= "</p>\n";
167 text+= "<p>Temperature: ";
168 text+= weather.temperature;
169 text+= "C</p>\n";
170 text+= "<p>Weather: ";
171 text+= weather.weather;
172 text+= "</p>\n";
173 text+= "<p>Humidity: ";
174 text+= weather.humidity;
175 text+= "%</p>\n";
176 text+= "<p>Wind Speed: ";
177 text+= weather.windSpeed;
178 text+= "m/s</p>\n";
179 text+= "</body>\n";
180 text+= "</html>";
181 return text;
182}