Problem transferring data to firebase using Sim7600CE, http - firebase

I am using ESP32 integrated Sim7600CE module to transfer data to Firebase Realtime database using http and got 400 Bad Request, please help me to solve this.
13:36:48.693 -> Status code: 400
13:36:48.833 -> Response: <html>
13:36:48.833 -> <head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
13:36:48.833 -> <body>
13:36:48.833 -> <center><h1>400 Bad Request</h1></center>
#define TINY_GSM_MODEM_SIM7600
#define TINY_GSM_RX_BUFFER 650
#include <ArduinoJson.h>
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h> //https://github.com/arduino-libraries/ArduinoHttpClient
#include "TinyGsmClient.h"
#define SIM_RESET_PIN 15
#define SIM_AT_BAUDRATE 115200
#define GSM_PIN ""
#define SIM_TX 17
#define SIM_RX 16
const char FIREBASE_HOST[] = "smartgrid1-930b0-default-rtdb.firebaseio.com";
const String FIREBASE_AUTH = "zezoyo69fX9eqPDpjaom6CJAyXhnW081GlJ4ah5c";
const String FIREBASE_PATH = "/";
const int SSL_PORT = 443;
char APN[] = "v-internet"; //
char USER[] = "";
char PASS[] = "";
TinyGsm modem(Serial2);
TinyGsmClient gsm_client_secure_modem(modem);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);
unsigned long previousMillis = 0;
// Hàm dùng để thiết lập modem SIM tạo kết nối GPRS
bool connectToGPRS() {
// Unlock SIM (nếu có)
if (GSM_PIN && modem.getSimStatus() != 3)
modem.simUnlock(GSM_PIN);
Serial.println("Ket noi toi nha mang...");
while (!modem.waitForNetwork(60000L)) {
Serial.println("._.");
delay(5000);
}
// Nếu không thấy sóng từ nhà mạng thì thoát hàm
if (!modem.isNetworkConnected())
return false;
Serial.println("Ket noi GPRS");
if (!modem.gprsConnect(APN)) {// Hàm kết nối tới gprs trả về true/false cho biết có kết nối được hay chưa
Serial.print("Khong the ket noi GPRS - ");
Serial.println(APN);
return false;
}
// Kiểm tra lại lần nữa để chắc cú
if (modem.isGprsConnected()){
Serial.println("Da ket noi duoc GPRS!");
return true;
}
return false;
}
// Hàm khởi động module SIM
bool initModemSIM() {
Serial.println("Bat dau khoi dong module SIM");
// Đặt chân reset của module xuống LOW để chạy
pinMode(SIM_RESET_PIN, OUTPUT);
digitalWrite(SIM_RESET_PIN, LOW);
delay(5000);
Serial2.begin(SIM_AT_BAUDRATE);// Module SIM giao tiếp với ESP qua cổng Serial2 bằng AT cmds
if (!modem.restart()) {
Serial.println("Khong the khoi dong lai module SIM => Co loi");
return false;
}
return true;
}
void setup() {
Serial.begin(115200);
if (initModemSIM()){
while(!connectToGPRS()) delay(100);
}
if (modem.isGprsConnected()) {
Serial.print(F("Connecting to "));
Serial.println(APN);
if (!modem.gprsConnect(APN, USER, PASS))
{
Serial.println(" fail");
delay(1000);
return;
}
}
if (initModemSIM()){
while(!connectToGPRS()) delay(100);
}
modem.restart();
String modemInfo = modem.getModemInfo();
Serial.print("Modem: ");
Serial.println(modemInfo);
http_client.setHttpResponseTimeout(10 * 1000); //^0 secs timeout
}
void loop() {
Serial.print(F("Connecting to "));
Serial.println(APN);
if (!modem.gprsConnect(APN, USER, PASS))
{
Serial.println(" fail");
delay(1000);
return;
}
Serial.println(" OK");
http_client.connect(FIREBASE_HOST, SSL_PORT);
while (true) {
if (!http_client.connected())
{
Serial.println();
http_client.stop();// Shutdown
Serial.println("HTTP not connect");
break;
}
else
{
dht_loop();
}
}
}
void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
String response;
int statusCode = 0;
http->connectionKeepAlive(); // Currently, this is needed for HTTPS
String url;
if (path[0] != '/')
{
url = "/";
}
url += path + ".json";
url += "?auth=" + FIREBASE_AUTH;
Serial.print("POST:");
Serial.println(url);
Serial.print("Data:");
Serial.println(data);
String contentType = "application/json";
http->put(url, contentType, data);
statusCode = http->responseStatusCode();
Serial.print("Status code: ");
Serial.println(statusCode);
response = http->responseBody();
Serial.print("Response: ");
Serial.println(response);
// if (!http->connected())
// {
// Serial.println();
// http->stop();// Shutdown
// Serial.println("HTTP POST disconnected");
// }
}
void dht_loop()
{
String h = String(random(1,100));
delay(100);
Serial.print("h = ");
Serial.println(h);
String Data = "{";
Data += "\"Sim\":" + h + "";
Data += "}";
PostToFirebase("PATCH", FIREBASE_PATH, Data, &http_client);
}

