Skip to content

Instantly share code, notes, and snippets.

@xxlukas42
Created March 8, 2019 19:24
Show Gist options
  • Save xxlukas42/3085c795999c78926bf8b1055d1476f7 to your computer and use it in GitHub Desktop.
Save xxlukas42/3085c795999c78926bf8b1055d1476f7 to your computer and use it in GitHub Desktop.
Simple sensorless weather station (OpenWeatherMap API)
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <TM1637Display.h>
#define CLK 32
#define DIO 33
const uint8_t CELSIUS[] = {SEG_A | SEG_D | SEG_E | SEG_F};
const uint8_t MINUS[] = {SEG_G};
const uint8_t HUMIDITY[] = {SEG_B | SEG_C | SEG_G | SEG_E | SEG_F};
uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 };
// WIFI
const char* ssid = "YOUR WIFI SSID";
const char* password = "ŸOUR WIFI PASSWORD";
// OPENWEATHER API
const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=brno,cz&units=metric&APPID=";
const String key = "YOUR OPENWEATHER API KEY";
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi network");
display.setBrightness(0x0f);
display.setSegments(data);
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
DynamicJsonDocument doc(1024);
http.begin(endpoint + 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 temperature = root["main"]["temp"];
int humidity = root["main"]["humidity"];
int pressure = root["main"]["pressure"];
display.setSegments(data);
display.showNumberDec(round(humidity),3,2);
display.setSegments(HUMIDITY,1,3);
Serial.println(humidity);
delay(2000);
display.setSegments(data);
display.showNumberDec(pressure);
Serial.println(pressure);
delay(2000);
display.setSegments(data);
temperature = round(temperature);
int len = 1; int pos = 2;
if(temperature < 0){
display.setSegments(MINUS,1,0);
temperature = abs(temperature);
}
if(temperature >= 10) {
len = 2;
pos = 1;
}
display.showNumberDec(round(temperature),false,len,pos);
display.setSegments(CELSIUS,1,3);
Serial.println(temperature);
delay(30000);
} else {
Serial.println("Error on HTTP request");
delay(30000);
}
http.end(); //Free the resources
}
}
@Tizfaver
Copy link

Hi, how can i get the values from weather --> description ?
Because if i set:
String desc = root ["weather"]["description"];
It doesn't work. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment