#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverUrl = "http://vedamation.mooo.com/aio.php"; // Server URL
// NeoPixel configuration
#define PIN 4 // Pin connected to NeoPixel
#define NUMPIXELS 5 // Number of LEDs in the NeoPixel strip
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// Colors
#define RED pixels.Color(255, 0, 0)
#define GREEN pixels.Color(0, 255, 0)
#define BLUE pixels.Color(0, 0, 255)
#define WHITE pixels.Color(255, 255, 255)
#define OFF pixels.Color(0, 0, 0)
void setup() {
Serial.begin(115200);
// Initialize NeoPixel
pixels.begin();
setPixelColor(0, RED); // Default to red until Wi-Fi connects
pixels.show();
// Start Wi-Fi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi...");
delay(1000);
}
Serial.println("Connected to WiFi");
setPixelColor(0, GREEN); // Green when Wi-Fi is connected
pixels.show();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.setTimeout(5000); // Set timeout to 5 seconds
// Upload sensor data
uploadSensorData(http);
// Fetch and handle switch states
fetchSwitchStates(http);
// Blink blue just once while processing
blinkOncePixel(0, BLUE);
} else {
Serial.println("WiFi Disconnected");
setPixelColor(0, RED); // Red when Wi-Fi is disconnected
pixels.show();
}
delay(10000); // Wait before the next cycle
}
void uploadSensorData(HTTPClient &http) {
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Set content type for POST
// Simulate sensor data
int id = 1;
float temperature = random(20, 35);
float humidity = random(30, 80);
float moistureLevel = random(10, 100);
float waterLevel = random(10, 100);
String weatherCondition = "Sunny";
// Construct POST data
String postData = "id=" + String(id) +
"&temperature=" + String(temperature) +
"&humidity=" + String(humidity) +
"&moisture_level=" + String(moistureLevel) +
"&water_level=" + String(waterLevel) +
"&weather_condition=" + weatherCondition;
// Include switch states
int switch6 = 1;
int switch7 = 0;
int switch11 = 1;
int switch12 = 0;
int switch13 = 1;
int switch14 = 0;
postData += "&switch6=" + String(switch6) +
"&switch7=" + String(switch7) +
"&switch11=" + String(switch11) +
"&switch12=" + String(switch12) +
"&switch13=" + String(switch13) +
"&switch14=" + String(switch14);
// Send the POST request
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response Code (Data Upload): ");
Serial.println(httpResponseCode);
String response = http.getString();
Serial.println("Response from server:");
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(http.errorToString(httpResponseCode));
}
http.end(); // Close connection
}
void fetchSwitchStates(HTTPClient &http) {
http.begin(serverUrl);
int httpCode = http.GET(); // Send the GET request
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Payload received:");
Serial.println(payload);
handleSwitches(payload); // Handle switch states
} else {
Serial.println("Error in HTTP GET request");
}
http.end(); // Close connection
}
void handleSwitches(String payload) {
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
return;
}
// Switch control logic
setSwitchState(doc, "switch6", 1, GREEN);
setSwitchState(doc, "switch7", 2, GREEN);
setSwitchState(doc, "switch11", 3, GREEN);
setSwitchState(doc, "switch12", 4, WHITE);
setSwitchState(doc, "switch13", 3, BLUE);
setSwitchState(doc, "switch14", 2, BLUE);
pixels.show(); // Update all LEDs
}
void setSwitchState(StaticJsonDocument<512> &doc, const char* key, int pixel, uint32_t color) {
if (doc.containsKey(key)) {
int state = doc[key];
if (state == 1) {
pixels.setPixelColor(pixel, color); // Turn on LED with specified color
} else {
pixels.setPixelColor(pixel, OFF); // Turn off LED
}
}
}
// Helper functions for NeoPixel
void setPixelColor(int pixel, uint32_t color) {
pixels.setPixelColor(pixel, color);
}
void blinkOncePixel(int pixel, uint32_t color) {
setPixelColor(pixel, color); // Set the pixel color
pixels.show();
delay(200); // Briefly keep the color
setPixelColor(pixel, OFF); // Turn off the LED
pixels.show();
}