#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(23, 22, 12, 14, 27, 26);
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const String API_key = "5259cb144f0e16bd92aba8d18e79198c";
const String city1 = "Anta Gorda";
const String url1 = "http://api.openweathermap.org/data/2.5/weather?q="+city1+"&appid="+API_key+"&units=metric";
const String city2 = "Curralinho";
const String url2 = "http://api.openweathermap.org/data/2.5/weather?q="+city2+"&appid="+API_key+"&units=metric";
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
WiFi.begin(ssid, password);
pinMode(15, OUTPUT);
Serial.println("Estabelecendo conexão");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("Rede Conectada!");
}
void loop() {
// Busca informações da primeira cidade
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(15, HIGH);
HTTPClient http;
http.begin(url1);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
String resposta = http.getString();
Serial.println(resposta);
char *ptr = strstr((const char *)resposta.c_str(), "name");
String cidade = &ptr[7];
Serial.print(cidade);
cidade.replace('"', '\0');
ptr = strstr((const char *)resposta.c_str(), "temp");
Serial.print(ptr);
String temperatura = &ptr[6];
temperatura.replace(',', '\0');
lcd.clear();
lcd.setCursor(0,0);
lcd.print(cidade.c_str());
lcd.setCursor(0,1);
lcd.print(temperatura.c_str());
lcd.print("\xDF""C");
}
}
else
{
digitalWrite(15, LOW);
WiFi.begin(ssid, password);
Serial.println("Reestabelecendo a conexão...");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
delay(20000);
// Busca informações da segunda cidade
if (WiFi.status() == WL_CONNECTED)
{
digitalWrite(15, HIGH);
HTTPClient http;
http.begin(url2);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK)
{
String resposta = http.getString();
Serial.println(resposta);
char *ptr = strstr((const char *)resposta.c_str(), "name");
String cidade = &ptr[7];
Serial.print(cidade);
cidade.replace('"', '\0');
ptr = strstr((const char *)resposta.c_str(), "temp");
Serial.print(ptr);
String temperatura = &ptr[6];
temperatura.replace(',', '\0');
lcd.clear();
lcd.setCursor(0,0);
lcd.print(cidade.c_str());
lcd.setCursor(0,1);
lcd.print(temperatura.c_str());
lcd.print("\xDF""C");
}
}
else
{
digitalWrite(15, LOW);
WiFi.begin(ssid, password);
Serial.println("Reestabelecendo a conexão...");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
}
}
delay(20000);
}