Related

Arduino HTTP Client not connecting to Firebase

I am trying to post GPS data to firebase using the Arduino Uno R3, SIM800L and NEO6M via GPRS. I have managed to connect to the APN, but I can't get the "http_client.connect()" to work. It keeps giving the same error.
Code
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER 256
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define SerialMonitor Serial
#define ARDUINO_GPS_RX 3
#define ARDUINO_GPS_TX 2
TinyGPSPlus tinyGPS;
SoftwareSerial ss(ARDUINO_GPS_TX , ARDUINO_GPS_RX);
#define gpsPort ss
#define rxPin 9
#define txPin 8
SoftwareSerial sim800(txPin,rxPin);
const char FIREBASE_HOST[] = "realtimedatabase.firebaseio.com";
const String FIREBASE_AUTH = "";
const String FIREBASE_PATH = "/gps-data/coordinate/employee_03";
const int SSL_PORT = 443;
char apn[] = "";
char user[] = "";
char pass[] = "";
TinyGsm modem(sim800);
TinyGsmClientSecure gsm_client_secure_modem(modem,0);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
gpsPort.begin(9600);
Serial.println(F("gps serial initialize"));
sim800.begin(9600);
Serial.println(F("SIM800A serial initialize"));
Serial.println(F("Initializing modem..."));
modem.init();
String modemInfo = modem.getModemInfo();
Serial.print(F("Modem: "));
Serial.println(modemInfo);
http_client.setHttpResponseTimeout(10 * 1000);
}
void loop()
{
Serial.print(F("Connecting to "));
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println(F(" fail"));
//delay(1000);
return;
}
Serial.println(F(" OK"));
if (modem.isGprsConnected()) { Serial.println("GPRS connected"); }
http_client.connect(FIREBASE_HOST, SSL_PORT);
while (true) {
if (!http_client.connected())
{
Serial.println();
http_client.stop();// Shutdown
Serial.println(F("HTTP not connected"));
break;
}
else
{
//ss.listen();
gps_loop();
}
}
}
void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
String response;
int statusCode = 0;
http->connectionKeepAlive();
String url;
if (path[0] != '/')
{
url = "/";
}
url += path + ".json";
url += "?auth=" + FIREBASE_AUTH;
Serial.print("POST:");
Serial.println(url);
Serial.print("Data:");
Serial.println(data);
String contentType = "application/json";
http->put(url, contentType, data);
statusCode = http->responseStatusCode();
Serial.print(F("Status code: "));
Serial.println(statusCode);
response = http->responseBody();
Serial.print(F("Response: "));
Serial.println(response);
if (!http->connected())
{
Serial.println();
http->stop();// Shutdown
Serial.println(F("HTTP POST disconnected"));
}
}
void gps_loop()
{
// ss.listen();
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 2000;){
//ss.listen();
while (ss.available()){
if (tinyGPS.encode(ss.read())){
newData = true;
break;
}
}
}
if(true){
newData = false;
String latitude, longitude;
latitude = String(tinyGPS.location.lat(), 6);
longitude = String(tinyGPS.location.lng(), 6);
Serial.print("Latitude= ");
Serial.print(latitude);
Serial.print(" Longitude= ");
Serial.println(longitude);
String gpsData = "{";
gpsData += "\"lat\":" + latitude + ",";
gpsData += "\"lng\":" + longitude + "";
gpsData += "}";
PostToFirebase("PATCH", FIREBASE_PATH, gpsData, &http_client);
}
}
Error
gps serial initialize
SIM800A serial initialize
Initializing modem...
Modem: SIM800 R14.18
Connecting to apn OK
GPRS connected
HTTP not connected
Connecting to apn OK
GPRS connected
I have tried using ESP8266 (Wemos D1 R1) to save my GPS data to the database via WiFi, and that worked. Now, the problem is I can't connect to the firebase database using my Arduino and SIM800L. What should I do?

