• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 Not Consistently Logging to Google sheets

Q&A Forum › ESP32 Not Consistently Logging to Google sheets
0 Vote Up Vote Down
Matt asked 6 years ago

Hi Team,

Thanks for all your help so far on my ESP32 journey. I do look elsewhere for answers to my questions so I don’t take up too much of your time but you guys are the best resource out there for this ESP32 stuff.

I have some test code running a web server and logs some temperature sensor data to google sheets. This all works well sometimes but it seems to fail to connect to ifttt quite often – it logs about once every 10-20 times it tries but is inconsistent. The code tries to log every 60 seconds, I have tried to extend that interval out to 5mins or 10mins but it makes no difference and the 60seconds is good for diagnosing issues.

The picture attached is what is happening in the serial monitor.Capture
I am confused because it works sometimes.. do you have any idea what might be going wrong?

Code is getting a bit bloated but here it goes.

//Testing webserver, temperature probe readings and changing a variable from the webserver on an
//ESP32 module. This code will be merged with some existing arduino code that controls a fermentation
//chamber once functions are proven
// Load libraries
#include <WiFi.h>
#include <Wire.h>
//Load Temp Sensor Libraries
#include <OneWire.h>
#include <DallasTemperature.h>
//Load EEPROM Library
#include <EEPROM.h>
// define the number of bytes you want to access
#define EEPROM_SIZE 1
//Define temp probe pin
#define ONE_WIRE_BUS 15
// set up 1-wire probes
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//replace addresses for each new probe see - http://henrysbench.capnfatz.com/henrys-bench/arduino-temperature-measurements/ds18b20-arduino-user-manual-introduction-and-contents/ds18b20-user-manual-part-2-getting-the-device-address/
DeviceAddress AIR_TEMP_SENSOR = {0x28, 0xFF, 0x16, 0x8D, 0x87, 0x16, 0x03, 0x50}; //Test sensor A
DeviceAddress VAT_TEMP_SENSOR = {0x28, 0xFF, 0x0A, 0x2E, 0x68, 0x14, 0x04, 0xA6}; //Test sensor B
//variables for temp sensors
float air_temp;
float vat_temp;
//set temp variables
float set_temp;
float new_set_temp;
//define state variables for testing
const int STATE_ERROR = -1;
const int STATE_RELAX = 0;
const int STATE_IDLE = 1;
const int STATE_COOL = 2;
const int STATE_HEAT = 3;
int state = STATE_COOL;
// Replace with your network credentials
//const char* ssid = "";
//const char* password = "";
const char* ssid = "";
const char* password = "";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Replace with your unique IFTTT URL resource
const char* resource = "https://maker.ifttt.com/trigger/fermenter_temp/with/key/";
// Maker Webhooks IFTTT
const char* server_ifttt = "maker.ifttt.com";
//timer for IFTTT
unsigned long IFTTT_TIMER = 300000; //5mins in milliseconds
unsigned long currentMillis;
unsigned long startMillis;
void setup() {
Serial.begin(115200);

//Start EEPROM at defined size (above).
EEPROM.begin(EEPROM_SIZE);
// Set up temperature probes
sensors.setResolution(AIR_TEMP_SENSOR, 11); //resolution of 0.125deg cels,
sensors.setResolution(VAT_TEMP_SENSOR, 11); //takes approx 375ms
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();

//set up set temp variables here.
if (EEPROM.read(0) != 255) {
set_temp = EEPROM.read(0);
}
if (EEPROM.read(0) == 255) {
set_temp = 19.0;
}
new_set_temp = set_temp;

//Initalize timer
startMillis = millis();
}
//web server function
void web_server(WiFiClient client){
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

//Set Temp Changes By Button
if (header.indexOf("GET /set/up") >= 0) {
Serial.println("Increase New Set Temperature by 0.5");
new_set_temp = new_set_temp + 0.5;
}
else if (header.indexOf("GET /set/down") >= 0) {
Serial.println("Decrease New Set Temperature by 0.5");
new_set_temp = new_set_temp - 0.5;
}

//Submit button for changing set temp
if (header.indexOf("GET /set/submit") >= 0) {
Serial.print("Change Set Temp To ");
set_temp = new_set_temp;
Serial.println(set_temp);
EEPROM.write(0, set_temp);
EEPROM.commit();
Serial.println("EEPROM Write");
}

//Disconnect from client button to make it easier for the logging code to keep running after changing set temp
if (header.indexOf("GET /set/disconnect") >= 0) {
client.stop();
}

// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
//CSS Styles
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; font-size: 15px}");
client.println(".button {background-color: lightgrey; color: black; padding: 10px 20px; text-decoration: none; font-size: 15px; margin: 2px; cursor: pointer; border-color: black; border-style: solid; border-radius: 10px;}");
client.println(".temp {background-color: #4CAF50; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".heat {background-color: orange; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".cool {background-color: lightblue; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".idle {background-color: lightgrey; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".error {background-color: red; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println(".relax {background-color: white; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println("</style></head>");

// Web Page Heading
client.println("<body><h1>Laser Snake Temperature</h1>");
//Set Temp Line
client.println("Set Temperature =");
client.println("<inline-block class=\"temp\">" );
client.println(set_temp);
client.println("&#8451</inline-block><br><br><br>");

//Change Set Temp Line
client.println("New Set Temperature =");
client.println("<inline-block class=\"temp\">" );
client.println(new_set_temp);
client.println("&#8451</inline-block>");
client.println("<a href=\"/set/up\"><button class=\"button\">+</button></a><a href=\"/set/down\"><button class=\"button\">-</button></a><a href=\"/set/submit\"><button class=\"button\">SUBMIT</button></a></p><br>");

//Fermenter Temp Line
client.println("Fermenter Temperature =");
client.println("<inline-block class=\"temp\">" );
client.println(vat_temp);
client.println("&#8451</inline-block><br><br><br>");
//Fridge Temp Line
client.println("Fridge Temperature =");
client.println("<inline-block class=\"temp\">" );
client.println(air_temp);
client.println("&#8451</inline-block><br><br><br>");
//Status Temp Line
client.println("Current State =");
if(state == STATE_IDLE) {
client.println("<inline-block class=\"idle\">" );
client.println("IDLE");
client.println("</inline-block><br><br><br>");
}
else if(state == STATE_HEAT) {
client.println("<inline-block class=\"heat\">" );
client.println("HEATING");
client.println("</inline-block><br><br><br>");
}
else if(state == STATE_COOL) {
client.println("<inline-block class=\"cool\">" );
client.println("COOLING");
client.println("</inline-block><br><br><br>");
}
else if(state == STATE_ERROR) {
client.println("<inline-block class=\"error\">" );
client.println("ERROR");
client.println("</inline-block><br><br><br>");
}
else if(state == STATE_RELAX) {
client.println("<inline-block class=\"relax\">" );
client.println("RELAX");
client.println("</inline-block><br><br><br>");
}
//Disconnect Button
client.println("<a href=\"/set/disconnect\"><button class=\"button\">DISCONNECT</button></a></p><br><br><br>");

client.println("</body></html>");

// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
//google sheets logging function
void IFTTT_Log() {
Serial.print("Connecting to: ");
Serial.print(ssid);
WiFi.begin(ssid, password);
int timeout = 10 * 4;
while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
delay(250);
Serial.print(".");
}
Serial.println("");
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect, going back to sleep");
}
Serial.print("WiFi connected in: ");
Serial.print(millis());
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
//}
// Make an HTTP request to the IFTTT web service
//void makeIFTTTRequest() {
Serial.print("Connecting to ");
Serial.print(server_ifttt);

WiFiClient client;
int retries = 5;
while(!!!client.connect(server_ifttt, 80) && (retries-- > 0)) {
Serial.print(".");
}
Serial.println();
if(!!!client.connected()) {
Serial.println("Failed to connect...");
}

Serial.print("Request resource: ");
Serial.println(resource);
String jsonObject = String("{\"value1\":\"") + set_temp + "\",\"value2\":\"" + vat_temp
+ "\",\"value3\":\"" + air_temp + "\"}";

client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server_ifttt);
client.println("Connection: close\r\nContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);

while(!!!client.available() && (timeout-- > 0)){
delay(100);
}
if(!!!client.available()) {
Serial.println("No response...");
}
while(client.available()){
Serial.write(client.read());
}
Serial.println("\nclosing connection");
client.stop();
//startMillis resets
startMillis = millis();
}
void loop(){

//start timer
currentMillis = millis();

//Get temps
sensors.requestTemperaturesByAddress(AIR_TEMP_SENSOR);
sensors.requestTemperaturesByAddress(VAT_TEMP_SENSOR);
vat_temp = sensors.getTempC(VAT_TEMP_SENSOR);
air_temp = sensors.getTempC(AIR_TEMP_SENSOR);
WiFiClient client = server.available(); // Listen for incoming clients

//conditional statements to run webserver or google sheets function
if (client) {
web_server(client);
}
else if ((currentMillis - startMillis >= IFTTT_TIMER) && !client) {
IFTTT_Log();
}
else if ((currentMillis - startMillis < IFTTT_TIMER) && !client) {
Serial.println((currentMillis - startMillis)/1000);
// delay(1000);
}
}
6 Answers
0 Vote Up Vote Down
Matt answered 6 years ago

I should note that I have a very stable internet connection and the web server side is working perfectly and these are my temp loggings from 2 hours of running.. there should be 120 logs in there not 4!!!

 

 

Capture2

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello Matt, I appreciate you trying to figure it out before asking, but I’m here to help 🙂

Thanks for providing those details (I’ve edited your question to remove your SSID/password and API key), never post/share that data with anyone online.

I think it’s a problem with IFTTT (it’s failing to run those commands), if you go to: https://ifttt.com/activity

What do you see? Is it failing to run your applet?

0 Vote Up Vote Down
Matt answered 6 years ago

Under the IFTTT/activity it shows it runs as it should when it actually logs an event. I think its just not connecting to IFTTT most of the time.
Here is a screen shot of the IFTTT/activity
 
 
Capture3
 
I ran the test overnight (from 11:30pm to 10:25am) trying to log every minute if it had worked correctly it should have logged 655 events but instead it randomly logged 23 events scattered through that time.
I understand the IFTTT service probably caps the amount of requests but it would do that in a more consistent manor by just cutting it off when you reached the limit.
Here is the google sheets log for the night.
 
Capture4

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Exactly… It looks like IFTTT didn’t even try to run the applet. Otherwise, it would say “Failed to run applet”.

I actually had my project running for a few days and it worked flawlessly.

I’ve never experienced that error “No response” that you see in the Arduino IDE serial monitor… I’m not sure what’s happening to be honest…

Do you have a stable Internet connection? Does your ESP can be connected to the internet reliably?

Note: as you said, you should keep the requests to their API to a minimum. I usually recommend 1 request maximum every 10 minutes.

0 Vote Up Vote Down
Matt answered 6 years ago

Looks like I need to may have fixed it. I was initializing the wifi in the set up and in one of the functions that the loop calls. Got rid of the one in the loop and it’s working as expected now.

 

Thanks for your help!!

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Hello Matt, thanks for letting me know, I’m glad it’s working now! (As I said, try to keep to a maximum of 1 request every 10 minutes). I’ll mark this post as resolved!

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.