Advertisement
Guest User

Untitled

a guest
Apr 14th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. #include <ESP8266HTTPClient.h> // http web access library
  4.  
  5. #include <ArduinoJson.h> // JSON decoding library
  6. // Libraries for SSD1306 OLED display
  7. #include <Wire.h> // include wire library (for I2C devices such as the SSD1306 display)
  8.  
  9. #include <Adafruit_GFX.h> // include Adafruit graphics library
  10.  
  11. #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver
  12.  
  13. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  14. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  15. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  16. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, & Wire, -1);
  17. const char * ssid = "XXXXXX";
  18. const char * password = "XXXXX";
  19. // set location and API key
  20. //String Location = "City Name, Country Code";
  21. //String API_Key = "Your API Key";
  22. String Location = "Rieti, it";
  23. String API_Key = "XXXXX";
  24.  
  25. void setup(void) {
  26. Serial.begin(9600);
  27. delay(1000);
  28. //Wire.begin(4, 0); // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz
  29. // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  30. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
  31. // init done
  32. Wire.setClock(400000); // set I2C clock to 400kHz
  33. display.clearDisplay(); // clear the display buffer
  34. display.setTextColor(WHITE, BLACK);
  35. display.setTextSize(1);
  36. display.setCursor(0, 0);
  37. display.println(" Internet Weather");
  38. display.print(" Station - RIETI");
  39. display.display();
  40. WiFi.begin(ssid, password);
  41. Serial.print("Connecting.");
  42. display.setCursor(0, 24);
  43. display.println("Connecting...");
  44. display.display();
  45. while (WiFi.status() != WL_CONNECTED) {
  46. delay(500);
  47. Serial.print(".");
  48. }
  49. Serial.println("connected");
  50. display.print("connected");
  51. display.display();
  52. delay(1000);
  53. }
  54. void loop() {
  55. if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
  56. {
  57. HTTPClient http; //Declare an object of class HTTPClient
  58. // specify request destination
  59. http.begin("http://api.openweathermap.org/data/2.5/weather?q=" + Location + "&APPID=" + API_Key); // !!
  60. int httpCode = http.GET(); // send the request
  61. if (httpCode > 0) // check the returning code
  62. {
  63. String payload = http.getString(); //Get the request response payload
  64. DynamicJsonBuffer jsonBuffer(512);
  65. // Parse JSON object
  66. JsonObject & root = jsonBuffer.parseObject(payload);
  67. if (!root.success()) {
  68. Serial.println(F("Parsing failed!"));
  69. return;
  70. }
  71. float temp = (float)(root["main"]["temp"]) - 273.15; // get temperature in °C
  72. //int temp = root["main"]["temp"]; // get temperature in °C
  73. int humidity = root["main"]["humidity"]; // get humidity in %
  74. float pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure in bar
  75. float wind_speed = root["wind"]["speed"]; // get wind speed in m/s
  76. int wind_degree = root["wind"]["deg"]; // get wind degree in °
  77. // print data
  78. Serial.printf("Temperature = %.2f°C\r\n", temp);
  79. Serial.printf("Humidity = %d %%\r\n", humidity);
  80. Serial.printf("Pressure = %.3f bar\r\n", pressure);
  81. Serial.printf("Wind speed = %.1f m/s\r\n", wind_speed);
  82. Serial.printf("Wind degree = %d°\r\n\r\n", wind_degree);
  83. display.setCursor(0, 24);
  84. display.printf("Temperature: %5.2f C\r\n", temp);
  85. //display.printf("Temperature: %d C\r\n", temp);
  86. display.printf("Humidity : %d %%\r\n", humidity);
  87. display.printf("Pressure : %.3fbar\r\n", pressure);
  88. display.printf("Wind speed : %.1f m/s\r\n", wind_speed);
  89. display.printf("Wind degree: %d", wind_degree);
  90. display.drawRect(109, 24, 3, 3, WHITE); // put degree symbol ( ° )
  91. display.drawRect(97, 56, 3, 3, WHITE);
  92. display.display();
  93. }
  94. http.end(); //Close connection
  95. }
  96. delay(20000); // wait 1 minute
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement