Skip to content

Instantly share code, notes, and snippets.

@zisra
Last active August 11, 2023 14:39
Show Gist options
  • Save zisra/b7f9b614fdc429e4557320a9a3be2895 to your computer and use it in GitHub Desktop.
Save zisra/b7f9b614fdc429e4557320a9a3be2895 to your computer and use it in GitHub Desktop.
Weather arduino
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <Arduino_JSON.h>
#include <LedControl.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
int latitude = 48.83;
int longitude = 10.09;
const char * ssid = "Obi LAN Kenobi";
const char * password = "IHaveTheHighGround";
JSONVar temperature;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
// DIN = 4, CS = 2, CLK = 3, Number of MAX7219 chips = 1
LedControl lc = LedControl(13, 14, 15, 1);
void displayTemperature(int v) {
int ones;
int tens;
int hundreds;
boolean negative = false;
if (v < -999 || v > 999)
return;
if (v < 0) {
negative = true;
v = v * -1;
}
ones = v % 10;
v = v / 10;
tens = v % 10;
v = v / 10; hundreds = v;
if (negative) {
lc.setChar(0, 3, '-', false);
} else {
lc.setChar(0, 3, ' ', false);
}
//Now print the number digit by digit
lc.setDigit(0, 4, (byte)hundreds, false);
lc.setDigit(0, 3, (byte)tens, true);
lc.setDigit(0, 2, (byte)ones, false);
lc.setChar(0, 0, 'C', false);
}
void displayTime(int hours, int minutes, int seconds) {
lc.setDigit(0, 7, hours / 10, false);
lc.setDigit(0, 6, hours % 10, true);
lc.setDigit(0, 5, minutes / 10, false);
lc.setDigit(0, 4, minutes % 10, true);
lc.setDigit(0, 3, seconds / 10, false);
lc.setDigit(0, 2, seconds % 10, true );
}
JSONVar getTemperature(float latitude, float longitude) {
WiFiClient client;
HTTPClient http;
if (http.begin(client, "rough.isra.workers.dev", 80, String("/?destination=https%3A%2F%2Fapi.brightsky.dev%2Fcurrent_weather%3Flat%3D") + latitude + "%26lon%3D" + longitude, true)) {
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
JSONVar weatherData = JSON.parse(payload);
return weatherData["weather"]["temperature"];
} else {
Serial.print("Error code:");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Unable to connect");
}
}
void setup() {
// The MAX7219 chip is in power-saving mode on startup
lc.shutdown(0, false); // wakeup call
lc.setIntensity(0, 8); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
Serial.begin(115200);
pinMode(D1, INPUT_PULLUP);
WiFi.begin(ssid, password);
Serial.print("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());
timeClient.begin();
timeClient.setTimeOffset(7200);
temperature = getTemperature(latitude, longitude);
}
void loop() {
timeClient.update();
bool isShowingTime;
isShowingTime = digitalRead(D1);
if(isShowingTime == true){
lc.clearDisplay(0);
int currentHour = timeClient.getHours();
int currentMinute = timeClient.getMinutes();
int currentSecond = timeClient.getSeconds();
displayTime(currentHour, currentMinute, currentSecond);
} else {
lc.clearDisplay(0);
displayTemperature(round(double(temperature) * 10));
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment