How to connect Google script to node mcu


Hello guys, i need help, i have a task from my university to make a automatic feed from a bot, so i have to get input from bot telegram, and the input i put at variable, but i confuse to transfer variable/connect itu to node mcu, i found a reference but it just code in arduino, in my case i need to get the code from the google script, it refers to the picture above, please help

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

You need to create a function, which is available by the node MCU.
In GoogleScript, it is called function doGet(e) for a GET and so on.
e can be a (few) parameter(s).
Returning a string value can be like that
return ContentService.createTextOutput("hello world");

Now you have to release this script to your Google-Login or public. Then you receive a key and a URl, which contains this key.

Calling that function is a bit complicated. This can be archived by:

#include <WiFiClientSecure.h>
#define SCRIPT_ID             "SeCr3tK3y"
#define PARAM                 "?param1=1337"
#define HTTPS_PORT            443
#define HOST                  "script.google.com"

String GetGoogleScriptResult() {
  WiFiClientSecure client;
  String movedURL;
SendGet(client, "https://" + String(HOST) + "/macros/s/" + ScriptId + "/exec" + String(PARAM));
ReadHeader(client, movedURL);
 client.stop();
SendGet(client, movedURL);
return client.readStringUntil('\r');
}
void SendGet(WiFiClientSecure &client, String adress) {
  client.println("GET " + adress);
  client.println("Host: " + String(HOST));
  client.println("Connection: close");
  client.println("Pragma: no-cache");
  client.println("Cache-Control: no-cache");
  client.println("User-Agent: ESP32");
  client.println("Accept: application/json");
  client.println();
}

But there is a redirection, which has to be followed. I did it quite dirty:

void ReadHeader(WiFiClientSecure &client, String &movedURL) {
  uint64_t startMillis = millis();
  String line;
  while (client.connected()) {
    if (millis() - startMillis  > 5000)
      break;
    line = client.readStringUntil('\n');
    if (line == "\r")
      break;
    if (line.indexOf("Location") >= 0) {  // Rerouting
      movedURL = line.substring(line.indexOf(":") + 2 ) ;
    }
  }
}

If this function returnes a value in "movedURL", then you have to SendGet() it again with that URL.
Just did that as a sample in the function above. You should introduce some checks and cleanup the client.

Okay, so i merge that coding, but it has an error for defining GetGoogleScriptResult()
redefinition of 'String GetGoogleScripResult()'
and i still dont get it which part that i get my variable from that google script? And in PARAM, what should i fill? Thank you so much, your helping me a lot

You just don´t need that param. It´s optional.
Use it, if you want to transfer data to the script.

The functions were copied and shortened to the important parts and you need to understand it, to modify it to your requirements.

The client.readStringUntil() gives you everything back from the webrequest. The first line will be something like "200 - OK" or some http-status. Anything else will be the return value from your script function doGet().

Mine looks like that:

function doGet(e){
  var param = "";
  if(e != null && e.parameter != null) 
    param = e.parameter.param1;
  if(param == "dateonly")
    return ContentService.createTextOutput(GetDayLong());
  var date = GetDayLong();
  var weather = GetWeather();
  var events = GetEvents();

  return ContentService.createTextOutput(date+ weather + events);
}

The methods just return some text (date, weather and some appointments).

Mostly, an error about a single function is somwhere above?! A missing semicolon?!
I just return the string in GetGoogleScriptResult:

String line = "";
  while (client.connected()) {
    if (client.available()) {
      line += client.readStringUntil('\r');
    }
  }
  client.stop();
  return line;

Not much to mess up there.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.