#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#define GLUCOSE_SENSOR_PIN 34 // Analog pin connected to the glucose sensor
const char* ssid = "Wokwi-GUEST"; // Enter your WiFi SSID
const char* password = ""; // Enter your WiFi password
WiFiClient client;
unsigned long channelID = 2480894; // Enter your ThingSpeak Channel ID
const char* writeAPIKey = "K7EZA72HWEB5D0NC"; // Enter your ThingSpeak Write API Key
void setup() {
Serial.begin(115200); // Initialize serial communication
setupWiFi();
}
void loop() {
// Read glucose level from sensor
int glucoseLevel = analogRead(GLUCOSE_SENSOR_PIN);
// Send glucose level data to ThingSpeak
sendDataToThingSpeak(glucoseLevel);
// Send glucose level data over UART to Raspberry Pi Pico
Serial.print("Glucose Level: ");
Serial.println(glucoseLevel);
delay(2000); // Delay before next reading
}
void setupWiFi() {
// Connect to WiFi network
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void sendDataToThingSpeak(int glucoseLevel) {
// Initialize ThingSpeak client
ThingSpeak.begin(client);
// Update ThingSpeak fields with glucose level
ThingSpeak.setField(1, glucoseLevel);
// Write data to ThingSpeak
int statusCode = ThingSpeak.writeFields(channelID, writeAPIKey);
if (statusCode == 200) {
Serial.println("ThingSpeak update successful");
} else {
Serial.print("Problem updating ThingSpeak. HTTP error code ");
Serial.println(statusCode);
}
}