Skip to content

Instantly share code, notes, and snippets.

@Ajak58a
Created April 21, 2024 18:48
Show Gist options
  • Save Ajak58a/c32cb6e4200dbfc3d3a35d6dc0f4240b to your computer and use it in GitHub Desktop.
Save Ajak58a/c32cb6e4200dbfc3d3a35d6dc0f4240b to your computer and use it in GitHub Desktop.
IoT Digital Watch
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_MOSI 23
#define OLED_CLK 18
#define OLED_DC 16
#define OLED_CS 5
#define OLED_RESET 17
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const char* ssid = "REPLACE_WITH_YOUR_OWN_WIFI_SSID";
const char* password = "REPLACE_WITH_YOUR_OWN_WIFI_PASSWORD";
const char* API_KEY = "REPLACE_WITH_YOUR_OWN_WEATHERAPI_API_KEY";
int y, mn, d, h, m, day;
String datetime;
const char* weekdays[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
const char* Month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double temp_c, hum;
StaticJsonDocument<200> doc;
void setup()
{
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());
if(!display.begin(SSD1306_SWITCHCAPVCC))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
delay(1000);
}
void stringToCharArray(String inputString, char* outputBuffer, int bufferSize) {
if (inputString.length() < bufferSize) {
inputString.toCharArray(outputBuffer, bufferSize);
} else {
// Handle the case where the string is too long for the buffer
Serial.println("Error: Buffer size is too small for the input string.");
}
}
void fetchData(){
HTTPClient http;
String endpoint = "http://api.weatherapi.com/v1/current.json?key=" + String(API_KEY) + "&q=auto:ip";
http.begin(endpoint.c_str());
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject location = doc["location"]; // Get the "current" object
String localtime = location["localtime"];
datetime = localtime;
char time12[18];
stringToCharArray(datetime, time12, sizeof(time12));
sscanf(time12, "%d-%d-%d %d:%d", &y, &mn, &d, &h, &m);
JsonObject current = doc["current"]; // Get the "current" object
double temp = current["temp_c"];
temp_c = temp;
double humidity = current["humidity"];
hum = humidity;
int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) daysInMonth[2] = 29;
int q = d;
int mon = mn;
int Yr = (mn <= 2) ? y - 1 : y;
int K = Yr % 100;
int J = Yr / 100;
int h = (q + ((13 * (mon + 1)) / 5) + K + (K / 4) + (J / 4) - (2 * J)) % 7;
day = (h + 5) % 7;
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
// End the connection
http.end();
}
void loop()
{
fetchData();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(20,5);
display.print(Month[mn-1]);
display.print(" ");
display.print(d);
display.print(" , ");
display.print(y);
display.setTextSize(2);
display.setCursor(10,22);
if(h<10) display.print("0");
display.print(h);
display.print(":");
if(m<10) display.print("0");
display.print(m);
display.print(" ");
display.print(weekdays[day]);
display.setTextSize(1);
display.setCursor(20,55);
display.print(temp_c);
display.print("C");
display.print(" ");
display.print(hum);
display.print("%");
display.display();
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment