Skip to content

Instantly share code, notes, and snippets.

@gaspart
Last active February 15, 2021 08:58
Show Gist options
  • Save gaspart/b3c30fb65f75b49a2879d307bf81e194 to your computer and use it in GitHub Desktop.
Save gaspart/b3c30fb65f75b49a2879d307bf81e194 to your computer and use it in GitHub Desktop.
Current Weather with NodeMCU & openweathermap
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-http-get-open-weather-map-thingspeak-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
int RossoLed = 16;
int GialloLed = 5;
int BluLed = 12;
int VerdeLed = 14;
String serverPath;
int weathercode;
int len;
int Rosso [] = {202, 211, 212, 503, 504, 602, 781};
int RossoGiallo [] = {201, 221, 232, 502, 522, 601, 622};
int Giallo [] = {200, 210, 230, 231, 302, 312, 313, 314, 321, 501, 511, 520, 521, 531, 600, 611, 612, 613, 615, 616, 620, 621, 771};
int GialloVerde [] = {301, 310, 311, 500};
int Verde [] = {300, 701, 711, 721, 731, 741, 751, 761, 762, 803, 804};
int VerdeBlu [] = {802};
int Blu [] = {800, 801};
const char* ssid = "FRITZ!Box 7590 DJ";
const char* password = "0075914042";
// Your Domain name with URL path or IP address with path
String openWeatherMapApiKey = "0074439922dd26b4e08256e394f2ae4f";
// Example:
//String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd4";
// Replace with your country code and city
String city = "Como";
String countryCode = "IT";
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 100000;
String jsonBuffer;
void setup() {
pinMode(RossoLed, OUTPUT);
pinMode(GialloLed, OUTPUT);
pinMode(VerdeLed, OUTPUT);
pinMode(BluLed, OUTPUT);
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, LOW);
digitalWrite(VerdeLed, LOW);
digitalWrite(BluLed, LOW);
// Serial.begin(115200);
WiFi.begin(ssid, password);
// Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
// Serial.println("");
// Serial.print("Connected to WiFi network with IP Address: ");
// Serial.println(WiFi.localIP());
// Serial.println("Timer set to 10 minutes (timerDelay variable), it will take 10 minutes before publishing the first reading.");
}
void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
// Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
// Serial.println("Parsing input failed!");
digitalWrite(RossoLed, HIGH);
digitalWrite(GialloLed, HIGH);
digitalWrite(VerdeLed, HIGH);
digitalWrite(BluLed, HIGH);
return;
}
/*
Serial.print("JSON object = ");
Serial.println(myObject);
Serial.print("Temperature: ");
Serial.println(myObject["main"]["temp"]);
Serial.print("Pressure: ");
Serial.println(myObject["main"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["main"]["humidity"]);
Serial.print("Wind Speed: ");
Serial.println(myObject["wind"]["speed"]);
Serial.print("Main: ");
Serial.println(myObject["weather"][0]["main"]);
Serial.print("Description: ");
Serial.println(myObject["weather"][0]["description"]);
*/
weathercode = (myObject["weather"][0]["id"]);
// Serial.println(weathercode);
len = sizeof(Rosso)/sizeof(Rosso[0]);
for (int i=0; i<len; i++) {
if (weathercode == Rosso[i]) {
// Serial.println("Rosso");
digitalWrite(RossoLed, HIGH);
digitalWrite(GialloLed, LOW);
digitalWrite(VerdeLed, LOW);
digitalWrite(BluLed, LOW);
}
}
len = sizeof(RossoGiallo)/sizeof(RossoGiallo[0]);
for (int i=0; i<len; i++) {
if (weathercode == RossoGiallo[i]) {
// Serial.println("RossoGiallo");
digitalWrite(RossoLed, HIGH);
digitalWrite(GialloLed, HIGH);
digitalWrite(VerdeLed, LOW);
digitalWrite(BluLed, LOW);
}
}
len = sizeof(Giallo)/sizeof(Giallo[0]);
for (int i=0; i<len; i++) {
if (weathercode == Giallo[i]) {
// Serial.println("Giallo");
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, HIGH);
digitalWrite(VerdeLed, LOW);
digitalWrite(BluLed, LOW);
}
}
len = sizeof(GialloVerde)/sizeof(GialloVerde[0]);
for (int i=0; i<len; i++) {
if (weathercode == GialloVerde[i]) {
// Serial.println("GialloVerde");
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, HIGH);
digitalWrite(VerdeLed, HIGH);
digitalWrite(BluLed, LOW);
}
}
len = sizeof(Verde)/sizeof(Verde[0]);
for (int i=0; i<len; i++) {
if (weathercode == Verde[i]) {
// Serial.println("Verde");
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, LOW);
digitalWrite(VerdeLed, HIGH);
digitalWrite(BluLed, LOW);
}
}
len = sizeof(VerdeBlu)/sizeof(VerdeBlu[0]);
for (int i=0; i<len; i++) {
if (weathercode == VerdeBlu[i]) {
// Serial.println("VerdeBlu");
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, LOW);
digitalWrite(VerdeLed, HIGH);
digitalWrite(BluLed, HIGH);
}
}
len = sizeof(Blu)/sizeof(Blu[0]);
for (int i=0; i<len; i++) {
if (weathercode == Blu[i]) {
// Serial.println("Blu");
digitalWrite(RossoLed, LOW);
digitalWrite(GialloLed, LOW);
digitalWrite(VerdeLed, LOW);
digitalWrite(BluLed, HIGH);
}
}
}
else {
// Serial.println("WiFi Disconnected");
digitalWrite(RossoLed, HIGH);
digitalWrite(GialloLed, HIGH);
digitalWrite(BluLed, HIGH);
digitalWrite(VerdeLed, HIGH);
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
payload = http.getString();
}
else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment