/* Project : Read Google Spread Sheet Data from ESP32 */
//https://script.google.com/macros/s/AKfycbyLQzfYW6yUjlzeHD1JlEKj_ch8WVlDnp1XPaqDqu1Ph91BXP8sXIcRS6qzWG9gUXcnLA/exec
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String GOOGLE_SCRIPT_ID = "AKfycbyk7BHs5Uj8T3cPix0DaQcHp_REJGWngOw52V4wPRvlJcvFU7egCFL8OGltR72PS77MOg";
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int analogPin = 34;
const int sendInterval = 5000;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Connecting to WiFi...");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
display.clearDisplay();
display.println("WiFi connected!");
display.display();
}
void loop() {
sendToGoogleSheet();
readFromGoogleSheet();
delay(sendInterval);
}
void sendToGoogleSheet() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
int analogValue = analogRead(analogPin);
String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?val1=" + String(analogValue);
http.begin(url);
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Data sent successfully: " + payload);
display.clearDisplay();
display.setCursor(0,0);
display.println("Sent: " + String(analogValue));
//display.println("Response: " + payload);
display.display();
} else {
Serial.println("Error sending data: " + String(httpResponseCode));
}
http.end();
}
}
void readFromGoogleSheet() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?read";
http.begin(url);
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Data received: " + payload);
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println("Gsheet:");
display.print(payload);
display.display();
} else {
Serial.println("Error receiving data: " + String(httpResponseCode));
}
http.end();
}
}
/*
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//Things to change
const char * ssid = "Wokwi-GUEST";
const char * password = "";
String GOOGLE_SCRIPT_ID = "AKfycbyLQzfYW6yUjlzeHD1JlEKj_ch8WVlDnp1XPaqDqu1Ph91BXP8sXIcRS6qzWG9gUXcnLA";
const int sendInterval = 5000;
#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);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
const int analogPin = 34;
void testdrawstyles(String str) {
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
// display.println(F("GSheet C2:"));
// display.setCursor(0,1); // Start at top-left corner
display.println(F(str.c_str()));
display.display();
delay(2000);
}
void setup() {
Serial.begin(115200);
delay(10);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
//display.drawBitmap(0, 0, image_data_Image, 128, 64, 1);
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Started");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Ready to go");
//testdrawstyles();
}
void loop() {
spreadsheet_comm();
//delay(sendInterval);
delay(3000);
// Save every 10 seconds. Adjust to frequency desired. This is a blocking delay for simplicity, switch
// to millis() mechanisim or simular if need to do other processing between writes.
}
void spreadsheet_comm(void) {
HTTPClient http;
String url="https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?read";
// Serial.print(url);
Serial.print("Making a request");
http.begin(url.c_str()); //Specify the URL and certificate
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
String payload;
if (httpCode > 0) { //Check for the returning code
payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
testdrawstyles(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
int analogValue = analogRead(analogPin);
if (WiFi.status() == WL_CONNECTED) {
String Send_Data_URL ="https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?";
Send_Data_URL += "val1=" + (String)analogValue;
//Send_Data_URL += "&val2=" + (String)analogRead(VAL2_POT_PIN);
//Send_Data_URL += "&val3=" + String(value);
Serial.println();
Serial.println("-------------");
Serial.println("Send data to Google Spreadsheet...");
Serial.print("URL : ");
Serial.println(Send_Data_URL);
// Writing data to Google Sheets.
// HTTP GET Request.
http.begin(Send_Data_URL.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
// Gets the HTTP status code.
int httpCode = http.GET();
Serial.print("HTTP Status Code : ");
Serial.println(httpCode);
// Getting response from google sheets.
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Response: " + payload);
}
//digitalWrite(LED_PIN, LOW);
Serial.println("-------------");
http.end();
}
}*/