#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Wokwi-GUEST"; // simulated network SSID
const char* password = ""; // no password needed for the Wokwi simulated network
WiFiClient client;
unsigned long myChannelNumber = 2592564; // replace with your channel number
const char *myWriteAPIKey = "3BUAHLH709EQJ1EC"; // replace with your write API key
// Time variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000; // 30 seconds
// Variable to hold temperature readings
float temperature;
int outputPin = 34; // GPIO pin for ADC on ESP32
void setup() {
Serial.begin(115200); // Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Delay for 1 second
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
int analogValue = analogRead(outputPin);
float millivolts = (analogValue / 4095.0) * 3300; // ESP32 ADC has a 12-bit resolution
temperature = millivolts / 10; // Adjust the conversion factor as needed
Serial.print("Temperature (°C): ");
Serial.println(temperature);
int x = ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}