Hi, i have this code:
#include <ArduinoJson.h>
//#include <SoftwareSerial.h>
#include <WiFi.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "icons.h"
#include <HTTPClient.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//SoftwareSerial HC12(8, 7); // HC-12 TX Pin, HC-12 RX Pin
const char* ssid = "V = R * I"; // SSID of local network
const char* password = "tesstsets"; // Password on network
void setup() {
Serial.begin(9600); // Serial port to computer
//HC12.begin(9600); // Serial port to HC12
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer.
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
WiFi.begin(ssid, password);
int cont = 0;
while (WiFi.status() != WL_CONNECTED) {
display.setCursor(20, 32);
display.print("Connecting...");
display.display();
cont++;
delay(1000);
}
display.clearDisplay();
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
DynamicJsonDocument doc(1024);
http.begin("http://api.openweathermap.org/data/2.5/weather?q=Lisbon,pt&APPID=[my key]"); //Specify the URL
int httpCode = http.GET(); //Make the request
String payload;
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
delay(120000);
return;
}
// Get the root object in the document
JsonObject root = doc.as<JsonObject>();
float windSpeed = root["wind"]["speed"];
int humidity = root["main"]["humidity"];
int pressure = root["main"]["pressure"];
int idd = root["weather"]["id"];
String desc = root["weather"]["description"];
Serial.println(windSpeed);
Serial.println(pressure);
Serial.println(idd);
Serial.println(desc);
}
http.end();
}
display.drawFastHLine(4, 60, 120, WHITE);
display.drawFastHLine(4, 63, 120, WHITE);
display.drawFastVLine(4, 60, 4, WHITE);
display.drawFastVLine(124, 60, 4, WHITE);
//display.drawCircle(64, 32, 3, WHITE);
//fare un last meteo come era, se era sole e continua a essere sole, non richiamare la funzione inutilmente.
display.setCursor(64, 10);
display.print("21.5'C");
display.setCursor(64, 30);
display.print("23.5'C");
display.setCursor(64, 50);
display.print("68%");
display.display();
setIcon("neve");
delay(2000);
setIcon("nuvoloso");
delay(2000);
setIcon("sole");
delay(2000);
setIcon("bruh");
delay(2000);
}
void setIcon(String meteo){
if(meteo == "alba"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, alba, 36, 33, WHITE);
display.display();
} else if (meteo == "fulmini"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, fulmini, 36, 33, WHITE);
display.display();
} else if (meteo == "notte_nuvolosa"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, notte_nuvolosa, 36, 33, WHITE);
display.display();
} else if (meteo == "notte"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, notte, 36, 33, WHITE);
display.display();
} else if (meteo == "nuvoloso"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, nuvoloso, 36, 33, WHITE);
display.display();
} else if (meteo == "pioggia"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, pioggia, 36, 33, WHITE);
display.display();
} else if (meteo == "sole_nuvoloso"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, sole_nuvoloso, 36, 33, WHITE);
display.display();
} else if (meteo == "sole"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, sole, 36, 33, WHITE);
display.display();
} else if (meteo == "tramonto"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, tramonto, 36, 33, WHITE);
display.display();
} else if (meteo == "neve"){
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, neve, 36, 33, WHITE);
display.display();
} else {
display.fillRect(10, 15, 36, 33, BLACK);
display.drawBitmap(10, 15, nointernet, 36, 33, WHITE);
display.display();
}
}
Don't care about the oled display. Btw why when i use:
String desc = root["weather"]["description"];
it gives me a null string. The other parameters works good, like the wind or the pressure.
Really thanks i can't understand.