Created
July 26, 2023 05:07
-
-
Save MrCoder/eb3c69207d34fa252dc391e2298990f1 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 <WiFi.h> | |
#include <HTTPClient.h> | |
const char *ssid = ""; // Your WiFi SSID | |
const char *password = ""; // Your WiFi password | |
/** | |
* If WiFi is connected, the ESP32 will use ADC2 pins (25, 26, 27, 14, 12, 13, 4, 2, 15) for WiFi. | |
* We must not use ADC2 pins for anything else. | |
* ADC1 INPUT ONLY: 34, 35, *36, 39 | |
* ADC1 INPUT / OUTPUT: *32, 33 | |
* Other: 23, 22, 1, 3, 21, 19, 18, 5, 17, *16 | |
*/ | |
// RGB led pins | |
const int ledPinGreen = 19; // LED connected to GPIO15 | |
const int ledPinRed = 18; // LED connected to GPIO15 | |
const int ledPinBlue = 5; // LED connected to GPIO15 | |
const int ledPin2 = 17; // LED connected to GPIO23 (source for LDR) | |
const int ldrPin = 36; // LDR connected to GPIO36. | |
const int buzzerPin = 32; // Buzzer connected to GPIO32. | |
bool activeStatus = false; | |
void setup() { | |
Serial.begin(115200); | |
// set up RGB LED and keep them OFF. | |
pinMode(ledPinRed, OUTPUT); | |
pinMode(ledPinGreen, OUTPUT); | |
pinMode(ledPinBlue, OUTPUT); | |
digitalWrite(ledPinRed, HIGH); | |
digitalWrite(ledPinGreen, HIGH); | |
digitalWrite(ledPinBlue, HIGH); | |
// set up LDR and its light source, and turn the light source ON | |
pinMode(ldrPin, INPUT); | |
pinMode(ledPin2, OUTPUT); | |
digitalWrite(ledPin2, HIGH); | |
// set up buzzer and keep it OFF | |
pinMode(buzzerPin, OUTPUT); // Initialize the buzzer pin as output | |
digitalWrite(buzzerPin, LOW); // Make sure the buzzer is off | |
selfCheck(); | |
// Create the task for getActiveStatus | |
xTaskCreate( | |
getActiveStatus, | |
"Active Status Fetch Task", | |
10000, | |
NULL, | |
1, | |
NULL); | |
} | |
void loop() { | |
// Execute the logic previously in the ldrReadTask | |
int ldrValue = analogRead(ldrPin); // Read the value from the LDR | |
Serial.print("LDR Value: "); | |
Serial.println(ldrValue); | |
if ((!isWiFiConnected() || activeStatus) && ldrValue < 1000) { | |
// flash BLUE and buzz | |
digitalWrite(buzzerPin, HIGH); | |
digitalWrite(ledPinBlue, LOW); | |
delay(200); | |
digitalWrite(buzzerPin, LOW); | |
digitalWrite(ledPinBlue, HIGH); | |
} else if(ldrValue < 1000) { | |
// flash GREEN | |
digitalWrite(ledPinGreen, LOW); | |
delay(200); // Buzz for 100ms | |
digitalWrite(ledPinGreen, HIGH); | |
} else if(ldrValue >= 1000) { | |
// flash YELLOW | |
digitalWrite(ledPinGreen, LOW); | |
digitalWrite(ledPinRed, LOW); | |
delay(200); | |
digitalWrite(ledPinGreen, HIGH); | |
digitalWrite(ledPinRed, HIGH); | |
} | |
delay(800); // give a delay; 100ms is too short as it does not give enough time for WiFi reconnecting. | |
} | |
void connectWiFi() { | |
int maxRetries = 7; | |
int retries = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
if (retries >= maxRetries) { | |
Serial.println("Failed to connect to WiFi. Maximum retries reached."); | |
break; | |
} | |
retries++; | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting to WiFi (Retry "); | |
Serial.print(retries); | |
Serial.print("/"); | |
Serial.print(maxRetries); | |
Serial.println(")..."); | |
// Wait for the connection to complete or timeout after 10 seconds | |
unsigned long startTime = millis(); | |
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) { | |
// flash RED | |
digitalWrite(ledPinRed, LOW); | |
delay(250); | |
digitalWrite(ledPinRed, HIGH); | |
delay(250); | |
Serial.print("."); | |
} | |
if (WiFi.status() == WL_CONNECTED) { | |
Serial.println("\nWiFi connected"); | |
} else { | |
Serial.println("\nFailed to connect to WiFi."); | |
} | |
} | |
} | |
bool isWiFiConnected() { | |
return WiFi.status() == WL_CONNECTED; | |
} | |
void getActiveStatus(void *parameter) { | |
for (;;) { | |
connectWiFi(); // Ensure WiFi connection before fetching the status | |
unsigned long startTime = millis(); // Get the start time | |
if (!isWiFiConnected()) { | |
Serial.println("WiFi not connected. Unable to fetch active status."); | |
activeStatus = false; | |
} else { | |
HTTPClient http; | |
// Replace with the actual URL to fetch the active status | |
const char *url = "https://medireminders.com/active"; | |
http.begin(url); | |
int httpCode = http.GET(); | |
if (httpCode == HTTP_CODE_OK) { | |
String response = http.getString(); | |
Serial.print("response: "); | |
Serial.println(response); | |
activeStatus = response.equalsIgnoreCase("true"); | |
} | |
Serial.print("isActive: "); | |
Serial.println(activeStatus); | |
http.end(); | |
unsigned long endTime = millis(); // Get the end time | |
unsigned long executionTime = endTime - startTime; // Calculate the difference | |
Serial.print("Execution time in milliseconds: "); | |
Serial.println(executionTime); | |
} | |
delay(1000); // Give some delay before the next fetch | |
} | |
} | |
void selfCheck() { | |
// Test LDR | |
int ldrValue = digitalRead(ldrPin); | |
Serial.print("LDR Value: "); | |
Serial.println(ldrValue); | |
// Test LED | |
digitalWrite(ledPinBlue, LOW); // Turn on LED | |
delay(1000); // Delay for 1 second | |
digitalWrite(ledPinBlue, HIGH); // Turn off LED | |
digitalWrite(ledPinRed, LOW); // Turn on LED | |
delay(1000); // Delay for 1 second | |
digitalWrite(ledPinRed, HIGH); // Turn off LED | |
digitalWrite(ledPinGreen, LOW); // Turn on LED | |
delay(1000); // Delay for 1 second | |
digitalWrite(ledPinGreen, HIGH); // Turn off LED | |
// Test Buzzer | |
digitalWrite(buzzerPin, HIGH); | |
delay(1000); // Delay for 1 second | |
digitalWrite(buzzerPin, LOW); // Turn off the buzzer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment