Advertisement
Guest User

OWM

a guest
Jan 5th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266mDNS.h>
  4. #include <ESP8266HTTPClient.h>
  5. #include <Time.h>
  6. #include <TimeLib.h>
  7. #include <OpenWeatherMap.h>
  8.  
  9. const char *ow_key = "Your Open Weather Map key here";
  10. const char *nodename = "esp8266-weather";
  11. const char *wifi_ssid = "ssid";
  12. const char *wifi_passwd = "password";
  13.  
  14.  
  15. //==============================================================================================================
  16.  
  17. typedef enum wifi_s {
  18. W_AP = 0, W_READY, W_TRY
  19. } WifiStat;
  20.  
  21. OWMconditions owCC;
  22. OWMfiveForecast owF5;
  23. OWMsixteenForecast owF16;
  24. WifiStat WF_status;
  25.  
  26. void connectWiFiInit(void) {
  27. WiFi.hostname(nodename);
  28. String ssid = wifi_ssid;
  29. String passwd = wifi_passwd;
  30. WiFi.begin(ssid.c_str(), passwd.c_str());
  31. }
  32.  
  33. String dateTime(String timestamp) {
  34. time_t ts = timestamp.toInt();
  35. char buff[30];
  36. sprintf(buff, "%2d:%02d %02d-%02d-%4d", hour(ts), minute(ts), day(ts), month(ts), year(ts));
  37. return String(buff);
  38. }
  39.  
  40. void setup() {
  41. Serial.begin(115200);
  42. Serial.println("\n\n\n\n");
  43.  
  44. connectWiFiInit();
  45. WF_status = W_TRY;
  46. }
  47.  
  48. void currentConditions(void) {
  49. OWM_conditions *ow_cond = new OWM_conditions;
  50. owCC.updateConditions(ow_cond, ow_key, "ru", "Moscow", "metric");
  51. Serial.print("Latitude & Longtitude: ");
  52. Serial.print("<" + ow_cond->longtitude + " " + ow_cond->latitude + "> @" + dateTime(ow_cond->dt) + ": ");
  53. Serial.println("icon: " + ow_cond->icon + ", " + " temp.: " + ow_cond->temp + ", press.: " + ow_cond->pressure);
  54. delete ow_cond;
  55. }
  56.  
  57. void fiveDayFcast(void) {
  58. OWM_fiveForecast *ow_fcast5 = new OWM_fiveForecast[40];
  59. byte entries = owF5.updateForecast(ow_fcast5, 40, ow_key, "ru", "Moscow", "metric");
  60. Serial.print("Entries: "); Serial.println(entries+1);
  61. for (byte i = 0; i <= entries; ++i) {
  62. Serial.print(dateTime(ow_fcast5[i].dt) + ": icon: ");
  63. Serial.print(ow_fcast5[i].icon + ", temp.: [" + ow_fcast5[i].t_min + ", " + ow_fcast5[i].t_max + "], press.: " + ow_fcast5[i].pressure);
  64. Serial.println(", descr.: " + ow_fcast5[i].description + ":: " + ow_fcast5[i].cond + " " + ow_fcast5[i].cond_value);
  65. }
  66. delete[] ow_fcast5;
  67. }
  68.  
  69. void sixteedDayFcast(void) {
  70. OWM_sixteenLocation *location = new OWM_sixteenLocation;
  71. OWM_sixteenForecast *ow_fcast16 = new OWM_sixteenForecast[7];
  72. byte entries = owF16.updateForecast(location, ow_fcast16, 7, ow_key, "ru", "Moscow", "metric");
  73. Serial.print("Entries: "); Serial.println(entries+1);
  74. Serial.print(location->city_name + ", " + location->country +"(" + location->city_id);
  75. Serial.println(") <" + location->longtitude + ", " + location->latitude + "> :");
  76. for (byte i = 0; i <= entries; ++i) {
  77. Serial.print(dateTime(ow_fcast16[i].dt) + ": icon: ");
  78. Serial.print(ow_fcast16[i].icon + ", temp.: ");
  79. Serial.print("{" + ow_fcast16[i].t_min + ", " + ow_fcast16[i].t_max + "}, temp. change: ");
  80. Serial.print(ow_fcast16[i].t_night + " -> " + ow_fcast16[i].t_morning + " -> " + ow_fcast16[i].t_day + " -> " + ow_fcast16[i].t_evening);
  81. Serial.println("; pressure: " + ow_fcast16[i].pressure + ", descr.:" + ow_fcast16[i].description);
  82. }
  83. delete location;
  84. delete[] ow_fcast16;
  85. }
  86.  
  87. void loop() {
  88. if (WF_status == W_TRY) {
  89. if (WiFi.status() == WL_CONNECTED) {
  90. MDNS.begin(nodename);
  91. WF_status = W_READY;
  92. Serial.println("Current Conditions: ");
  93. currentConditions();
  94. Serial.println("Five days forecast: ");
  95. fiveDayFcast();
  96. Serial.println("16 days forecast: ");
  97. sixteedDayFcast();
  98. }
  99. }
  100. delay(1000);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement