#include<WiFi.h>
#include <ThingSpeak.h> //always include thingspeak header files and custom macros
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_PASS ""// replace with your WiFi password
#define SECRET_CH_ID 2530463// replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "6X7K5AMC31IQ7F13" // replace XYZ with your channel write API Key
char ssid[]= SECRET_SSID;//your network SSID
char pass[]= SECRET_PASS;
int keyIndex=0;
WiFiClient client;
unsigned long myChannelNumber=SECRET_CH_ID;
const char*myWriteAPIKey = SECRET_WRITE_APIKEY;
int temppin=32;
float pinval;
float temp=0.00;
void setup()
{
pinMode(temppin, INPUT);
Serial.begin(115200);
while (!Serial) {;}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop()
{
if(WiFi.status() != WL_CONNECTED)
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPAZ network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
pinval =analogRead(temppin);
temp=pinval*(0.122);
Serial.println(temp);
int x = ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);
if (x==200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel HTTP error code" + String(x));
}
delay (1000);// Wait 7 seconds to update the channel again
}