#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include "WiFi.h"
#include <HTTPClient.h>
#include "time.h"
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800;
const int daylightOffset_sec = 0;
//#include <Fonts/FreeSansBold24pt7b.h>
//HardwareSerial SerialPMS(1);
//PMS pms(SerialPMS);
//PMS::DATA data;
#define RXD2 26
#define TXD2 25
// ESP32 --> Pantower PMS7003
// 26 --> RX
// 25 --> TX
// GND --> GND
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1// Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int psmAwake =0;
String GOOGLE_SCRIPT_ID = "AKfycbzwaOUCpEotwrSj7ttvZKMEVfFxYZpdHpvXAR_C-_EUmwAA4YJXIc-Sn_pH1P2qWAWIGg"; // change Gscript ID
int count = 0;
void setup()
{
Serial.begin(115200);
//SerialPMS.begin(9600, SERIAL_8N1, RXD2, TXD2);
// pms.passiveMode();
// display.setFont(&FreeSansBold24pt7b);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.println();
Serial.print("Connecting to wifi: ");
Serial.flush();
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
static bool flag = false;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
String asString(timeStringBuff);
asString.replace(" ", "-");
Serial.print("Time:");
Serial.println(asString);
String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"date=" + asString + "&sensor=" + String(count);
Serial.print("POST data to spreadsheet:");
Serial.println(urlFinal);
HTTPClient http;
http.begin(urlFinal.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
//---------------------------------------------------------------------
//getting response from google sheet
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Payload: "+payload);
}
//---------------------------------------------------------------------
http.end();
}
count++;
delay(1000);
}