Arduino - trying to get latitude and longitude from GPS and send to Firebase but failed

This is the problem continue from my previous question. After I added for listen() in loop function, the data can be read from the GPS and shown in Serial Monitor, but the http connection for now is failed, and it shows the status -2, anyone knows this status error? Is it I need to add other function as well?
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER 256
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define SerialMonitor Serial
#define ARDUINO_GPS_RX 10
#define ARDUINO_GPS_TX 11
TinyGPSPlus tinyGPS;
SoftwareSerial ss(ARDUINO_GPS_TX , ARDUINO_GPS_RX);
#define gpsPort ss
#define rxPin 8
#define txPin 9
SoftwareSerial sim800(txPin,rxPin);
const char FIREBASE_HOST[] = "my-firebase-host";
const String FIREBASE_AUTH = "my-firebase-auth";
const String FIREBASE_PATH = "/";
const int SSL_PORT = 443;
char apn[] = "internet";
char user[] = "";
char pass[] = "";
TinyGsm modem(sim800);
TinyGsmClientSecure gsm_client_secure_modem(modem, 0);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
gpsPort.begin(9600);
Serial.println(F("gps serial initialize"));
sim800.begin(9600);
Serial.println(F("SIM800A serial initialize"));
Serial.println(F("Initializing modem..."));
modem.init();
String modemInfo = modem.getModemInfo();
Serial.print(F("Modem: "));
Serial.println(modemInfo);
http_client.setHttpResponseTimeout(10 * 1000);
}
void loop()
{
Serial.print(F("Connecting to "));
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println(F(" fail"));
//delay(1000);
return;
}
Serial.println(F(" OK"));
http_client.connect(FIREBASE_HOST, SSL_PORT);
while (true) {
if (!http_client.connected())
{
Serial.println();
http_client.stop();// Shutdown
Serial.println(F("HTTP not connected"));
break;
}
else
{
//ss.listen();
gps_loop();
}
}
}
void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
String response;
int statusCode = 0;
http->connectionKeepAlive();
String url;
if (path[0] != '/')
{
url = "/";
}
url += path + ".json";
url += "?auth=" + FIREBASE_AUTH;
Serial.print("POST:");
Serial.println(url);
Serial.print("Data:");
Serial.println(data);
String contentType = "application/json";
http->put(url, contentType, data);
statusCode = http->responseStatusCode();
Serial.print(F("Status code: "));
Serial.println(statusCode);
response = http->responseBody();
Serial.print(F("Response: "));
Serial.println(response);
if (!http->connected())
{
Serial.println();
http->stop();// Shutdown
Serial.println(F("HTTP POST disconnected"));
}
}
void gps_loop()
{
ss.listen();
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 2000;){
//ss.listen();
while (ss.available()){
if (tinyGPS.encode(ss.read())){
newData = true;
break;
}
}
}
if(true){
newData = false;
String latitude, longitude;
latitude = String(tinyGPS.location.lat(), 6);
longitude = String(tinyGPS.location.lng(), 6);
Serial.print("Latitude= ");
Serial.print(latitude);
Serial.print(" Longitude= ");
Serial.println(longitude);
String gpsData = "{";
gpsData += "\"lat\":" + latitude + ",";
gpsData += "\"lng\":" + longitude + "";
gpsData += "}";
PostToFirebase("PATCH", FIREBASE_PATH, gpsData, &http_client);
}
}

Arduino - trying to get longitude and latitude from GPS with Arduino Uno and GSM and send the data to Firebase cloud but getting 0.0000

