esp to google spreadsheets

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

char* ssid = "xxxxx";
char* password = "xxxxx";



const char* host = "script.google.com";


const int httpsPort = 443;




WiFiClientSecure client;

void setup()
{
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED)
     {  delay(1000);
  }
  client.setInsecure();
}
    

void loop(){
  if(Serial.available()) {
      String theTime = Serial.readString();
      Serial.println(theTime);
      if( WiFi.status()== WL_CONNECTED ){
        sendTime(theTime);
      } 
  }
}
void sendTime(String s)
{      
// "https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxx/exec?addData=";   
  
  if (!client.connect(host,httpsPort)){
    Serial.println("connection failed");
    return;
  }
  String url = "/macros/s/AKfycbwCXht9EiXpg32X3N9epaTIIJht2hqc1v0leczfOxM7oG2UYmy_rFs8/exec?addData=" + s;
 Serial.print("requesting URL: ");
  Serial.println(url);

  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");
}

can some one tell me why i am getting “Error 400 (Bad Request)!!1” error and how to possible fix it, im trying to add data to a google spread sheet and not really sure whats going on with this error

I use the examples in this OTA Updates — ESP8266 Arduino Core 3.0.0-7-gd7d03f9a documentation

Start simple - try with this code to see what you get back (console @115200 bauds)
typed here from your own code, fully untested

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "xxx";
const char* password = "xxx";
const char* host = "script.google.com";
const int httpsPort = 443;
WiFiClientSecure client;

const char * request = "GET /macros/s/AKfycbwCXht9EiXpg32X3N9epaTIIJht2hqc1v0leczfOxM7oG2UYmy_rFs8/exec?addData=42  HTTP/1.1";

void setup() {
  Serial.begin(115200);
  while (!Serial);

  Serial.print("\n\nConnecting to WiFi ...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(100);
  Serial.println("...Connected");

  client.setInsecure();
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  client.println(request);
  client.print("Host: "); client.println(host);
  client.println("User-Agent: ESP8266");
  client.println("Connection: close\r\n");

  Serial.println("request sent");

  while (client.connected()) {
    while (client.available()) Serial.write(client.read());
  }
  client.stop();
}

void loop() {}

@j-m-l the code you sent me worked, what do you think the problem with how i had my code originally is and how would i be able to go about fixing it

i think it might be a problem with putting the string in the url in might need char but im not sure any idea on how to fix this?
or is there a way to try converting string to char so i could try it

I’ve not checked in details - reading on my iPhone - may be you acquire or print the wrong string

Also look at how I waited for the answer

 while (client.connected()) {
    while (client.available()) Serial.write(client.read());
  }
  client.stop();

compared to your code

I would suggest to study Serial Input Basics to handle serial input and then you don’t have any String. (But string could work). The same tcehnic could be applied to listen to the answer from your web request

@nick_d12, I am having the same problem.

Were you able to find a solution?

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