QBluetoothSocketPrivate::_q_readNotify() 18 error: -1 "Connection timed out" - qt

I am using qt on an embedded board for communication with an IOS device. The embedded board is runnning qt 5.10 and is in peripheral mode. I am getting the above-mentioned error sometimes and after that Bluetooth Error takes place, with error signal as follows
Error: QLowEnergyController::Error(UnknownError)
How can I know the source of this issue? The document says nothing about it.
The code is based on qt heart rate server example
https://doc.qt.io/qt-5/qtbluetooth-heartrate-server-example.html
My Class for Bluetooth
#include "bluetooth.h"
#define log qDebug()<<"["<<__DATE__<<":"<<QTime::currentTime().toString()<<"]"<<"["<<__LINE__<<"]"<<"["<<__FILE__<<"] "
Bluetooth::Bluetooth(mainprocess *main,QObject *parent) :QObject(parent), mainObject(main)
{
initializeBluetooth();
}
void Bluetooth::initializeBluetooth(){
try{
log<<"Initializing Bluetooth...\n";
//! [Advertising Data]
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setLocalName("AtlasGo");
advertisingData.setServices(QList<QBluetoothUuid>() <<QBluetoothUuid::Atlas);
// For IOS its readonly and for Atlas its write only
ReadCharacteristicData.setUuid(QBluetoothUuid::ReadCharacteristic);
ReadCharacteristicData.setProperties(QLowEnergyCharacteristic::Notify);
ReadCharacteristicDesc.setUuid(QBluetoothUuid::ClientCharacteristicConfiguration);
ReadCharacteristicDesc.setValue(QByteArray(2,0));
ReadCharacteristicData.addDescriptor(ReadCharacteristicDesc);
// For IOS its write only characteristic and for Atlas its read only
WriteCharactersiticData.setUuid(QBluetoothUuid::WriteCharacteristic);
WriteCharactersiticData.setProperties(QLowEnergyCharacteristic::Write |QLowEnergyCharacteristic::Notify);
WriteCharactersiticDesc.setUuid(QBluetoothUuid::AtlasDescriptor);
WriteCharactersiticDesc.setValue(QByteArray::fromHex("Write").toHex());
WriteCharactersiticData.addDescriptor(WriteCharactersiticDesc);
// For IOS its readonly and for Atlas its write only
ConnectivityData.setUuid(QBluetoothUuid::Connectivity);
ConnectivityData.setProperties(QLowEnergyCharacteristic::Notify);
ConnectivityDesc.setUuid(QBluetoothUuid::ClientCharacteristicConfiguration);
ConnectivityDesc.setValue(QByteArray(2, 0));
ConnectivityData.addDescriptor(ConnectivityDesc);
// For IOS its write only characteristic and for Atlas its read only
TrackerData_Data.setUuid(QBluetoothUuid::TrackingData);
TrackerData_Data.setProperties(QLowEnergyCharacteristic::Notify);
TrackerDesc.setUuid(QBluetoothUuid::ClientCharacteristicConfiguration);
TrackerDesc.setValue(QByteArray(2, 0));
TrackerData_Data.addDescriptor(TrackerDesc);
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid::Atlas);
// Adding characteristic to our service
serviceData.addCharacteristic(ReadCharacteristicData);
serviceData.addCharacteristic(WriteCharactersiticData);
serviceData.addCharacteristic(ConnectivityData);
serviceData.addCharacteristic(TrackerData_Data);
leController.reset(QLowEnergyController::createPeripheral()); // We are resetting only the pointer here (leController is a scoped pointer)
service.reset(leController->addService(serviceData)); // Resetting only the pointer here (service is a scoped pointer)
while ( localdevice.hostMode()==QBluetoothLocalDevice::HostPoweredOff){
log<<"Host is powered off";
system("sudo rfkill unblock 3");
system("sudo rfkill unblock 0");
if(localdevice.hostMode()==QBluetoothLocalDevice::HostConnectable)
break;
}
connect(&heartbeatTimer, &QTimer::timeout,mainObject, &mainprocess::TickProvider);
connect(leController.data(), &QLowEnergyController::disconnected,mainObject,&mainprocess::reconnect);
connect(leController.data(), &QLowEnergyController::connected,mainObject,&mainprocess::bleConnected);
connect(service.data(),&QLowEnergyService::characteristicChanged,mainObject,&mainprocess::parseResponse);
connect(leController.data(), static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this,&Bluetooth::reconnectOnError);
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,advertisingData);
// We have to check if advertising succeeded ot not. If there was an advertising error we will
// try to reinitialize our bluetooth service
while (leController->state()!= QLowEnergyController::AdvertisingState){
resetBluetoothService();
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,advertisingData);
}
// Timer for periodically sending information to IOS
// Tracking data
// Connectivity information
log<<"Starting the Tick Timer Timer";
heartbeatTimer.start(66);
// heartbeatTimer.start(200); Just for testing
}
catch(std::exception e)
{
log<<"---------------------";
log<<"Bluetooth Crashed";
log<<"---------------------";
log<<"Bluetooth Initialization failed";
log<<e.what();
emit recoverBluetooth();
}
}
void Bluetooth::reconnectOnDisconnect(){
try{
log<<"Bluetooth Device Disconnected";
log<<"Resetting Bluetooth bt.service";
//resetBluetoothService();
if(leController->state()!= QLowEnergyController::ConnectedState){
//leController->disconnectFromDevice();
leController.reset(QLowEnergyController::createPeripheral());
service.reset(leController->addService(serviceData));
}
if (!service.isNull()){
if(leController->state()!=QLowEnergyController::AdvertisingState && leController->state()!=QLowEnergyController::ConnectedState){
leController->startAdvertising(QLowEnergyAdvertisingParameters(),advertisingData,advertisingData); //, advertisingData);
}
// We have to check if advertising succeeded ot not. If there was an advertising error we will
// try to reinitialize our bluetooth service
while (leController->state()!= QLowEnergyController::AdvertisingState && leController->state()!= QLowEnergyController::ConnectedState ){
resetBluetoothService();
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,advertisingData);
}
connect(service.data(),&QLowEnergyService::characteristicChanged,mainObject,&mainprocess::parseResponse);
connect(leController.data(), &QLowEnergyController::disconnected,mainObject,&mainprocess::reconnect);
connect(leController.data(), &QLowEnergyController::disconnected,mainObject,&mainprocess::stopTracker);
connect(leController.data(), &QLowEnergyController::connected,mainObject,&mainprocess::bleConnected);
connect(leController.data(), static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this,&Bluetooth::reconnectOnError);
}
}
catch(std::exception e){
log<<"---------------------";
log<<"Bluetooth Crashed";
log<<"---------------------";
log<<"Error while handling bluetooth Disconnect";
log<<e.what();
emit recoverBluetooth();
}
}
void Bluetooth::reconnectOnError(QLowEnergyController::Error error){
try{
// Have to check this error because of Qt's bug. It throws error when remote device disconnects
if(error!=QLowEnergyController::RemoteHostClosedError){
log<<"Bluetooth Error !!";
log<<"Resetting Bluetooth bt.service";
log<<"Error: "<<error;
// resetBluetoothService();
if(leController->state()!= QLowEnergyController::ConnectedState){
//leController->disconnectFromDevice();
leController.reset(QLowEnergyController::createPeripheral());
service.reset(leController->addService(serviceData));
}
if (service!=nullptr){
if(leController->state()!=QLowEnergyController::AdvertisingState && leController->state()!=QLowEnergyController::ConnectedState){
leController->startAdvertising(QLowEnergyAdvertisingParameters(),advertisingData,advertisingData); //, advertisingData);
}
// We have to check if advertising succeeded or not. If there was an advertising error we will
// try to reinitialize our bluetooth service
while (leController->state()!= QLowEnergyController::AdvertisingState && leController->state()!= QLowEnergyController::ConnectedState ){
//resetBluetoothService();
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,advertisingData);
}
// Connections
connect(service.data(),&QLowEnergyService::characteristicChanged,mainObject,&mainprocess::parseResponse);
connect(service.data(), QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),[=](QLowEnergyService::ServiceError newError){
log<<"---------------------";
log<<"Bluetooth Service Error:"<<newError;
log<<"---------------------";
});
connect(leController.data(), &QLowEnergyController::disconnected,mainObject,&mainprocess::reconnect);
connect(leController.data(), &QLowEnergyController::connected,mainObject,&mainprocess::bleConnected);
connect(leController.data(), &QLowEnergyController::disconnected,mainObject,&mainprocess::stopTracker);
connect(leController.data(), static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this,&Bluetooth::reconnectOnError);
}
}
}
catch(std::exception e){
log<<"---------------------";
log<<"Bluetooth Crashed";
log<<"---------------------";
log<<"Error while reconnecting after remote device disconnection";
log<<e.what();
emit recoverBluetooth();
}
}
void Bluetooth::resetBluetoothService(){
try{
log<<"Trying to reset bluetooth";
if(leController->state()==QLowEnergyController::AdvertisingState)
leController->stopAdvertising();
// Todo: Use timer
system("sudo service bluetooth stop");
QThread::sleep(1);
system("sudo service bluetooth start");
QThread::sleep(1);
log<<"Bluetooth Reset complete";
}
catch(std::exception e){
log<<"---------------------";
log<<"Bluetooth Crashed";
log<<"---------------------";
log<<"Error while resetting bluetooth";
log<<e.what();
emit recoverBluetooth();
}
}