I am using Arduino Uno R3, GSM SIM 800 and GPS to get the latitude and longitude and send to the Firebase cloud, but end up I only can get 0.0000 for the location, was trying for TinyGPS++ code before and the GPS is working. Can someone helps me to see where is the error, I really tried for many solutions but still can't work
*** solved for the above problems
But now comes with the problem is about the http connect with the firebase. The status showing -2 for the http connection problem.
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER 256
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define SerialMonitor Serial
#define ARDUINO_GPS_RX 11
#define ARDUINO_GPS_TX 10
TinyGPSPlus tinyGPS;
SoftwareSerial ss(ARDUINO_GPS_TX , ARDUINO_GPS_RX);
#define gpsPort ss
#define rxPin 3
#define txPin 2
SoftwareSerial sim800(txPin,rxPin);
const char FIREBASE_HOST[] = "my_firebase_host";
const String FIREBASE_AUTH = "my_firebase_auth";
const String FIREBASE_PATH = "/";
const int SSL_PORT = 443;
char apn[] = "internet";
char user[] = "";
char pass[] = "";
TinyGsm modem(sim800);
TinyGsmClientSecure gsm_client_secure_modem(modem, 0);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
gpsPort.begin(9600);
Serial.println(F("gps serial initialize"));
sim800.begin(9600);
Serial.println(F("SIM800A serial initialize"));
Serial.println(F("Initializing modem..."));
modem.init();
String modemInfo = modem.getModemInfo();
Serial.print(F("Modem: "));
Serial.println(modemInfo);
http_client.setHttpResponseTimeout(10 * 1000);
}
void loop()
{
Serial.print(F("Connecting to "));
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println(F(" fail"));
//delay(1000);
return;
}
Serial.println(F(" OK"));
http_client.connect(FIREBASE_HOST, SSL_PORT);
while (true) {
if (!http_client.connected())
{
Serial.println();
http_client.stop();// Shutdown
Serial.println(F("HTTP not connected"));
break;
}
else
{
gps_loop();
}
}
}
void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
String response;
int statusCode = 0;
http->connectionKeepAlive();
String url;
if (path[0] != '/')
{
url = "/";
}
url += path + ".json";
url += "?auth=" + FIREBASE_AUTH;
Serial.print("POST:");
Serial.println(url);
Serial.print("Data:");
Serial.println(data);
String contentType = "application/json";
http->put(url, contentType, data);
statusCode = http->responseStatusCode();
Serial.print(F("Status code: "));
Serial.println(statusCode);
response = http->responseBody();
Serial.print(F("Response: "));
Serial.println(response);
if (!http->connected())
{
Serial.println();
http->stop();// Shutdown
Serial.println(F("HTTP POST disconnected"));
}
}
void gps_loop()
{
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 2000;){
while (ss.available()){
if (tinyGPS.encode(ss.read())){
newData = true;
break;
}
}
}
if(true){
newData = false;
String latitude, longitude;
latitude = String(tinyGPS.location.lat(), 6);
longitude = String(tinyGPS.location.lng(), 6);
Serial.print("Latitude= ");
Serial.print(latitude);
Serial.print(" Longitude= ");
Serial.println(longitude);
String gpsData = "{";
gpsData += "\"lat\":" + latitude + ",";
gpsData += "\"lng\":" + longitude + "";
gpsData += "}";
PostToFirebase("PATCH", FIREBASE_PATH, gpsData, &http_client);
}
}

Trying to understand ESP32 OTA over the GSM code

