#include <WiFi.h>
#include <ThingSpeak.h>
#define LIGHT_SENSOR_PIN 34
#define SECRET_SSID "Wokwi-GUEST" // replace with your WiFi network name
#define SECRET_PASS "" // replace with your WiFi password
#define SECRET_CH_ID 616045 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "B1NBFBL42LL9WNK9" // replace XYZ with your channel write API Key
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
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/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
int analogValue = analogRead(LIGHT_SENSOR_PIN);
delay(10); // this speeds up the simulation
//float ADC = 3.3/4096;
float V0 = analogValue*(3.3/4096);
float lux = (250/V0)-50;
// float voltage = analogValue / 4095. * 3.3;
// float resistance = 2000 * voltage / (1 - voltage / 3.3);
// float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println(analogValue);
Serial.println(lux);
ThingSpeak.setField(7, analogValue);
ThingSpeak.setField(8, lux);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(10000); // Wait 7 seconds to update the channel again
delay(5000);
}