#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID"; // Enter your WiFi SSID
const char* password = "YOUR_WIFI_PASSWORD"; // Enter your WiFi Password
const char* server = "api.thingspeak.com";
const String apiKey = "6VXGBG0ZJ20F8P95";
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
int sensorValue1 = analogRead(33); // Read from the first potentiometer connected to pin 12
int sensorValue2 = analogRead(32); // Read from the second potentiometer connected to pin 14
float voltage1 = sensorValue1 * (5.0 / 4095); // Convert sensor value to voltage for potentiometer 1
float voltage2 = sensorValue2 * (5.0 / 4095); // Convert sensor value to voltage for potentiometer 2
Serial.print("Voltage 1: ");
Serial.println(voltage1);
Serial.print("Voltage 2: ");
Serial.println(voltage2);
sendToThingSpeak(voltage1, voltage2);
delay(100); // Adjust the delay as needed
}
// void connectWiFi() {
// Serial.println("Connecting to Wi-Fi");
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("Connected to Wi-Fi");
// }
void sendToThingSpeak(float value1, float value2) {
HTTPClient http;
String url = "http://" + String(server) + "/update?api_key=" + apiKey + "&field1=" + String(value1) + "&field2=" + String(value2);
Serial.print("Sending data to ThingSpeak: ");
Serial.println(url);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
// #include <WiFi.h>
// #include <HTTPClient.h>
// const char* ssid = "dlink-DE90"; // Enter your WiFi SSID
// const char* password = "qqcym97979"; // Enter your WiFi Password
// const char* server = "api.thingspeak.com";
// const String apiKey = "6VXGBG0ZJ20F8P95";
// void loop() {
// int sensorValue = analogRead(33);
// float voltage = sensorValue * (5.0 / 4095);
// Serial.println(voltage);
// sendToThingSpeak(voltage);
// delay(100);
// }
// // void connectWiFi() {
// // Serial.println("Connecting to Wi-Fi");
// // WiFi.begin(ssid, password);
// // while (WiFi.status() != WL_CONNECTED) {
// // delay(500);
// // Serial.print(".");
// // }
// // Serial.println("Connected to Wi-Fi");
// // }
// void sendToThingSpeak(float value) {
// HTTPClient http;
// String url = "http://api.thingspeak.com/update?api_key=" + apiKey + "&field1=" + String(value);
// Serial.print("Sending data to ThingSpeak: ");
// Serial.println(url);
// http.begin(url);
// int httpResponseCode = http.GET();
// if (httpResponseCode > 0) {
// String response = http.getString();
// Serial.print("HTTP Response code: ");
// Serial.println(httpResponseCode);
// Serial.println(response);
// } else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// }
// http.end();
// }