#include <ESP8266.h>
// Network information.
#define WIFI_NAME "Wokwi_GUEST"
#define PASSWORD ""
// Hardware information.
#define SENSOR_POWER 13 // Connect the power for the soil sensor here.
#define SOIL_PIN A0 // Connect the sensor output pin here.
#define TIMEOUT 5000 // Timeout for server response.
#define SLEEP_TIME_SECONDS 1800
// ThingSpeak information.
#define NUM_FIELDS 2 // To update more fields, increase this number and add a field label below.
#define SOIL_MOISTURE_FIELD 1 // ThingSpeak field for soil moisture measurement.
#define ELAPSED_TIME_FIELD 2 // ThingSpeak field for elapsed time from startup.
#define THING_SPEAK_ADDRESS "api.thingspeak.com"
String writeAPIKey="TXMQS50YY7QVTN94"; // Change this to the write API key for your channel.
// Global variables.
int numMeasure = 5; // Number of measurements to average.
int ADCValue = 0; // Moisture sensor reading.
WiFiClient client;
void setup()
{
Serial.begin( 115200 ); // You may need to adjust the speed depending on your hardware.
connectWifi();
pinMode( SENSOR_POWER , OUTPUT );
digitalWrite( SENSOR_POWER , LOW ); // Set to LOW so no power is flowing through the sensor.
}
void loop()
{
// Write to successive fields in your channel by filling fieldData with up to 8 values.
String fieldData[ NUM_FIELDS+1 ];
// You can write to multiple fields by storing data in the fieldData[] array, and changing numFields.
// Write the moisture data to field 1.
fieldData[ SOIL_MOISTURE_FIELD ] = String( readSoil( numMeasure ) );
Serial.print( "Soil Moisture = " );
Serial.println( fieldData[ SOIL_MOISTURE_FIELD ] );
// Write the elapsed time from startup to Field 2.
fieldData[ ELAPSED_TIME_FIELD ] = String( millis() );
HTTPPost( NUM_FIELDS , fieldData );
delay( 1000 );
Serial.print( "Goodnight for "+String( SLEEP_TIME_SECONDS ) + " Seconds" );
ESP.deepSleep( SLEEP_TIME_SECONDS * 1000000 );
// If you disable sleep mode, add delay so you don't post to ThingSpeak too often.
// delay( 20000 );
}