Hello everyone,
i've been trying to send a GET request to fill a Google Sheets directly by WIFI using WIFI101 but I can't find the error in my code or in the way i'm doing it.
I used a tutorial on youtube to do that (sorry can't find it at the moment), it was working on a MKR1010 but then I had to convert it to MKR1000 (using Wifi101 instead of Wifinina), I managed to solve some problems but not the last one.
The typical Wifi101 example code for sending a Google request is working.
But the code below does not work (I've tried port = 443 and 80, i've been on forum and posts, and tried things without sucess until now):
#include <SPI.h>
#include <WiFi101.h>
#include "Arduino.h"
#include <Wire.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) // your network key index number
int status = WL_IDLE_STATUS; // the WiFi radio's status
const char host[] = "script.google.com";
const int httpsPort = 80;
WiFiClient client;
String GAS_ID = SECRET_GAS;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// check for the WiFi module:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// once you are connected :
Serial.print("You're connected to the network");
printWiFiStatus();
}
void loop() {
uint16_t co2 = 800;
float temperature = 25;
float humidity = 100;
String Temp = "Temperature : " + String(temperature) + " °C";
String Humi = "Humidity : " + String(humidity) + " %";
String strco2 = "CO2 : " + String(co2) + " ppm";
Serial.println(Temp);
Serial.println(Humi);
Serial.println(strco2);
sendData(temperature, humidity, co2);
delay(10000);
}
void sendData(float tem, float hum, uint16_t coo) {
Serial.println("==========");
Serial.print("connecting to ");
Serial.println(host);
//----------------------------------------Connect to Google host
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//----------------------------------------Processing data and sending data
String string_temperature = String(tem);
// String string_temperature = String(tem, DEC);
String string_humidity = String(hum);
String string_co2 = String(coo);
String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity + "&co2=" + string_co2;
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
//----------------------------------------
//----------------------------------------Checking whether the data was sent successfully or not
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
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.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
//----------------------------------------
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
}
Here is what I get from the client.read that I've added to have some info
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Sun, 22 Jan 2023 07:57:54 GMT
Location: https://script.google.com/macros/s/XXX
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-XSS-Protection: 1; mode=block
Server: GSE
Accept-Ranges: none
Vary: Accept-Encoding
Connection: close
Transfer-Encoding: chunked
headers received
esp8266/Arduino CI has failed
reply was : 15a
closing connection