Related

Connecting and adding Arduino MKR NBIoT 1500 board to the cloud server

I tried connecting the Arduino MKR NBIoT 1500 board to the Azure IoT Hub but wasn't successful. The board was able to connect to the cellular network and I tried to connect to Azure IoT Hub using MQTT but getting an error “-2”. I have also tried Google IoT and AWS IoT core and I'm still having the same error. I would appreciate if anyone could give me a feedback on what to do to be able to solve the problem. Thank you
Azure IoT Hub NB
This sketch securely connects to an Azure IoT Hub using MQTT over NB IoT/LTE Cat M1.
It uses a private key stored in the ATECC508A and a self signed public
certificate for SSL/TLS authetication.
It publishes a message every 5 seconds to "devices/{deviceId}/messages/events/" topic
and subscribes to messages on the "devices/{deviceId}/messages/devicebound/#"
topic.
The circuit:
- MKR NB 1500 board
- Antenna
- SIM card with a data plan
- LiPo battery
The following tutorial on Arduino Project Hub can be used
to setup your Azure account and the MKR board:
https://create.arduino.cc/projecthub/Arduino_Genuino/securely-connecting-an-arduino-nb-1500-to-azure-iot-hub-af6470
This example code is in the public domain.
*/
#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>
#include <utility/ECCX08SelfSignedCert.h>
#include <ArduinoMqttClient.h>
#include <MKRNB.h>
#include "arduino_secrets.h"
/////// Enter your sensitive data in arduino_secrets.h
const char pinnumber[] = SECRET_PINNUMBER;
const char broker[] = SECRET_BROKER;
String deviceId = SECRET_DEVICE_ID;
NB nbAccess=true;
GPRS gprs;
NBClient nbClient; // Used for the TCP socket connection
BearSSLClient sslClient(nbClient); // Used for SSL/TLS connection, integrates with ECC508
MqttClient mqttClient(sslClient);
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!ECCX08.begin()) {
Serial.println("No ECCX08 present!");
while (1);
}
// reconstruct the self signed cert
ECCX08SelfSignedCert.beginReconstruction(0, 8);
ECCX08SelfSignedCert.setCommonName(ECCX08.serialNumber());
ECCX08SelfSignedCert.endReconstruction();
// Set a callback to get the current time
// used to validate the servers certificate
ArduinoBearSSL.onGetTime(getTime);
// Set the ECCX08 slot to use for the private key
// and the accompanying public certificate for it
sslClient.setEccSlot(0, ECCX08SelfSignedCert.bytes(), ECCX08SelfSignedCert.length());
// Set the client id used for MQTT as the device id
mqttClient.setId(deviceId);
// Set the username to "<broker>/<device id>/api-version=2018-06-30" and empty password
String username;
username += broker;
username += "/";
username += deviceId;
username += "/api-version=2018-06-30";
mqttClient.setUsernamePassword(username, "");
// Set the message callback, this function is
// called when the MQTTClient receives a message
mqttClient.onMessage(onMessageReceived);
}
void loop() {
if (nbAccess.status() != NB_READY || gprs.status() != GPRS_READY) {
connectNB();
}
if (!mqttClient.connected()) {
// MQTT client is disconnected, connect
connectMQTT();
}
// poll for new MQTT messages and send keep alives
mqttClient.poll();
// publish a message roughly every 5 seconds.
if (millis() - lastMillis > 5000) {
lastMillis = millis();
publishMessage();
}
}
unsigned long getTime() {
// get the current time from the cellular module
return nbAccess.getTime();
//return 1583929365;
}
void connectNB() {
Serial.println("Attempting to connect to the cellular network");
while ((nbAccess.begin(pinnumber) != NB_READY) ||
(gprs.attachGPRS() != GPRS_READY)) {
// failed, retry
Serial.print(".");
delay(1000);
}
Serial.println("You're connected to the cellular network");
Serial.println();
}
void connectMQTT() {
Serial.print("Attempting to MQTT broker: ");
Serial.print(broker);
Serial.println(" ");
while (!mqttClient.connect(broker, 8883)) {
// failed, retry
Serial.print(".");
Serial.println(mqttClient.connectError());
delay(5000);
}
Serial.println();
Serial.println("You're connected to the MQTT broker");
Serial.println();
// subscribe to a topic
mqttClient.subscribe("devices/" + deviceId + "/messages/devicebound/#");
}
void publishMessage() {
Serial.println("Publishing message");
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage("devices/" + deviceId + "/messages/events/");
mqttClient.print("hello ");
mqttClient.print(millis());
mqttClient.endMessage();
}
void onMessageReceived(int messageSize) {
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClient.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");
// use the Stream interface to print the contents
while (mqttClient.available()) {
Serial.print((char)mqttClient.read());
}
Serial.println();
Serial.println();
}
// NB settings
#define SECRET_PINNUMBER ""
// Fill in the hostname of your Azure IoT Hub broker
#define SECRET_BROKER "ArduinoProjectHub.azure-devices.net"
// Fill in the device id
#define SECRET_DEVICE_ID "MKRNB1500"

