//#include "WiFi.h"
//#include "ThingSpeak.h"
#include <ESP32Time.h>
#define TIP_SENS_PIN 3 // Hall effect sensor pin
#define DEBOUNCE_DELAY 50 // milliseconds debounce delay
#define conversion_factor 0.00854 // Calibration value
ESP32Time rtc(3600); // offset in seconds GMT+1
volatile int tip_counter = 0;
volatile unsigned long last_tip_time = 0;
/*
// WiFi credentials
const char* ssid = "i47Net";
const char* password = "0000035007abYZ";
// ThingSpeak settings
const char* server = "api.thingspeak.com";
const char* api_key = "S9JOZ8FMJ8ZN11Q9";
*/
//WiFiClient client;
// Function to handle tipping bucket interrupts
// Introduced debouncing concept to ensure the false tipping due to tipping bucket toggle
void IRAM_ATTR handleTip() {
unsigned long current_time = millis();
if (current_time - last_tip_time > DEBOUNCE_DELAY) {
tip_counter++;
last_tip_time = current_time;
}
}
// Function to set the intial time. Note this function needs to be called only once
void time_set()
{
/*Ref format
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30 */
rtc.setTime(30, 40, 20, 3, 8, 2024);
}
void setup() {
Serial.begin(9600);
Serial.println('\n');
pinMode(TIP_SENS_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(TIP_SENS_PIN), handleTip, FALLING);
tip_counter = 0;
time_set();
// Connect to WiFi
/* WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
*/
}
void loop() {
delay(1000); // Wait the software till hardware sensors gets iniatialsed
Serial.print("PulseCount: ");
Serial.println(tip_counter);
Serial.print("Measurement: ");
float Measurement = tip_counter * conversion_factor;
Serial.println(Measurement);
/*Measurement = tip_counter;
if (client.connect(server, 80)) { // Connect to ThingSpeak
String postStr = "GET /update?api_key=" + String(api_key) + "&field1=" + String(Measurement) + " HTTP/1.1\r\n" +
"Host: " + String(server) + "\r\n" +
"Connection: close\r\n\r\n";
client.print(postStr);
delay(1000);
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Connection failed");
}
client.stop();
*/
String tim = rtc.getTime();
Serial.println(tim);
//Serial.println("Hour");
//Serial.println(rtc.getHour(true));
delay(1000); // Delay for 1 seconds to match ThingSpeak's update rate
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1