However when I try and use the following code it does not! Does anyone knbow why this is not working. TIA
#include "ESP8266WiFi.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)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
/* -------------------------------------------------------------------------- */
void setup() {
/* -------------------------------------------------------------------------- */
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to WiFi network:
Serial.printf("\nConnecting to: %s \n", ssid);
WiFi.mode(WIFI_STA);
status = WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.printf("\n");
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
printWifiStatus();
}
/* -------------------------------------------------------------------------- */
void loop() {
/* -------------------------------------------------------------------------- */
char server[] = "script.google.com"; // name address for Google (using DNS)
char buffer[700];
//Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
sprintf(buffer, "");
sprintf(buffer, "%sPOST /macros/s/AKfycbzOeD77Fj_qYMP9yIXcF-hLWihcbQoMi6lU5ASjesiKIFurpjD860C-6VANqSGwuaoX/exec HTTP/1.1\n", buffer);
sprintf(buffer, "%sHost: script.google.com\n", buffer);
sprintf(buffer, "%sContent-Type: application/json\n", buffer);
sprintf(buffer, "%sConnection: close\n", buffer);
sprintf(buffer, "%sContent-Length: 36\n", buffer);
sprintf(buffer, "%s{\"value1\": 10.569, \"value2\": 34.257}\n", buffer);
Serial.printf("Sending:\n%s", buffer);
client.println(buffer);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
delay(2000);
}
/* -------------------------------------------------------------------------- */
void printWifiStatus() {
/* -------------------------------------------------------------------------- */
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board'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");
}
Google Scripts require HTTPS, not plain HTTP on port 80. (Postman works because it uses HTTPS automatically)
➜ you need to use WiFiClientSecure and port 443
also HTTP requires each header line to end with CRLF , Using only \n
can cause the server to ignore or reject the request.
➜ You should use \r\n
Also the headers must be followed by a blank line before the body so you should have a double \r\n after the content length and before the values
You should not use sprintf() the way you do, why don't you just either print directly to the client without creating a huge buffer on your side or use strlcpy() if you really want to have the buffer?
client.println("POST /macros/s/XXXXXX/exec HTTP/1.1");
client.println("Host: script.google.com");
client.println("Content-Type: application/json");
client.println("Content-Length: 36");
client.println("Connection: close");
client.println(); // don't need empty string here -- note there is no `print()` with no args
client.print(R"({"value1": 10.569, "value2": 34.257})"); // don't need "\r\n" after content
(The raw string makes the JSON literal a little less tedious. And if you're not going to have the compiler count the Content-Length for you, it's also a little easier to spot-check that manual length is correct.)
Yes, I hesitated putting that and thought it would be clearer having the CRLF in the string as per the point I was trying to make. But you are right, that’s the better way.
Thanks for your help but I have a similar but different question. I have a similar project but using an arduino R4 so am having to use a different library.
It sometimes connects but then after a few trys will not connect. Any ideas what I am doing wrong?
The code is below:
// This example uses an Arduino Uno together with
// a WiFi Shield to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt
#include <WiFiS3.h>
#include <WiFiSSLClient.h>
#include "arduino_secrets.h"
//Google webhooks
char host[] = "script.google.com";
int port = 443;
char url[] = "/macros/s/####/exec";
///////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)
int status = WL_IDLE_STATUS; // the WiFi radio's status
//Calibration Data
const float V_REF = 5.0; // Analog reference voltage (e.g., 5V or 3.3V)
const float R_BITS = 10.0; // ADC resolution (bits)
const float ADC_STEPS = (1 << int(R_BITS)) - 1; // Number of steps (2^R_BITS - 1)
const float ZERO_OFFSET = 2.515; // Zero Offset of Ammeter
const float VperA = .072; //Volts per Amp on Ammeter
const int VoltagePin = A0; // Voltage measurement on analogue Pin 1
const int CurrentPin = A1; // Current measurement on analogue Pin 0
long lastMillis = 0;
WiFiSSLClient client;
float readADC(int pin) //Returns the voltage of the analouge port defined by pin
{
unsigned int x = 0;
float ADCValue = 0.0, Samples = 0.0, AvgADC = 0.0, Volts = 0.0;
for (int x = 0; x < 150; x++) { //Get 150 samples
ADCValue = analogRead(pin); //Read current sensor values
Samples = Samples + ADCValue; //Add samples together
delay(3); // let ADC settle before next sample 3ms
}
AvgADC = Samples / 150.0; //Taking Average of Samples and turn into voltage
Volts = V_REF * AvgADC / ADC_STEPS;
return Volts;
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
char macstr[17];
mac2str(macstr, mac);
Serial.print("Device MAC Address: ");
Serial.println(macstr);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
char macstr[17];
mac2str(macstr, bssid);
Serial.println(macstr);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void mac2str(char* macstr, byte mac[6]) {
sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(A0, INPUT);
pinMode(A1, INPUT);
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 2 seconds for connection:
delay(2000);
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
printCurrentNet();
printWifiData();
//Set certificate
client.setCACert(root_ca);
}
void loop() {
float voltage, current;
char buffer[36];
if (millis() - lastMillis > 5 * 1000) {
voltage = 25 * readADC(VoltagePin) / 5;
current = (readADC(CurrentPin) - ZERO_OFFSET) / VperA;
lastMillis = millis();
//Output to google sheets
if (client.connect(host, port)) {
Serial.println("connected to server");
client.print("POST ");
client.print(url);
client.println("HTTP/1.1");
client.print("Host: ");
client.print(host);
client.println("");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println("Content-Length: 36");
client.println("");
sprintf(buffer, "");
sprintf(buffer, "{\"voltage\":%6.2f, \"current\":%6.2f}", voltage, current);
client.println(buffer);
} else {
Serial.println("Client not connected.");
client.stop();
}
}
}
I have tried that and still no connection. see below:
// This example uses an Arduino Uno together with
// a WiFi Shield to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt
#include <WiFiS3.h>
#include <WiFiSSLClient.h>
#include "arduino_secrets.h"
//Google webhooks
char host[] = "script.google.com";
int port = 443;
char url[] = "/macros/s/AKfycbxc_m555AsnBqiVJxi3SU4xox07Ig2ZR6xPzFuuF4IML1Qi_e3-U192oXbUrFTo7L06/exec";
///////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)
int status = WL_IDLE_STATUS; // the WiFi radio's status
//Calibration Data
const float V_REF = 5.0; // Analog reference voltage (e.g., 5V or 3.3V)
const float R_BITS = 10.0; // ADC resolution (bits)
const float ADC_STEPS = (1 << int(R_BITS)) - 1; // Number of steps (2^R_BITS - 1)
const float ZERO_OFFSET = 2.515; // Zero Offset of Ammeter
const float VperA = .072; //Volts per Amp on Ammeter
const int VoltagePin = A0; // Voltage measurement on analogue Pin 1
const int CurrentPin = A1; // Current measurement on analogue Pin 0
long lastMillis = 0;
WiFiSSLClient client;
float readADC(int pin) //Returns the voltage of the analouge port defined by pin
{
unsigned int x = 0;
float ADCValue = 0.0, Samples = 0.0, AvgADC = 0.0, Volts = 0.0;
for (int x = 0; x < 150; x++) { //Get 150 samples
ADCValue = analogRead(pin); //Read current sensor values
Samples = Samples + ADCValue; //Add samples together
delay(3); // let ADC settle before next sample 3ms
}
AvgADC = Samples / 150.0; //Taking Average of Samples and turn into voltage
Volts = V_REF * AvgADC / ADC_STEPS;
return Volts;
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
char macstr[17];
mac2str(macstr, mac);
Serial.print("Device MAC Address: ");
Serial.println(macstr);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
char macstr[17];
mac2str(macstr, bssid);
Serial.println(macstr);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void mac2str(char* macstr, byte mac[6]) {
sprintf(macstr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(A0, INPUT);
pinMode(A1, INPUT);
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 2 seconds for connection:
delay(2000);
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
printCurrentNet();
printWifiData();
//Set certificate
client.setCACert(root_ca);
}
/* just wrap the received data up to 80 columns in the serial print*/
/* -------------------------------------------------------------------------- */
void read_response() {
/* -------------------------------------------------------------------------- */
uint32_t received_data_num = 0;
while (client.available()) {
/* actual data reception */
char c = client.read();
/* print data to serial port */
Serial.print(c);
/* wrap data to 80 columns*/
received_data_num++;
if (received_data_num % 80 == 0) {
Serial.println();
}
}
}
void loop() {
float voltage, current;
char buffer[50];
if (millis() - lastMillis > 5 * 1000) {
voltage = 25 * readADC(VoltagePin) / 5;
current = (readADC(CurrentPin) - ZERO_OFFSET) / VperA;
lastMillis = millis();
//Output to google sheets
if (client.connect(host, port)) {
Serial.println("connected to server");
client.print("POST ");
client.print(url);
client.println("HTTP/1.0");
client.print("Host: ");
client.print(host);
client.println("");
client.println("Content-Type: application/json");
client.println("Connection: close");
snprintf(buffer, 50, "");
snprintf(buffer, 50, "{\"voltage\":%6.2f, \"current\":%6.2f}", voltage, current);
client.print("Content-Length: ");
client.println(strlen(buffer));
client.println("");
client.println(buffer);
read_response();
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
}
}