ESP32 Arduino BLE DeepSleep

I am working on a project involving 2 ESP32 Wemos D1 Mini boards. I am using the BLE feature to transmit a sensor reading from the "server" to the "client". I use a Characteristic Notify to the client which receives the sensor reading. If I want to implement deep sleep functionality to the server, what would happen to the client? Does the client also have to reset at some point?
Also, is it advisable to use Characteristic.Notify in a deep sleep scenario?
Thanks.
Server code:
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
#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;
DHT dht(DHTPIN, DHTTYPE);
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 50;
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
//Function that prints the reason by which ESP32 has been awaken from sleep
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
}
}
void setup() {
Serial.begin(115200);
Serial.println(F("initating DHT22..."));
dht.begin();
// Create the BLE Device
BLEDevice::init("UART"); // Name must not be longer than 5 chars!!!
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY|BLECharacteristic::PROPERTY_READ|BLECharacteristic::PROPERTY_WRITE
);
BLE2902 *desc = new BLE2902();
desc->setNotifications(true);
pCharacteristic->addDescriptor(desc);
// Start the service
pService->start();
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
// Start advertising
pServer->getAdvertising()->start();
Serial.println(pService->getUUID().toString().c_str());
Serial.println("Waiting a client connection to notify...");
if (deviceConnected) {
float f = dht.readTemperature(true);
char fStr[10];
sprintf(fStr, "%4.4f", f);
Serial.print("Temperature reading: ");
Serial.println(fStr);
Serial.printf("*** Sent Value: %d ***\n", fStr);
pCharacteristic->setValue(fStr);
pCharacteristic->notify();
//Set timer to 5 seconds
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");
//Go to sleep now
esp_deep_sleep_start();
}
//delay(60000);
}
void loop() {}
Client code:
#include "BLEDevice.h"
#include <WiFi.h>
// The remote service we wish to connect to.
static BLEUUID serviceUUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
static BLEUUID charUUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
const char* ssid = "Kings";
const char* password = "GoCanada";
const char* host = "menezes-service.herokuapp.com";
WiFiClient client;
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
//std::string value = pBLERemoteCharacteristic->readValue();
byte buffer[42];
Serial.print("The characteristic value sent was: ");
//Serial.println(pBLERemoteCharacteristic->readValue().c_str());
//Serial.println(pBLERemoteCharacteristic->readUInt8());
std::string farhenheight = pRemoteCharacteristic->readValue();
Serial.print("Farheinheight: ");
Serial.println(farhenheight.c_str());
Serial.println(F("Posting to api!"));
Serial.println();
Serial.println("closing connection");
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("connected again ... ");
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer(BLEAddress pAddress) {
Serial.print("Forming a connection to ");
Serial.println(pAddress.toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server.
pClient->connect(pAddress);
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
Serial.println(pRemoteService->toString().c_str());
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return false;
}
Serial.println(" - Found our service");
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(charUUID.toString().c_str());
return false;
}
Serial.println(" - Found our characteristic");
pRemoteCharacteristic->registerForNotify(notifyCallback);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
//Serial.print(advertisedDevice.haveServiceUUID());
if(advertisedDevice.haveServiceUUID()){
Serial.println(advertisedDevice.getServiceUUID().toString().c_str());
}
// We have found a device, let us now see if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
//
Serial.print("Found our device! address: ");
advertisedDevice.getScan()->stop();
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
doConnect = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup() {
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
} // End of setup.
// This is the Arduino main loop function.
void loop() {
if (doConnect == true) {
if (connectToServer(*pServerAddress)) {
Serial.println("We are now connected to the BLE Server.");
connected = true;
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
//doConnect = false;
}
if (connected == false){
BLEDevice::getScan()->start(0);
}
else{
doConnect = false;
}
delay(1000); // Delay a second between loops.
} // End of loop
Please see answers to your questions below:-
If I want to implement deep sleep functionality to the server, what would happen to the client? Does the client also have to reset at some point?
No the client does not have to reset, but you may have to reconnect to the server because in deep sleep the BLE connection is lost. If you want the connection to be established automatically as soon as the server wakes up, then you have to update the client code so that it continuously attempts to reconnect to the server. This way, as soon as the server wakes up, the client would connect to it and continue receiving notifications.
Also, is it advisable to use Characteristic.Notify in a deep sleep scenario?
In deep sleep the CPU will be off, therefore you will not be able to send notifications in this state. In this mode you will only be able to wake up the CPU through a timer or an external peripheral (e.g. touch pins). Please see the link below for more information:-
https://lastminuteengineers.com/esp32-deep-sleep-wakeup-sources/
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html
https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
I hope this helps.

Sending reliable HTTP request with Arduino Wifi Client

When a sensor on my Arduino MKR1000 is triggered, I send an email:
//....
WiFiClient client;
char server[] = "www.myserver.com";
//....
void setup() {
//... Connect to wifi ....
}
void loop() {
// when sensor triggered =>
if (client.connect(server, 80)) {
Serial.println("Sending email");
client.print("GET /WaterOff/sendGridCall.php");
client.print("?valve=");
client.print(valve);
client.print("&position=");
client.print(position);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("User-Agent: Arduino");
client.println("Connection: close");
client.println();
}
delay(6000);
}
However, it only works the first couple of times ("Sending email" is printed) and after that it does not execute the request ("Sending email" is not printed). Am I supposed to take some additional steps to make it reliable.
You never close the connection. But blocking to wait for email confirmation can take forever. You should consider a small state machine for your network connection to work.
void loop()
{
// when sensor triggered =>
if (client.connect(server, 80)) { // You connect to client, but it's already
// connected the second time around
// send ...
// client() should wait for response, and client.disconnect()
}
// ...
delay(6000); // tip: avoid blocking the main loop, this blocks your ability to
// read your sensor while sending emails.
}
A state machine for your app could look like this:
enum EmailState {
emIdle, emSendMail, emCheckMailSent, emEmailOK, emEmailError,
};
EmailState email_state;
unsigned long long mail_timer; // can be shared between states.
// small state handlers that do a bit at a time,
// check for an event, wait on some delay (use millis())
// avoid blocking the instruction flow as much as possible.
static void on_emIdle()
{
// read sensor, check timer?
email_state = (triggered) ? emSendMail : emIdle;
}
static void on_sendMail()
{
if (client.connect())
{
// ... prints
email_state = emCheckMailSent;
return;
}
email_state = emEmailError;
}
static void on_emCheckMailSent()
{
// receive HTTP response... can have a long timeout without blocking.
email_state = emCheckMailSent;
// or emEmailOK or emEmailError or retry emSendMail
}
static void on_emEmailOK()
{
if (/*client is connected?*/)
client.disconnect();
if (!mail_timer)
mail_timer = millis();
if (millis() - mail_timer < 6000) // stay in this state for 6 seconds...
{
email_state = emEmailOK;
return;
}
mail_timer = 0; // always reset the timer so it can be used by other states
email_state = emIdle;
}
static void on_emEmailError()
{
if (/*client is connected?*/)
client.disconnect();
// keep a log ?
email_state = emIdle; // or another timer
}
//...
void loop()
{
// update readings, manage other things...
// ...
switch(email_state)
{
default:
case emIdle: on_emIdle(); break;
case emSendMail: on_emSendMail(); break;
case emCheckMailSent: on_emCheckMailSent(); break;
case emEmailOK: on_emEmailOK(); break;
case emEmailError: on_emEmailError(); break;
}
}

Arduino Uno + ESP8266 reading server's response

I'm sending a GET request from Arduino Uno using ESP8266. The request is sent, but I'm unable to print the received response.
I'm using code from https://elementztechblog.wordpress.com/2015/05/13/esp8266-based-temperature-data-logger-using-arduino/
I have changed the code for connecting to my server and I can see the GET request is received on my server's log.
I tried putting
while (ser.available())
{
Serial.write(ser.read());
}
after the Serial.println("AT+CIPCLOSE"); statement.
BUT I'm not getting anything on Serial monitor after "AT+CIPCLOSE"
EDIT:
Here's my entire code:
// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // TX, RX
// this runs once
void setup()
{
// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(9600);
// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop()
{
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "192.168.0.25";
cmd += "\",3000";
ser.println(cmd);
if(ser.find("Error"))
{
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /api/myservice";
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else
{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{
// char c = ser.read();
Serial.write(ser.read());
// if (c == '\r') Serial.print('\n');
}
}
delay(1000);
}
ESP Details:
ESP-01
AT version: 0.40.0.0
If you are only struggling to read a response then the answer is simple;
You are closing the TCP connection before you try to read:
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{
Move the reading while into the above block just beneath ser.print(getStr); but add a delay between the two as well.

How to read SMS from arduino Gboard

// libraries
#include <GSM.h>
// PIN Number
//#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
char remoteNumber[20]; // Holds the emitting number
void setup()
{
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Receiver");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin()==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop()
{
char c;
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(remoteNumber, 20);
Serial.println(remoteNumber);
// This is just an example of message disposal
// Messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}
// Read message bytes and print them
while(c=sms.read())
Serial.print(c);
Serial.println("\nEND OF MESSAGE");
// delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}
Error: GSM_SMS does not a name type....
so i don't understand what actually error is......plz give me a proper answer.
exactly, i want to read SMS using arduino Gboard and doing led on or off through mobile.
Try to download the last arduino software arduino-1.0.4 and check again the code.
Please try 1.0.4 (http://arduino.cc/en/main/software) , 1.5.2 is beta. If i wrong sorry...

Resources