Guest User

weather_station_code_merge

a guest
Oct 7th, 2018
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoJson.h>
  3. float tempitod;
  4. float humitod;
  5.  
  6. String result;
  7. char ssid[] = "YourSSID";
  8. char pass[] = "YourPass";
  9. String APIKEY = "YourAPI";
  10. String CityID = "YourCityID";
  11.  
  12. float temps;
  13. float hums;
  14. String request;
  15. WiFiServer server(80);
  16. IPAddress ip(192, 168, 178, 80);
  17. IPAddress gateway(192, 168, 178, 1);
  18. IPAddress subnet(255, 255, 255, 0);
  19.  
  20. WiFiClient client;
  21. char servername[] = "api.openweathermap.org";
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   WiFi.begin(ssid, pass);
  26.   while (WiFi.status() != WL_CONNECTED) {
  27.     delay(500);
  28.   }
  29.   server.begin();
  30. }
  31.  
  32. void loop() {
  33. String data;
  34.     if (client) {
  35.         if (client.connected()) {
  36.           request = client.readStringUntil('\r');
  37.           hums = request.toFloat();
  38.           client.flush();
  39.           request = client.readStringUntil('\r');
  40.           temps = request.toFloat();
  41.           client.flush();
  42.         }
  43.         client.stop();
  44.         data = "temps.txt=\"" + String(temps, 1) + "\"";
  45.         NextionData(data);
  46.         data = "hums.txt=\"" + String(hums, 1) + "\"";
  47.         NextionData(data);
  48.   }
  49.     getWeatherData();
  50.     data = "tempitod.txt=\"" + String(tempitod, 1) + "\"";
  51.     NextionData(data);
  52.     data = "humitod.txt=\"" + String(humitod, 1) + "\"";
  53.     NextionData(data);
  54.     delay(10000);
  55.   }
  56.  
  57. void getWeatherData()
  58. {
  59.   if (client.connect(servername, 80)) {
  60.     client.println("GET /data/2.5/weather?id=" + CityID + "&units=metric&APPID=" + APIKEY);
  61.     client.println("Host: api.openweathermap.org");
  62.     client.println("User-Agent: ArduinoWiFi/1.1");
  63.     client.println("Connection: close");
  64.     client.println();
  65.   }
  66.   while (client.connected() && !client.available()) delay(1);
  67.   while (client.connected() || client.available()) {
  68.     char c = client.read();
  69.     result = result + c;
  70.   }
  71.   client.stop();
  72.   result.replace('[', ' ');
  73.   result.replace(']', ' ');
  74.   char jsonArray [result.length() + 1];
  75.   result.toCharArray(jsonArray, sizeof(jsonArray));
  76.   jsonArray[result.length() + 1] = '\0';
  77.   StaticJsonBuffer<1024> json_buf;
  78.   JsonObject &root = json_buf.parseObject(jsonArray);
  79.   String location = root["name"];
  80.   String country = root["sys"]["country"];
  81.   float temperature = root["main"]["temp"];
  82.   float humidity = root["main"]["humidity"];
  83.   String weather = root["weather"]["main"];
  84.   String description = root["weather"]["description"];
  85.   float pressure = root["main"]["pressure"];
  86.   tempitod = temperature;
  87.   humitod = humidity;
  88. }
  89.  
  90. void NextionData(String data) {
  91.  
  92.   Serial.print(data);
  93.   Serial.write(0xff);
  94.   Serial.write(0xff);
  95.   Serial.write(0xff);
  96.   delay(500);
  97. }
RAW Paste Data