So as to send data every second, it should like
if(millis() - lastMillis > 1000) {
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
}
So as to send data every second, it should like
if(millis() - lastMillis > 1000) {
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
}
Read this post as why one should not send data at rapid rate Sending MQTT messages within rate limits
Hold on you are just sending data once and sleep. So why dont you put the entire code in main loop.
void loop{
//read sensor
//send data
//sleep.
}
Hi,
For some reason it went to sleep before the sensor readings could be sent to Cayenne,
Do you think that the previous setup where we usd the sleep within the Cayenne OUT procedure, is not efficient?
I guess you mean also using the Sent = TRUE within the void loop?
can you send the basic code you are using to connect your device to cayenne. Remove all sensor reading code from it.
Hello again,
Please find the code below
#define RX 32
#define TX 33
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds /
#define TIME_TO_SLEEP 15 / Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
#define VIRTUAL_CHANNEL 0
bool send;
// Your GPRS credentials (leave empty, if missing)
const char apn = ââ; // Your APN
const char gprsUser = ââ; // User
const char gprsPass = ââ; // Password
const char simPIN = ââ; // SIM card PIN code, if any
// TTGO T-Call pin definitions
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <CayenneMQTTGSM.h>
char username = ââ;
char password = ââ;
char clientID = ââ;
#include <Wire.h>
#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println(âWakeup caused by external signal using RTC_IOâ); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println(âWakeup caused by external signal using RTC_CNTLâ); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println(âWakeup caused by timerâ); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println(âWakeup caused by touchpadâ); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println(âWakeup caused by ULP programâ); break;
default : Serial.printf(âWakeup was not caused by deep sleep: %d\nâ,wakeup_reason); break;
}
}
void GSM_SETUP() {
// Set console baud rate
SerialMon.begin(9600);
delay(10);
// Set-up modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println(âInitializing modemâŚâ);
modem.restart();
// Or, use modem.init() if you donât need the complete restart
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
Serial.println("SIM Unlocked");
}
Cayenne.begin(username, password, clientID, SerialAT, apn, gprsUser, gprsPass, simPIN);
}
void GSM_LOOP() {
Cayenne.loop();
}
void TFMINI_SETUP()
{
}
void TFMINI_LOOP() {
if (Serial2.available()) { //check if serial port has data input
}
Cayenne.loop();
if (send)
{
//delay(20000);
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println(âSetup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Secondsâ);
//Serial.println(âGoing to sleep nowâ);
Serial.println(âBefore SerialFlushâ);
//Serial.flush();
Serial.println(âAfter SerialFlushâ);
Serial.println(âBefore Deep Sleepâ);
esp_deep_sleep_start();
Serial.println(âAfter Deep Sleepâ);
Serial.println(âThis will never be printedâ);
}
}
}
}
}
void setup() {
GSM_SETUP();
TFMINI_SETUP();
}
void loop() {
GSM_LOOP();
TFMINI_LOOP();
}
CAYENNE_OUT_DEFAULT(){
Cayenne.virtualWrite(VIRTUAL_CHANNEL, dist);
send = true;
}
try this code and point out what was the mistake you were making:
#include <Wire.h>
#include <TinyGsmClient.h>
#include <CayenneMQTTGSM.h>
#define RX 32
#define TX 33
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds /
#define TIME_TO_SLEEP 15 / Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
#define VIRTUAL_CHANNEL 0
bool send = false;
// Your GPRS credentials (leave empty, if missing)
const char apn = ""; // Your APN
const char gprsUser = ""; // User
const char gprsPass = ""; // Password
const char simPIN = ""; // SIM card PIN code, if any
// TTGO T-Call pin definitions
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
char username = "";
char password = "";
char clientID = "";
TinyGsm modem(SerialAT);
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: % d\n", wakeup_reason); break;
}
}
void setup() {
// Set console baud rate
SerialMon.begin(9600);
delay(10);
// Set-up modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modemâŚ");
modem.restart();
// Or, use modem.init() if you donât need the complete restart
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
Serial.println("SIM Unlocked");
}
Cayenne.begin(username, password, clientID, SerialAT, apn, gprsUser, gprsPass, simPIN);
}
void loop() {
Cayenne.loop();
if (send)
{
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Before Deep Sleep");
esp_deep_sleep_start();
Serial.println("After Deep Sleep");
}
}
CAYENNE_OUT_DEFAULT() {
Cayenne.virtualWrite(VIRTUAL_CHANNEL, dist);
send = true;
}
Hello again,
If I interpreted correctly the code, you entered the if send statement within the GSM initialisation loop and not in the sensor output void loop. Did I understand that correctly?
Furthermore you added the
#include <Wire.h>
#include <TinyGsmClient.h>
#include <CayenneMQTTGSM.h>
A library or a cmobination of them causes an error duroing compilation of the TTGO ESP32 LTE board and the error message I get is:
C:\Users\Spiros\Documents\Arduino\libraries\TinyGSM\src/TinyGsmClient.h:117:4: error: #error âPlease define GSM modem modelâ
#error âPlease define GSM modem modelâ
Is it okay I do not include these libraries?
Thank you again
Spyros
this where already in your code and rest all is the same.
the only thing that I changed was this bool send = false;
Hi Shramik,
Thanks for your reply. It is advised to start a send statemnet using the false as first statement of operation?
i did not understand what you mean up there.
Apologies,
In the this bool send = false;
is it advised to start with a false statement showing that Cayenne has not yet send data?
can you tell me what does this do?
if (send)
{
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Before Deep Sleep");
esp_deep_sleep_start();
Serial.println("After Deep Sleep");
}
I think it creates a counter for keeping record of the number of wake up that have taken place, correct?
not that, but this part and especially the if statement:-
if (send)
{
// Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Before Deep Sleep");
esp_deep_sleep_start();
Serial.println("After Deep Sleep");
}
Hi Shramik,
Apologies for the late reply, I now think I have understood it.
You have created an if statement that includes all the sleep command part and will only be run when the transmission of data to Cayenne has been completed.
You also have stated at the beginning of the code that the boolean expression by the name âsendâ is stated as FALSE as to avoid any errors taking place as the board wakes up and might lead to the board going back to sleep again immediately,
Did I get it right?
Thanks Again
somewhat correct. i hope it works as you want.
Hello again,
I did enter the amendments to my previous code buy unfortunately I do not get the data sent across to Cayene before deep sleep takes place.
The complete code is listed here in case you may have time to have a look at it,
#define RX 32
#define TX 33
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 15 /* Time ESP32 will go to sleep (in seconds) */
RTC_DATA_ATTR int bootCount = 0;
#define VIRTUAL_CHANNEL 0
//char ssid[] = "";
//char wifiPassword[]="";
bool send = false;
/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and
directly use Serial1 serial port*/
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
// Your GPRS credentials (leave empty, if missing)
const char apn[] = ""; // Your APN
const char gprsUser[] = ""; // User
const char gprsPass[] = ""; // Password
const char simPIN[] = ""; // SIM card PIN code, if any
// TTGO T-Call pin definitions
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <CayenneMQTTGSM.h>
char username[] = "";
char password[] = "";
char clientID[] = "";
// Define the serial console for debug prints, if needed
//#define TINY_GSM_DEBUG SerialMon
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void GSM_SETUP() {
// Set console baud rate
SerialMon.begin(9600);
delay(10);
// Set-up modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Or, use modem.init() if you don't need the complete restart
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
Serial.println("SIM Unlocked");
}
Cayenne.begin(username, password, clientID, SerialAT, apn, gprsUser, gprsPass, simPIN);
}
void GSM_LOOP() {
// Put ESP32 into deep sleep mode (with timer wake up)
Cayenne.loop();
if (send)
{
// Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Before Deep Sleep");
esp_deep_sleep_start();
Serial.println("After Deep Sleep");
}
}
//}
void TFMINI_SETUP()
{
//Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer
//Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
Serial2.begin(115200, SERIAL_8N1, RX, TX);
}
void TFMINI_LOOP() {
if (Serial2.available()) { //check if serial port has data input
//Serial.print("*");
if(Serial2.read() == HEADER) { //assess data package frame header 0x59
uart[0]=HEADER;
if (Serial2.read() == HEADER) { //assess data package frame header 0x59
uart[1] = HEADER;
for (i = 2; i < 9; i++) { //save data in array
uart[i] = Serial2.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
dist = uart[2] + uart[3] * 256; //calculate distance value
strength = uart[4] + uart[5] * 256; //calculate signal strength value
temprature = uart[6] + uart[7] *256;//calculate chip temprature
temprature = temprature/8 - 256;
//Serial.println("HEllo5");
Serial.print("dist = ");
Serial.println(dist); //output measure distance value of LiDAR
}
}
}
}
}
void setup() {
GSM_SETUP();
TFMINI_SETUP();
//SLEEP_SETUP();
}
void loop() {
GSM_LOOP();
TFMINI_LOOP();
//SLEEP_LOOP();
}
CAYENNE_OUT_DEFAULT(){
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(VIRTUAL_CHANNEL, dist);
send = true;
}
can you check the serial monitor on what is the error you are getting.
Modem: SIM800 R14.18
SIM Unlocked
[8208] Initializing modem
[8480] Unlocking SIM
[8484] Waiting for network
[15877] Connecting to GPRS
[24046] Connected to GPRS
[24052] IP: 10.148.xxx.xxx
[24052] Connecting to mqtt.mydevices.com:1883
[26441] Connected
[27300] Publish: topic 4, channel 65534, value ESP32, subkey , key
[27322] Publish: topic 6, channel 65534, value XtensaLX6, subkey , key
[27346] Publish: topic 7, channel 65534, value 240000000, subkey , key
[27413] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
[27484] Publish: topic 8, channel 65534, value GSM, subkey , key
dist = 41
[29620] Publish: topic 1, channel 0, value 41, subkey , key
Boot number: 1
Wakeup was not caused by deep sleep: 0
Setup ESP32 to sleep for every 15 Seconds
Before Deep Sleep