Hi, anyone has encountered this while reading data from google sheet. My code compiles and runs OK but does not return any value from google sheet. I want to read a cell in google sheet and get the value (message) to ESP8266 to be used in my LED board project. But the respond returns nothing. My code will read the value in cell A2 every 5 seconds and returns the value as message.
This is my code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char * ssid = "xxxxxxx"; // Wifi Username
const char * password = "yyyyyyyy"; // Wifi Password
String GOOGLE_SCRIPT_ID = "AKfycbxw0eRx6qgPUBqW6VcD4BTiYbVC-2GEev_43kiiQHQrL9PYFUOhQClTfq7KXTf3Afsitg";
String message;
WiFiClientSecure client;
void setup()
{
Serial.begin(19200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("Wifi Connected: ");
Serial.println("OK");
}
void loop()
{
read_google_sheet();
delay(5000);
}
void read_google_sheet(void)
{
//-----------------------------------------------------------------------------------
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?read=A2";
Serial.println("Reading Data From Google Sheet.....");
Serial.print("URL: ");Serial.println(url);
https.begin(*client, url.c_str());
//-----------------------------------------------------------------------------------
//Removes the error "302 Moved Temporarily Error"
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
//-----------------------------------------------------------------------------------
//Get the returning HTTP status code
int httpCode = https.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
//-----------------------------------------------------------------------------------
if (httpCode <= 0) {
Serial.println("Error on HTTP request");
https.end();
return;
}
//-----------------------------------------------------------------------------------
//reading data comming from Google Sheet
String payload = https.getString();
Serial.println("Payload: " + payload);
//-----------------------------------------------------------------------------------
if (httpCode == 200)
message = payload;
Serial.println("Message: "+ message);
Serial.println("--------------------");
//-------------------------------------------------------------------------------------
https.end();
}
The serial monitor output this:
.......Wifi Connected: OK
Reading Data From Google Sheet.....
URL: https://script.google.com/macros/s/AKfycbxw0eRx6qgPUBqW6VcD4BTiYbVC-2GEev_43kiiQHQrL9PYFUOhQClTfq7KXTf3Afsitg/exec?read=A2
HTTP Status Code: 200
Message:
--------------------
Reading Data From Google Sheet.....
URL: https://script.google.com/macros/s/AKfycbxw0eRx6qgPUBqW6VcD4BTiYbVC-2GEev_43kiiQHQrL9PYFUOhQClTfq7KXTf3Afsitg/exec?read=A2
HTTP Status Code: 200
Message:
--------------------
If I copy the URL and paste it in a browser it shows the value in cell A2. I don't know what is wrong with my code. Your help is much appreciated. Thanks.