I am performing ESP32 OTA over the GSM. I have found some example code online, modified it slightly based on my board connections and was able to sucesfully perform a firmware update.
I use lilygo ESP32 GSM development board.
However, it frustrates me that I am not capable of understanding what really happens in the code at the low level. I really want to understand before I go any further with my project.
Full code for my OTA update:
#include <Update.h>
#define TINY_GSM_MODEM_SIM800
// Increase RX buffer
#define TINY_GSM_RX_BUFFER 1030
const char apn[] = "omnitel";
const char user[] = "omni";
const char pass[] = "omni";
#define MODEM_RST 5
#define MODEM_PWRKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define LED_GPIO 13
#define LED_ON HIGH
#define LED_OFF LOW
#define SerialAT Serial1
#include <TinyGsmClient.h>
#include <CRC32.h>
#include "FS.h"
#include "SPIFFS.h"
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, Serial);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
const char server[] = "myesptestserver.ddns.net";
const int port = 80;
const char resource[] = "/esp.bin"; //here de bin file
uint32_t knownCRC32 = 0x6f50d767;
uint32_t knownFileSize = 1024; // In case server does not send it
void setup()
{
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
// Set console baud rate
Serial.begin(115200);
setupModem();
delay(10);
if (!SPIFFS.begin(true))
{
Serial.println("SPIFFS Mount Failed");
return;
}
SPIFFS.format();
listDir(SPIFFS, "/", 0);
// Set GSM module baud rate
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
String modemInfo = modem.getModemInfo();
Serial.print("Modem: ");
Serial.println(modemInfo);
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
}
void loop()
{
Serial.print("Waiting for network...");
if (!modem.waitForNetwork())
{
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(server);
// if you get a connection, report back via serial:
if (!client.connect(server, port))
{
Serial.println(" fail");
delay(10000);
return;
}
Serial.println(" OK");
// Make a HTTP request:
client.print(String("GET ") + resource + " HTTP/1.0\r\n");
client.print(String("Host: ") + server + "\r\n");
client.print("Connection: close\r\n\r\n");
long timeout = millis();
while (client.available() == 0)
{
if (millis() - timeout > 5000L)
{
Serial.println(">>> Client Timeout !");
client.stop();
delay(10000L);
return;
}
}
Serial.println("Reading header");
uint32_t contentLength = knownFileSize;
File file = SPIFFS.open("/update.bin", FILE_APPEND);
while (client.available())
{
String line = client.readStringUntil('\n');
line.trim();
//Serial.println(line); // Uncomment this to show response header
line.toLowerCase();
if (line.startsWith("content-length:"))
{
contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
}
else if (line.length() == 0)
{
break;
}
}
timeout = millis();
uint32_t readLength = 0;
CRC32 crc;
unsigned long timeElapsed = millis();
printPercent(readLength, contentLength);
while (readLength < contentLength && client.connected() && millis() - timeout < 10000L)
{
int i = 0;
while (client.available())
{
// read file data to spiffs
if (!file.print(char(client.read())))
{
Serial.println("Appending file");
}
//Serial.print((char)c); // Uncomment this to show data
//crc.update(c);
readLength++;
if (readLength % (contentLength / 13) == 0)
{
printPercent(readLength, contentLength);
}
timeout = millis();
}
}
file.close();
printPercent(readLength, contentLength);
timeElapsed = millis() - timeElapsed;
Serial.println();
client.stop();
Serial.println("stop client");
modem.gprsDisconnect();
Serial.println("gprs disconnect");
Serial.println();
float duration = float(timeElapsed) / 1000;
/*
Serial.print("Tamaño de Archivo: ");
Serial.println(contentLength);
Serial.print("Leido: ");
Serial.println(readLength);
Serial.print("Calculado. CRC32: 0x");
Serial.println(crc.finalize(), HEX);
Serial.print("Conocido CRC32: 0x");
Serial.println(knownCRC32, HEX);
Serial.print("Bajado en: ");
Serial.print(duration);
Serial.println("s");
Serial.println("Se genera una espera de 3 segundos");
for (int i = 0; i < 3; i++)
{
Serial.print(String(i) + "...");
delay(1000);
}
*/
//readFile(SPIFFS, "/update.bin");
updateFromFS();
// Do nothing forevermore
while (true)
{
delay(1000);
}
}
void appendFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file)
{
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message))
{
Serial.println("APOK");
}
else
{
Serial.println("APX");
}
}
void readFile(fs::FS &fs, const char *path)
{
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available())
{
Serial.write(file.read());
delayMicroseconds(100);
}
}
void writeFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message))
{
Serial.println("File written");
}
else
{
Serial.println("Write failed");
}
}
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root)
{
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory())
{
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file)
{
if (file.isDirectory())
{
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels)
{
listDir(fs, file.name(), levels - 1);
}
}
else
{
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void deleteFile(fs::FS &fs, const char *path)
{
Serial.printf("Deleting file: %s\n", path);
if (fs.remove(path))
{
Serial.println("File deleted");
}
else
{
Serial.println("Delete failed");
}
}
void updateFromFS()
{
File updateBin = SPIFFS.open("/update.bin");
if (updateBin)
{
if (updateBin.isDirectory())
{
Serial.println("Directory error");
updateBin.close();
return;
}
size_t updateSize = updateBin.size();
if (updateSize > 0)
{
Serial.println("Starting update");
performUpdate(updateBin, updateSize);
}
else
{
Serial.println("Error, archivo vacío");
}
updateBin.close();
// whe finished remove the binary from sd card to indicate end of the process
//fs.remove("/update.bin");
}
else
{
Serial.println("no such binary");
}
}
void performUpdate(Stream &updateSource, size_t updateSize)
{
if (Update.begin(updateSize))
{
size_t written = Update.writeStream(updateSource);
if (written == updateSize)
{
Serial.println("Writes : " + String(written) + " successfully");
}
else
{
Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
}
if (Update.end())
{
Serial.println("OTA finished!");
if (Update.isFinished())
{
Serial.println("Restart ESP device!");
ESP.restart();
}
else
{
Serial.println("OTA not fiished");
}
}
else
{
Serial.println("Error occured #: " + String(Update.getError()));
}
}
else
{
Serial.println("Cannot beggin update");
}
}
void printPercent(uint32_t readLength, uint32_t contentLength)
{
// If we know the total length
if (contentLength != -1)
{
Serial.print("\r ");
Serial.print((100.0 * readLength) / contentLength);
Serial.print('%');
}
else
{
Serial.println(readLength);
}
}
void setupModem()
{
#ifdef MODEM_RST
// Keep reset high
pinMode(MODEM_RST, OUTPUT);
digitalWrite(MODEM_RST, HIGH);
#endif
pinMode(MODEM_PWRKEY, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
// Turn on the Modem power first
digitalWrite(MODEM_POWER_ON, HIGH);
// Pull down PWRKEY for more than 1 second according to manual requirements
digitalWrite(MODEM_PWRKEY, HIGH);
delay(100);
digitalWrite(MODEM_PWRKEY, LOW);
delay(1000);
digitalWrite(MODEM_PWRKEY, HIGH);
// Initialize the indicator as an output
pinMode(LED_GPIO, OUTPUT);
digitalWrite(LED_GPIO, LED_OFF);
}
From what I understand, the following part of the code reads my binary file that is stored in the server and read it into SPIFFS memory:
while (readLength < contentLength && client.connected() && millis() - timeout < 10000L)
{
int i = 0;
while (client.available())
{
// read file data to spiffs
if (!file.print(char(client.read())))
{
Serial.println("Appending file");
}
//Serial.print((char)c); // Uncomment this to show data
//crc.update(c);
readLength++;
if (readLength % (contentLength / 13) == 0)
{
printPercent(readLength, contentLength);
}
timeout = millis();
}
}
Then the updateFromFS() function is executed, which checks whether the binary is all good and then calls another function performUpdate(updateBin, updateSize);
void updateFromFS()
{
File updateBin = SPIFFS.open("/update.bin");
if (updateBin)
{
if (updateBin.isDirectory())
{
Serial.println("Directory error");
updateBin.close();
return;
}
size_t updateSize = updateBin.size();
if (updateSize > 0)
{
Serial.println("Starting update");
performUpdate(updateBin, updateSize);
}
else
{
Serial.println("Error, archivo vacío");
}
updateBin.close();
// whe finished remove the binary from sd card to indicate end of the process
//fs.remove("/update.bin");
}
else
{
Serial.println("no such binary");
}
}
in the performUpdate(updateBin, updateSize), 2 functions from Arduino Update Library are being called ( Update.begin(updateSize) and Update.writeStream(updateSource) ) and that is where I cant follow anymore...
void performUpdate(Stream &updateSource, size_t updateSize)
{
if (Update.begin(updateSize))
{
size_t written = Update.writeStream(updateSource);
if (written == updateSize)
{
Serial.println("Writes : " + String(written) + " successfully");
}
else
{
Serial.println("Written only : " + String(written) + "/" + String(updateSize) + ". Retry?");
}
if (Update.end())
{
Serial.println("OTA finished!");
if (Update.isFinished())
{
Serial.println("Restart ESP device!");
ESP.restart();
}
else
{
Serial.println("OTA not fiished");
}
}
else
{
Serial.println("Error occured #: " + String(Update.getError()));
}
}
else
{
Serial.println("Cannot beggin update");
}
}
I have found this Update library source code :
https://github.com/espressif/arduino-esp32/blob/master/libraries/Update/src/Updater.cpp
But it seems that this is beyond my knowledge to understand what really is going on in this code.
First of all, we pass only 1 variable to .begin method, but the function prototype requires more than that:
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, const char *label)
And then I am trying to understand what happens in function Update.writeStream(updateSource) but cant..
If anyone could shed some light here I would appreciate a lot! Thanks in advance...
The algorithm downloads the new firmware (.bin) from the cloud to the device's local memory and makes the firmware change with the UpdateClass library
(I see the texts in Spanish if you want we speak in Spanish)
Check the .h (only one argument required)
https://github.com/espressif/arduino-esp32/blob/master/libraries/Update/src/Update.h
/*
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL);
You could use the OTAdrive library and use it for OTA updates.
Check here for the esp32 gsm modem OTA example.

Error using thingspeak talk back to control arduino led

The error I'm facing is about using talkback function of thingspeak to control my Arduino LED. it cannot execute the talkback command to off the led. The error is in my void get talkback. Please help me
#include "ThingSpeak.h"
#include <Ethernet.h>
#define redLED 8
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F};
const String channelsAPIKey = "";
const String talkBackAPIKey = "";
const String talkBackID = "";
const String talkCommandID = "";
const unsigned int getTalkBackInterval = 10 * 1000;
const unsigned int updateChannelsInterval = 15 * 1000;
String talkBackCommand;
long lastConnectionTimeChannels = 0;
boolean lastConnectedChannels = false;
int failedCounterChannels = 0;
long lastConnectionTimeTalkBack = 0;
boolean lastConnectedTalkBack = false;
int failedCounterTalkBack = 0;
char charIn;
// Arduino Ethernet Client is initialized
EthernetClient client;
void setup()
{
Ethernet.init(10); // Most Arduino Ethernet hardware
Serial.begin(9600); //Initialize serial
// start the Ethernet connection:
pinMode(redLED, OUTPUT);
digitalWrite(redLED, HIGH);
}
void loop()
{
getTalkBack();
}
void getTalkBack()
{
String tsData;
tsData = talkBackID + "/commands/execute?api_key=" + talkBackAPIKey;
if ((!client.connected() && (millis() - lastConnectionTimeTalkBack > getTalkBackInterval)))
{
if (client.connect("api.thingspeak.com", 80))
{
client.println("GET /talkbacks/" + tsData + " HTTP/1.0");
client.println();
lastConnectionTimeTalkBack = millis();
if (client.connected())
{
Serial.println("---------------------------------------");
Serial.println("GET TalkBack command");
Serial.println();
Serial.println("Connecting to ThingSpeak");
Serial.println();
Serial.println();
Serial.println("Server response");
Serial.println();
failedCounterTalkBack = 0;
while (client.connected() && !client.available()) delay(2000); //waits for data
while (client.connected() || client.available())
{
charIn = client.read();
talkBackCommand += charIn;
Serial.print(charIn);
if (talkBackCommand == "LED_ON")
{
digitalWrite(redLED, HIGH);
}
if (talkBackCommand == "LED_OFF")
{
digitalWrite(redLED, LOW);
}
}
if (talkBackCommand = talkBackCommand.substring(talkBackCommand.indexOf("_CMD_") + 5));
{
Serial.println();
Serial.println();
Serial.println("Disconnected");
Serial.println();
Serial.println("--------");
Serial.println();
Serial.println("talkback command was");
Serial.println();
Serial.println("--------");
Serial.println();
Serial.println(talkBackCommand);
Serial.println("--------");
Serial.println();
}
}
else
{
failedCounterTalkBack++;
Serial.println("Connection to ThingSpeak failed (" + String(failedCounterTalkBack, DEC) + ")");
Serial.println();
lastConnectionTimeChannels = millis();
}
}
}
if (failedCounterTalkBack > 3 )
{
startEthernet();
}
client.stop();
Serial.flush();
}
The below is a image from my serial monitor. It shows that I can capture the command but couldn't execute it.
Looks like you are reading and appending all the received bytes to talkBackCommand. So, talkBackCommand is "HTTP/1.1 200 OK Date...." and if (talkBackCommand == "LED_ON") would never be true.
I think you wanted to see if the received data contain your commands LED_ON or LED_OFF. You can do something like this:
if (talkBackCommand.indexOf("LED_ON") != -1)
indexOf() locates a String in a String and returns index or -1 if not found.
See more info about indexOf() here: https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/

Resources