Hello,
I have got the following situation that I can't find a solution to.
I am using Arduino Uno rev3 with ESP-01 as a wifi shield and trying to send live sensor data direct to google sheets without third-party iot cloud.
There are plenty of tutorials on how to do the above but with ESP32 or ESP8266 integrated boards but not any tutors for my case using ESP-01 as a wifi shield.
So this is the wifi library I am using it works and connects to internet:
//WiFiEsp example
#include <WiFiEsp.h>
#ifndef HAVE_HWSERIAL
#endif
// Emulate Serial on pins 5/6
#include "SoftwareSerial.h"
SoftwareSerial EspSerial(5, 6); // RX, TX
#define ESP8266_BAUD 38400
// Enter network credentials:
char ssid[] = "*********"; // your network SSID (name)
char pass[] = "*********"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
EspSerial.begin(ESP8266_BAUD);
// initialize ESP module
WiFi.init(&EspSerial);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true)
;
}
// 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);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
}
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");
}
07:52:14.405 -> [WiFiEsp] Initializing ESP module
07:52:19.920 -> [WiFiEsp] >>> TIMEOUT >>>
07:52:19.920 -> [WiFiEsp] Initilization successful - 1.5.4
07:52:19.920 -> Attempting to connect to WPA SSID: BTHub6-Q73T
07:52:26.914 -> [WiFiEsp] Connected to BTHub6-Q73T
07:52:26.955 -> Connected to wifi
07:52:26.955 -> SSID: BTHub6-Q73T
07:52:26.989 -> IP Address: 192.168.1.205
07:52:26.989 -> signal strength (RSSI):-522 dBm
And this is the second code that I want to merge with the above. I have done the google script part already.
// Example Arduino/ESP8266 code to upload data to Google Sheets
// Follow setup instructions found here:
// https://github.com/StorageB/Google-Sheets-Logging
// reddit: u/StorageB107
// email: StorageUnitB@gmail.com
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
// Enter network credentials:
const char* ssid = "*********";
const char* password = "*********";
// Enter Google Script Deployment ID:
const char *GScriptId = "*****************";
// Enter command (insert_row or append_row) and your Google Sheets sheet name (default is Sheet1):
String payload_base = "{\"command\": \"insert_row\", \"sheet_name\": \"Sheet1\", \"values\": ";
String payload = "";
// Google Sheets setup (do not edit)
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "";
String url = String("/macros/s/") + GScriptId + "/exec";
HTTPSRedirect* client = nullptr;
// Declare variables that will be published to Google Sheets
int value0 = 0;
int value1 = 0;
int value2 = 0;
void setup() {
Serial.begin(9600);
delay(10);
Serial.println('\n');
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
Serial.print("Connecting to ");
Serial.println(host);
// Try to connect for a maximum of 5 times
bool flag = false;
for (int i=0; i<5; i++){
int retval = client->connect(host, httpsPort);
if (retval == 1){
flag = true;
Serial.println("Connected");
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag){
Serial.print("Could not connect to server: ");
Serial.println(host);
return;
}
delete client; // delete HTTPSRedirect object
client = nullptr; // delete HTTPSRedirect object
}
void loop() {
// create some fake data to publish
value0 ++;
value1 = random(0,1000);
value2 = random(0,100000);
static bool flag = false;
if (!flag){
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
}
if (client != nullptr){
if (!client->connected()){
client->connect(host, httpsPort);
}
}
else{
Serial.println("Error creating client object!");
}
// Create json object string to send to Google Sheets
payload = payload_base + "\"" + value0 + "," + value1 + "," + value2 + "\"}";
// Publish data to Google Sheets
Serial.println("Publishing data...");
Serial.println(payload);
if(client->POST(url, host, payload)){
// do stuff here if publish was successful
}
else{
// do stuff here if publish was not successful
Serial.println("Error while connecting");
}
// a delay of several seconds is required before publishing again
delay(5000);
}