1

my friend made a google script, and gave me this link: https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec

If you click you will se "52" in your browser. Now I want to create a script in Arduino that can receive this value and use it in order to adjust the amount of light of a LED (code below is without LED part).

I wrote a code:

#include <ESP8266WiFi.h>       
#include <DNSServer.h>
#include <ESP8266WebServer.h>

#include <WiFiClientSecure.h>

const char* ssid = "TabletS3";
const char* password = "123456789";


//https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec
WiFiServer server(80);
//String GAS_ID = "AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1";  // Replace by your GAS service id
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6";
WiFiClientSecure client;


void setup() {

 Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } 


}

void loop() {
  String url = "https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec";


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               //"User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

               Serial.println("Response:");
              while (client.connected())
   {    
  String  line = client.readStringUntil('\r');
    Serial.print(line);
    client.flush();
   }
delay(10000);
}

but I can't read value "52" on Serial Monitor. Where is the mistake?

Thanks in advance, RR

After your insight I tried this:

#include <ESP8266WiFi.h>       
//#include <DNSServer.h>
//#include <ESP8266WebServer.h>

//#include <WiFiClientSecure.h>

const char* ssid = "TabletS3";
const char* password = "123456789";


//https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec
//WiFiServer server(80);
//String GAS_ID = "AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1";  // Replace by your GAS service id
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "46 B2 C3 44 9C 59 09 8B 01 B6 F8 BD 4C FB 00 74 91 2F EF F6";
WiFiClientSecure client;


void setup() {

 Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } 


}

void loop() {
  String url = "https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec";


  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
delay(10000);
}

But doesn't catch nothing

Kos
  • 4,890
  • 9
  • 38
  • 42
TroRi
  • 11
  • 2
  • It's not possible to open that link, please provide the Apps Script code as part of your question – alberto vielma Jan 23 '20 at 11:45
  • The link is private and needs a Google account. Ask your friend to publish the web app as "Anyone, **even anonymous**" can access. Else you'd need your arduino to handle oauth and send authorization headers. – TheMaster Jan 23 '20 at 12:24
  • you didn't call `client.connect`. after GET goes only the url without host and port. remove all includes except of `#include `. delete `WiFiServer server(80);` – Juraj Jan 23 '20 at 12:26
  • @albertovielma, excuse me, the link is now working. Sorry, my mistake – TroRi Jan 23 '20 at 15:56
  • @Juraj, as you see I tried your suggestion (I added the code in the main question) but doesn't work. Any idea? – TroRi Jan 24 '20 at 07:32
  • there is still no client.connect() and the GET has the full URL – Juraj Jan 24 '20 at 07:59
  • @Juraj, could you please post the correct code? If I insert in the loop client.connect(); there is this error: 'error: no matching function for call to 'WiFiClientSecure::connect().' – TroRi Jan 24 '20 at 09:56
  • https://www.arduino.cc/en/Reference/WiFiClientConnect – Juraj Jan 24 '20 at 10:09
  • the url after GET should not contain protocol, server and port – Juraj Jan 25 '20 at 09:32
  • @Juraj, String url = "https://script.google.com/macros/s/AKfycbxxWwU59sp_rF9C6phVIFSdLF6IIM83hFRWUrrJyVI8TdY6F-t1/exec"; if (client.connect(url2,80)) { Serial.println("Connected"); client.println(String("GET ") + url); line = client.readString(); Serial.println(line); } Same answer – TroRi Jan 25 '20 at 16:38
  • the url after GET should not contain protocol, **server** and port – Juraj Jan 25 '20 at 16:42
  • @Juraj, can you please write the url that I have to put after GET? Excuse me but I'm struggling. Thanks – TroRi Jan 25 '20 at 21:07
  • `script.google.com` is server. the location on server starts with `/` after server name. so `/macros/s/...` is a location on server. you want to access this location with GET – Juraj Jan 26 '20 at 07:01
  • @Juraj, thanks, I have tried without success. A colleague told me that because I'm using https the problems could be others, for example fingerprints and so on...do you think his suggestions are right? – TroRi Jan 27 '20 at 10:36
  • I think there are "redirects link" but how can I manage them? – TroRi May 05 '20 at 10:20

0 Answers0