#include <WiFi.h>
#include "ThingSpeak.h"
// WiFi + ThingSpeak
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long myChannelNumber = 3050867;
const char* myWriteAPIKey = "EBUTJFMRKDP5DAQ8";
WiFiClient client;
// Global variables to hold last entered values
float temperature = 34, irradiance = 855, vdc = 403, idc = 5.4;
float vac = 228, iac = 7.9, soc = 82, efficiency = 0;
unsigned long lastUpdate = 0;
const unsigned long interval = 15000; // 15 sec
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(".");
}
Serial.println("\nWiFi Connected!");
Serial.println("Enter data in this format:");
Serial.println("Temp Irr Vdc Idc Vac Iac Speed");
}
void loop() {
/* if (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
float values[7];
int index = 0;
char *token;
char buf[100];
line.toCharArray(buf, sizeof(buf));
token = strtok(buf, " ,\t");
while (token != NULL && index < 7) {
values[index++] = atof(token);
token = strtok(NULL, " ,\t");
}
if (index == 7) {*/
float values[7];
temperature = values[0];
irradiance = values[1];
vdc = values[2];
idc = values[3];
vac = values[4];
iac = values[5];
soc = values[6];
efficiency = (vdc * idc > 0) ? (vac * iac) / (vdc * idc) * 100.0 : 0;
Serial.println("✅ Values updated from Serial input.");
/* } else {
Serial.println("⚠️ Please enter 7 values correctly!");
}
}*/
// Every 15 sec → print and send to ThingSpeak
if (millis() - lastUpdate >= interval) {
lastUpdate = millis();
// Print values
Serial.printf("T:%.2f IR:%.2f Vdc:%.2f Idc:%.2f Vac:%.2f Iac:%.2f Spd:%.2f Eff:%.2f%%\n",
temperature, irradiance, vdc, idc, vac, iac, soc, efficiency);
// Send to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, irradiance);
ThingSpeak.setField(3, vdc);
ThingSpeak.setField(4, idc);
ThingSpeak.setField(5, vac);
ThingSpeak.setField(6, iac);
ThingSpeak.setField(7, efficiency);
ThingSpeak.setField(8, soc);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println((x == 200) ? "Update OK" : "Error " + String(x));
}
}