#include <WiFi.h>
#include <HTTPClient.h>
#include "ThingSpeak.h"
// channel ID : 2801800
// api write : 4HGU4M7QZJ3UJTH9
//#define RAIN_DIGITAL 16
#define WATER_ANALOG 34
#define BEZZO 5
const int MyChannelID = 2801800;
const char* SERVER = "api.thingspeak.com";
const char* API_KEY = "4HGU4M7QZJ3UJTH9";
WiFiClient client;
// استبدل ببيانات شبكتك
const char* SSID = "Wokwi-GUEST";
const char* PASS = "";
// استبدل بعنوان URL الخاص بـ API
const char* serverName = "http://192.168.1.1/api/endpoint";
void setup() {
// put your setup code here, to run once:
pinMode(WATER_ANALOG, INPUT);
//pinMode(RAIN_DIGITAL, INPUT);
pinMode(BEZZO, OUTPUT);
analogReadResolution(10);
Serial.begin(9600);
//Serial.begin(115200);
WiFi.begin(SSID, PASS);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
//WiFi.mode(WIFI_STA);
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
Serial.print("Water amount: ");
Serial.println(analogRead(WATER_ANALOG));
int waterLevel = analogRead(WATER_ANALOG);
if (analogRead(WATER_ANALOG))
Serial.println("It's full!");
if (waterLevel > 200)
tone(BEZZO, 262, 300); // Plays 262Hz tone for 0.250 seconds
else
noTone(BEZZO); // Stop the tone if it’s not raining
ThingSpeak.setField(1,waterLevel);
int x = ThingSpeak.writeFields(MyChannelID,API_KEY);
delay(1000);
}
// #include <WiFi.h>
// #include <HTTPClient.h>
// // استبدل ببيانات شبكتك
// const char* ssid = "Wokwi-GUEST";
// const char* password = "";
// // استبدل بعنوان URL الخاص بـ API
// const char* serverName = "http://YOUR_SERVER_URL/api/endpoint";
// const int rainSensorPin = 34; // ADC pin for the rain sensor
// const int digitalRainSensorPin = 2; // GPIO pin for the digital signal
// void setup() {
// Serial.begin(115200);
// pinMode(rainSensorPin, INPUT);
// pinMode(digitalRainSensorPin, INPUT);
// WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi");
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("Connected to WiFi");
// }
// void loop() {
// int analogValue = analogRead(rainSensorPin);
// int digitalValue = digitalRead(digitalRainSensorPin);
// Serial.print("Analog Value: ");
// Serial.println(analogValue);
// Serial.print("Digital Value: ");
// Serial.println(digitalValue);
// if (WiFi.status() == WL_CONNECTED) {
// HTTPClient http;
// http.begin(serverName);
// http.addHeader("Content-Type", "application/json");
// String jsonData = "{\"analogValue\":" + String(analogValue) + ",\"digitalValue\":" + String(digitalValue) + "}";
// int httpResponseCode = http.POST(jsonData);
// if (httpResponseCode > 0) {
// String response = http.getString();
// Serial.println(httpResponseCode);
// Serial.println(response);
// } else {
// Serial.print("Error on sending POST: ");
// Serial.println(httpResponseCode);
// }
// http.end();
// } else {
// Serial.println("WiFi Disconnected");
// }
// delay(5000); // ارسال البيانات كل 5 ثواني
// }