Hello , I would like to ask any advice or modifications needed for my current esp32 project.
Basically what I am trying to achieve for my program is sending the data of the frequency of a push button pressed to google spreadsheet in a certain period of time (in my case every 15 minutes).
The hardware I'm using is :
ESP32-WROOM-32
This is my current view from spreadsheets. The main problem I am facing is the counter does not increase higher than 1 even though the push button is pressed many times.
Below is my coding for the arduino:
#include <WiFi.h> // Wifi Library
#include <HTTPClient.h>
#define button_pin 14
bool button_state;
bool pastbutton_state;
bool reset_state;
bool pastreset_state;
int counter = 0;
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 60000;
//---------------------------------------------------------------------
const char * ssid = "TT-Visitor"; // type your Wifi name
const char * password = ""; // Type your wifi password
String GOOGLE_SCRIPT_ID = ""; // Type your App Script id (setiap program lain2 ID)
const int sendInterval = 50;
WiFiClientSecure client;
void setup() {
pinMode (button_pin , INPUT);
pinMode(2,OUTPUT);
Serial.begin(9600);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid , password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(2,LOW); // lampu biru esp menyala dan tutup sementara tunggu wifi connect
delay(500);
Serial.print(".");
digitalWrite(2,HIGH);
delay(500);
}
Serial.println("OK"); // dah connect akan keluar " OK "
}
void loop()
{
for (int j=0; j<10 ; j++)
{
for (int i=0; i<1 ; i++)
{
pastbutton_state = button_state ;
button_state = digitalRead(button_pin);
if (button_state != pastbutton_state && button_state == LOW)
{
counter += (i+1);
Serial.print ("Counter increases to ");
Serial.println(counter);
}
delay(40);
}
}
write_google_sheet( "value1="+String(counter));
delay(5000); // The data will be sent to google spreadsheet every 180s = 3minutes)
//counter = 0;
}
///////////////////////////////////////////////
void write_google_sheet(String params) {
HTTPClient http;
String url="https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+params;
Serial.println(url);
Serial.println("Updating the frequency of button pressed");
http.begin(url.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
//Serial.print("HTTP Status Code: ");
// Serial.println(httpCode);
String payload;
if (httpCode > 0) {
payload = http.getString();
// Serial.println("Payload: "+payload);
}
http.end();
}
Thank you very much