//////////////////////////
//DEFINE
////////////////////////
#define VERSION "1.0"
#define BUTTON_PIN 21
#define LED_IN 2
#define LED_SW 19
//////////////////////////////
// GLOBAL VARIABLES
/////////////////////////////
const char * root_ca=\
"-----BEGIN CERTIFICATE-----\n" \
"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" \
"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv\n" \
"b3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAw\n" \
"MDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNVBAYTAkJFMRkwFwYDVQQKExBHbG9i\n" \
"YWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYDVQQDExJHbG9iYWxT\n" \
"aWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDaDuaZ\n" \
"jc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavp\n" \
"xy0Sy6scTHAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp\n" \
"1Wrjsok6Vjk4bwY8iGlbKk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdG\n" \
"snUOhugZitVtbNV4FpWi6cgKOOvyJBNPc1STE4U6G7weNLWLBYy5d4ux2x8gkasJ\n" \
"U26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrXgzT/LCrBbBlDSgeF59N8\n" \
"9iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8E\n" \
"BTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0B\n" \
"AQUFAAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOz\n" \
"yj1hTdNGCbM+w6DjY1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE\n" \
"38NflNUVyRRBnMRddWQVDf9VMOyGj/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymP\n" \
"AbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhHhm4qxFYxldBniYUr+WymXUad\n" \
"DKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" \
"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" \
"-----END CERTIFICATE-----\n";
#ifdef ESP32
#include <WiFi.h>
#include <Wire.h>
#include <ESP32Servo.h>
#include <HTTPClient.h>
Servo myservo;
#endif
//google
// const char* host = "script.google.com";
// const int httpsPort = 443;
String GAS_ID = "AKfycbxRqQkwG90bDc7Rgr78LML0QC9zGOaWhMQFRpfZquEoXbzl30q7NZsFnCNZfZ6sQtM5dw"; //--> spreadsheet script ID
///
// Config Wifi//
typedef struct {
char ssid[64] = "Wokwi-GUEST";
char pass[64] = "";
} config_wf;
config_wf cfg_wifi;
TimerHandle_t wifiReconnectTimer;
// WIFI
#ifdef ESP32
#include <WiFi.h>
#include <Wire.h>
#endif
// Config Debounce sw//
typedef struct {
bool lastSteadyState = 0; // the previous steady state from the input pin
bool lastFlickerableState = 0; // the previous flickerable state from the input pin
bool currentState = 0; // the current reading from the input pin
uint16_t check = 0;
uint32_t lastDebounceTime = 0; // the last time the output pin was toggled
uint16_t Debouncetime = 50; //#define DEBOUNCE_TIME 50
} config_sw;
config_sw cfg_sw;
// SENSOR VALUE
typedef struct {
uint32_t WeightScale = 0;
} sensor_t;
sensor_t sensor;
TimerHandle_t sensorTimer;
// Config Timezone///
typedef struct {
char NTP_SERVER[64] = "ch.pool.ntp.org";
char TZ_INFO[64] = "<+07>-7";
} config_t;
config_t cfg_TZ;
char time_output[30];
char starttime[30];
char endtime[30];
tm timeinfo;
time_t now;
/////////////////////////////////////
// Setup
////////////////////////////////////
// WIFI
void wifi_setup() {
Serial.println("WIFI: SETUP");
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(wifi_connect));
wifi_connect();
}
void wifi_connect() {
Serial.println("WIFI: Connecting...");
WiFi.begin(cfg_wifi.ssid, cfg_wifi.pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
digitalWrite(LED_IN, !digitalRead(LED_IN));
}
digitalWrite(LED_IN, 1);
Serial.println("WIFI: Connected");
Serial.print("WIFI: IP address=");
Serial.println(WiFi.localIP());
}
//////////////////////////////
//Switch
/////////////////////////////////
void Switch(bool currentState)
{
// If the switch/button changed, due to noise or pressing:
if (cfg_sw.currentState != cfg_sw.lastFlickerableState) {
// reset the debouncing timer
cfg_sw.lastDebounceTime = millis();
// save the the last flickerable state
cfg_sw.lastFlickerableState = cfg_sw.currentState;
}
if ((millis() - cfg_sw.lastDebounceTime) > cfg_sw.Debouncetime) {
// if the button state has changed:
if (cfg_sw.lastSteadyState == HIGH && cfg_sw.currentState == LOW)
{
digitalWrite(LED_SW, 1);
strncpy(starttime, time_output, sizeof(starttime));
// Serial.println(starttime);
Gripper();
strncpy(endtime, time_output, sizeof(endtime));
sendData();
// Serial.println(endtime);
}
else if (cfg_sw.lastSteadyState == LOW && cfg_sw.currentState == HIGH)
{
digitalWrite(LED_SW, 0);
}
// save the the last steady state
cfg_sw.lastSteadyState = cfg_sw.currentState;
}
}
//////////////////////////////
//Gripper
/////////////////////////////
void Gripper()
{
for (int pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15); // waits 15ms to reach the position
}
for (int pos = 0; pos <= 90; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15); // waits 15ms to reach the position
}
}
/////////////////////////////////////////////
// SENSOR
/////////////////////////////////////////////
void sensor_setup() {
sensorTimer = xTimerCreate("sensorTimer", pdMS_TO_TICKS(1000), pdTRUE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(sensor_update));
xTimerStart(sensorTimer, 0);
}
void sensor_update() {
sensor.WeightScale = random(60, 80);
}
///////////////////////
///Timezone
///////////////////
TimerHandle_t TimezoneTimer;
void Timezone_setup() {
// Serial.println("Timezone: SETUP");
configTime(0, 0, cfg_TZ.NTP_SERVER);
setenv("TZ", cfg_TZ.TZ_INFO, 1);
TimezoneTimer = xTimerCreate("TimezoneTimer", pdMS_TO_TICKS(1000), pdTRUE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(getNTPtime));
xTimerStart(TimezoneTimer, 0);
}
void getNTPtime() {
uint32_t start = millis();
do {
time(&now);
localtime_r(&now, &timeinfo);
// Serial.print(".");
delay(10);
//sec=10
} while (((millis() - start) <= (1000 * 1)) && (timeinfo.tm_year < (2016 - 1900)));
// Serial.print("now "); Serial.println(now);
strftime(time_output, 30, "%d-%m-%y+%H:%M:%S3", localtime(&now));
}
void sendData() {
String url = "https://script.google.com/macros/s/" + GAS_ID + "/exec?starttime=" + starttime + "&endtime=" + endtime + "&weightscale=" + sensor.WeightScale;
Serial.print("requesting URL: ");
Serial.print("POST data to spreadsheet:");
Serial.println(url);
// http.begin(url); //Specify the URL and certificate
// int httpCode = http.GET();
// http.end();
// Serial.println(": done " + httpCode);
HTTPClient http;
http.begin(url,root_ca);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
}
void setup() {
Serial.begin(115200);
pinMode(LED_IN, OUTPUT);
pinMode(LED_SW, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
myservo.attach(15);
myservo.write(0);
sensor_setup();
wifi_setup();
Timezone_setup();
}
void loop() {
cfg_sw.currentState = digitalRead(BUTTON_PIN);
Switch(cfg_sw.currentState);
}