#include "WiFi.h"
#include <HTTPClient.h>
// Defining LED PINs on the ESP32 Board.
#define On_Board_LED_PIN 2
String User1, User2;
int USER_QTD = 0;
int* userPasswords; // Pointer for dynamic array
char (*userNames)[11]; // Pointer for dynamic array of char arrays
//----------------------------------------SSID and PASSWORD of your WiFi network.
const char* ssid = "Rodrigo"; //--> Your wifi name
const char* password = "suasenha"; //--> Your wifi password
//----------------------------------------
// Google script Web_App_URL.
String Web_App_URL = "https://script.google.com/macros/s/AKfycbwwqpcF4nXgP5THnIzCL-xYf1qIqDteIzXCJUrE9MjWogT0vyeHwUfCy5IqSlGHVpl3Ug/exec";
//________________________________________________________________________________getValue()
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
//________________________________________________________________________________
//________________________________________________________________________________VOID SETUP()
void setup() {
Serial.begin(115200);
Serial.println();
delay(1000);
pinMode(On_Board_LED_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.begin(ssid, password);
int connecting_process_timed_out = 20; //--> 20 = 20 seconds.
connecting_process_timed_out = connecting_process_timed_out * 2;
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(On_Board_LED_PIN, HIGH);
delay(500);
digitalWrite(On_Board_LED_PIN, LOW);
delay(500);
if (connecting_process_timed_out > 0) connecting_process_timed_out--;
if (connecting_process_timed_out == 0) {
Serial.println("connect failed");
delay(1000);
Serial.println("Esp Restart");
delay(1000);
ESP.restart();
}
}
digitalWrite(On_Board_LED_PIN, LOW);
Serial.println("WiFi connected ok");
delay(1000);
Serial.println("Get Data from Google Sheets");
}
// Function to update USER_QTD and resize arrays
void updateUserQty(String data) {
int newUserQty = getValue(data, ',', 0).toInt();
if (newUserQty != USER_QTD) {
USER_QTD = newUserQty;
// Resize arrays
if (userPasswords != nullptr) {
free(userPasswords); // Free previous memory
}
userPasswords = (int*)malloc(USER_QTD * sizeof(int)); // Allocate new memory for passwords
if (userNames != nullptr) {
free(userNames); // Free previous memory
}
userNames = (char(*)[11])malloc(USER_QTD * sizeof(char[11])); // Allocate new memory for names
Serial.print("USER_QTD updated to: ");
Serial.println(USER_QTD);
}
}
// Function to get integer values from even indices (greater than zero)
void getValuesInt(String data, int* array, int& count) {
int index = 0;
count = 0;
while (true) {
String value = getValue(data, ',', index);
if (value == "") break; // No more values to process
if (index % 2 == 0 && index > 0) {
if (count < USER_QTD) {
array[count] = value.toInt(); // Store in array
count++;
}
}
index++;
}
}
// Function to get character values from odd indices
void getValuesChar(String data, char array[][11], int& count) {
int index = 0;
count = 0;
while (true) {
String value = getValue(data, ',', index);
if (value == "") break; // No more values to process
if (index % 2 != 0) {
if (count < USER_QTD) {
value.toCharArray(array[count++], 11); // Store in array
}
}
index++;
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(On_Board_LED_PIN, HIGH);
String Read_Data_URL = Web_App_URL + "?sts=read";
Serial.println();
Serial.println("-------------");
Serial.println("Read data from Google Spreadsheet...");
Serial.print("URL : ");
Serial.println(Read_Data_URL);
HTTPClient http;
http.begin(Read_Data_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);
updateUserQty(payload);
int passwordCount;
getValuesInt(payload, userPasswords, passwordCount);
int nameCount;
getValuesChar(payload, userNames, nameCount);
Serial.println("User Names:");
for (int i = 0; i < nameCount; i++) {
Serial.println(userNames[i]);
}
Serial.println("Passwords:");
for (int i = 0; i < passwordCount; i++) {
Serial.println(userPasswords[i]);
}
} else {
Serial.print(httpCode); Serial.println("Erro");
}
http.end();
digitalWrite(On_Board_LED_PIN, LOW);
Serial.println("-------------");
Serial.println(httpCode == 200);
}
delay(10000);
}