#define BLYNK_PRINT Serial
// Token dari akun Blynk
#define BLYNK_TEMPLATE_ID "TMPL6r_vKBSb8"
#define BLYNK_TEMPLATE_NAME "PKM UNIMED"
#define BLYNK_AUTH_TOKEN "8VQI6rJ7Sh45dv84NTJ_V_N8kjeOK5q2"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define uS_TO_S_FACTOR 1000000 // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 15 // Time ESP32 will go to sleep (in seconds)
RTC_DATA_ATTR int bootCount =0; // holds the total boot counts
// Moisture sensor is connected to GPIO 34 (Analog ADC1_CH6)
const int sensorPin = 34;
// Untuk koneksi ke Internet / Wifi
char auth[] = BLYNK_AUTH_TOKEN; //Auth Token
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan
// this function reads the sensor values and send them to blynk
void publishSensorValues()
{
// how many readings to take
#define MAX_SENSOR_READS 10
// variable for storing the moisture sensor value
int sensorValue = 0;
// Reading moisture sensor values
for (int i = 0; i < MAX_SENSOR_READS; i++)
{
sensorValue += analogRead(sensorPin);
delay(20);
}
sensorValue = sensorValue / MAX_SENSOR_READS;
// Sending to blynk
Blynk.virtualWrite(V0, sensorValue);
// Send uptime
Blynk.virtualWrite(V1, bootCount);
}
void setup()
{
// Debug console
Serial.begin(115200);
delay(500); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// schedule a wakeup in the future
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
//Blynk.begin(auth, ssid, pass);
// setup the connection to blynk
Blynk.config(auth);
// setup WiFi
Blynk.connectWiFi(ssid, pass);
//will continue trying to connect to Blynk server.
//Returns true when connected, false if timeout has been reached.
//Default timeout is 30 seconds.
bool result = Blynk.connect();
if (result)
{
// read and publish the sensor values
publishSensorValues();
Serial.println("Publish Data Berhasil ! ");
}
Serial.println("Going to sleep now");
Serial.flush();
Blynk.disconnect();
esp_deep_sleep_start();
}
void loop()
{
}