#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
char ssid[] = "Wokwi-GUEST"; // Your WiFi SSID
char pass[] = ""; // Your WiFi password
// Google Apps Script web service URL
const char* serverAddress = "script.google.com";
const int serverPort = 443; // Use port 443 for HTTPS
// Command strings for different operations
const char* nextCommand = "next";
const char* previousCommand = "previous";
const char* currentCommand = "current";
const char* allCommand = "all";
int row=1,column=1;
// Current row, column, and value variables
int currentRow=1, currentColumn=1;
int totalRows=1,totalColumns=1;
String value="";
const int MAX_ROWS = 10; // Maximum number of rows
const int MAX_COLUMNS = 10; // Maximum number of columns
String allCellValues[MAX_ROWS][MAX_COLUMNS]; // 2D array to store cell values
// Create HTTP object
HTTPClient http;
// // Initialize the WiFi client library
// WiFiClientSecure wifiClient;
// // Initialize the HttpClient
// HttpClient client = HttpClient(wifiClient, serverAddress, serverPort);
const int prevbutton = 2;
const int nextbutton = 3;
void setup() {
// Initialize serial communication
Serial.begin(115200);
pinMode(prevbutton, INPUT_PULLUP);
pinMode(nextbutton, INPUT_PULLUP);
// Initialize OLED display
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
// oled.display();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Send HTTP GET request for "next" command
if (digitalRead(nextbutton) == HIGH) {
sendRequest(nextCommand, currentRow, currentColumn);
delay(5000);
}
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
int16_t textWidth = strlen("Value:") * 6 * 2;
int16_t textHeight = 2 * 8;
int16_t x = (SCREEN_WIDTH - textWidth) / 2;
int16_t y = (SCREEN_HEIGHT - textHeight) / 2;
oled.setCursor(x, y);
oled.println("Value: 5");
oled.println(value);
oled.display();
// Delay for a while
delay(5000);
// Send HTTP GET request for "previous" command
if (digitalRead(prevbutton) == HIGH) {
sendRequest(previousCommand, currentRow, currentColumn);
delay(5000);
}
// Send HTTP GET request for "current" command
sendRequest(currentCommand, currentRow, currentColumn);
// Delay for a while
delay(5000);
// Send HTTP GET request for "all" command
sendRequest(allCommand, currentRow, currentColumn);
// Delay for a while
delay(5000);
}
void sendRequest(const char* command, int& row, int& column) {
// Construct the URL with command, row, and column parameters
String url = "https://script.google.com/macros/s/AKfycbxlZhhfkssDCkWLAvfUsxf0g_vYLuziIzH9X6dUWF2XQeeVx1ORyrVO4Qrt_eEN8WHA/exec?";
url += "command=";
url += command;
url += "&row=";
url += row;
url += "&column=";
url += column;
Serial.println(url);
// Send HTTP GET request
http.begin(url.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
// Parse JSON response
if(command!= "all"){
parseResponse(http.getString());
}else{
parseAllResponse(http.getString());
}
// Print parsed values
if(command!= "all"){
Serial.print("Value: ");
Serial.println(value);
}
else{
printAllCellValues();
}
Serial.print("Row: ");
Serial.println(currentRow);
Serial.print("Column: ");
Serial.println(currentColumn);
Serial.print("TotalRow: ");
Serial.println(totalRows);
Serial.print("TotalColumn: ");
Serial.println(totalColumns);
} else {
Serial.print("Error: HTTP status code ");
Serial.println(httpResponseCode);
}
// Disconnect
http.end();
}
void parseResponse(String responseBody) {
// Parse JSON object
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, responseBody);
if (!error) {
// Extract values from JSON object
const char* newvalue = doc["value"];
value=String(newvalue);
currentRow = doc["row"];
currentColumn = doc["column"];
totalRows = doc["totalRows"];
totalColumns = doc["totalColumns"];
} else {
Serial.print("Error: Failed to parse JSON: ");
Serial.println(error.c_str());
}
}
void parseAllResponse(String responseBody) {
// Parse JSON object
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, responseBody);
if (!error) {
// Extract values from JSON object
JsonArray valueArray = doc["value"].as<JsonArray>();
totalRows = doc["totalRows"];
totalColumns = doc["totalColumns"];
// Iterate over the 2D array and store cell values
for (int i = 0; i < totalRows && i < MAX_ROWS; i++) {
JsonArray innerArray = valueArray[i].as<JsonArray>();
for (int j = 0; j < totalColumns && j < MAX_COLUMNS; j++) {
allCellValues[i][j] = innerArray[j].as<String>();
}
}
currentRow = doc["row"];
currentColumn = doc["column"];
} else {
Serial.print("Error: Failed to parse JSON: ");
Serial.println(error.c_str());
}
}
void printAllCellValues() {
Serial.println("All Cell Values:");
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLUMNS; j++) {
Serial.print(allCellValues[i][j]);
Serial.print("\t"); // Add tab for formatting
}
Serial.println(); // Move to the next line after printing each row
}
}