Created
November 7, 2023 15:49
-
-
Save Ajak58a/148c687ce555a57de695f9aa153cf044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClient.h> | |
#include <ArduinoJson.h> | |
const char* ssid = "replacewithyourSSID"; | |
const char* password = "replacewithyourNetworkPassword"; | |
const char* API_KEY = "replacewithyourWeatherAPIkey"; | |
long sunriseTimeMinutes; | |
long localTimeMinutes; | |
boolean setSunriseAlarm = false; | |
const int buttonPin = 4; // the number of the pushbutton pin | |
const int buzzerPin = 5; | |
const int ledPin = 10; | |
bool ledState = LOW; | |
void setup() { | |
pinMode(buttonPin, INPUT); | |
pinMode(buzzerPin, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
Serial.begin(115200); | |
// Connect to Wi-Fi | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
long convertToMinutes(const char* time12) { | |
int h, m; | |
char ampm[3]; | |
// Extract hour, minute, and AM/PM from the input string | |
sscanf(time12, "%d:%d %2s", &h, &m, ampm); | |
// Convert to 24-hour format | |
if (strcmp(ampm, "PM") == 0 && h != 12) { | |
h += 12; | |
} | |
else if (strcmp(ampm, "AM") == 0 && h == 12) { | |
h = 0; | |
} | |
long timeInMinutes = h*60+m; | |
return timeInMinutes; | |
} | |
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 getSunriseTime(){ | |
WiFiClient client; | |
HTTPClient http; | |
String endpoint = "http://api.weatherapi.com/v1/forecast.json?key=" + String(API_KEY) + "&q=auto:ip&days=1"; | |
// Specify the IP geolocation service's endpoint | |
http.begin(client, endpoint); | |
// Begin the request | |
int httpCode = http.GET(); | |
// If the request was successful, handle the response | |
if (httpCode > 0) { | |
String payload = http.getString(); | |
Serial.println(payload); // Print the response payload | |
int sunrisePos = payload.indexOf("\"sunrise\":\""); | |
if (sunrisePos > 0) { | |
int startTime = sunrisePos + 11; // move past the identifier | |
int endTime = payload.indexOf("\"", startTime); | |
String sunriseTime = payload.substring(startTime, endTime); | |
Serial.println("Next Sunrise Time: " + sunriseTime); | |
char time12[12]; | |
stringToCharArray(sunriseTime, time12, sizeof(time12)); | |
long sunriseTimeMinutes = convertToMinutes(time12); | |
Serial.println(sunriseTimeMinutes); | |
} | |
} else { | |
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
// End the connection | |
http.end(); | |
} | |
void getCurrentTime(){ | |
int y, mn, d, h, m; | |
WiFiClient client; | |
HTTPClient http; | |
String endpoint = "http://api.weatherapi.com/v1/current.json?key=" + String(API_KEY) + "&q=auto:ip"; | |
http.begin(client, endpoint); | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
String payload = http.getString(); | |
int timePos = payload.indexOf("\"localtime\":\""); | |
if (timePos > 0) { | |
int startTime = timePos + 13; // move past the identifier | |
int endTime = payload.indexOf("\"", startTime); | |
String localtime = payload.substring(startTime, endTime); | |
Serial.println("Current Local Time: " + localtime); | |
char time12[18]; | |
stringToCharArray(localtime, time12, sizeof(time12)); | |
sscanf(time12, "%d-%d-%d %d:%d", &y, &mn, &d, &h, &m); | |
long localTimeMinutes = h*60+m; | |
Serial.println(localTimeMinutes); | |
} | |
} else { | |
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
// End the connection | |
http.end(); | |
} | |
void loop() { | |
if (WiFi.status() == WL_CONNECTED) { | |
getSunriseTime(); | |
if(digitalRead(buttonPin)==LOW){ | |
setSunriseAlarm = !setSunriseAlarm; | |
ledState = !ledState; | |
digitalWrite(ledPin, ledState); | |
delay(50); | |
} | |
if(setSunriseAlarm){ | |
Serial.println("Sunrise Alarm is Set"); | |
getCurrentTime(); | |
if(localTimeMinutes-sunriseTimeMinutes==0){ | |
digitalWrite(buzzerPin, HIGH); | |
delay(60000); | |
digitalWrite(buzzerPin, LOW); | |
} | |
} | |
delay(60000); // Delay for 1 minute before fetching again | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment