Added a new packet handling needed to get mailbox events and send a mail when one is triggered. Added experimental work around because of a connection error when posting the post data to the server through HTTP. This need to be investigated !

This commit is contained in:
Th3maz1ng 2022-09-11 18:53:40 +02:00
parent fce92285e3
commit 9bcbf78d04

View File

@ -1,7 +1,7 @@
/**
* Author : Anatole SCHRAMM-HENRY
* Created the : 11/07/2021
* This is a quick and dirty firmewire to get save the data in the database
* This is a quick and dirty firmewire to receive and save the data in the database
*/
#include <SPI.h>
@ -23,7 +23,10 @@
#define RECV_CHECK 5000 //We test every 5s if we received something
#define WIFI_CHECK_TIMEOUT 20000
DataPacket p;
uint8_t payload[32] = {0};
DataPacket dp;
WeatherStationDataPacket wsdp;
MailboxDataPacket mdp;
RF24 NRF_1(NRF_1_CE, NRF_1_CS);
RF24 NRF_2(NRF_2_CE, NRF_2_CS);
@ -33,7 +36,7 @@ PCF8574 PCF(0x20);
WiFiEventHandler gotIp, lostConnection;
HttpClient DBHost(DB_SERVER, PATH);
Dictionary<DictionaryHelper::StringEntity> postData;
Dictionary<DictionaryHelper::StringEntity> weatherStationPostData, mailboxPostData;
const uint8_t ADDR[] = "WEST1", ADDR2[] = "ABCDE";
uint64_t timeStamp(0), irqSaver(0);
@ -66,17 +69,25 @@ void setup() {
WiFi.softAPdisconnect(true);
Serial.println("Connecting to the access point");
WiFi.begin(SSID, PWD);
//We initialize the dictionary
postData.add("accessCode", DictionaryHelper::StringEntity(ACCESS_CODE));
postData.add("deviceType", DictionaryHelper::StringEntity(DEV_TYPE));
postData.add("packetUID", DictionaryHelper::StringEntity(NULL));
postData.add("battery", DictionaryHelper::StringEntity(NULL));
postData.add("light", DictionaryHelper::StringEntity(NULL));
postData.add("bmp_tmp", DictionaryHelper::StringEntity(NULL));
postData.add("pressure", DictionaryHelper::StringEntity(NULL));
postData.add("humidity", DictionaryHelper::StringEntity(NULL));
postData.add("compensated_humidity", DictionaryHelper::StringEntity(NULL));
postData.add("htu_tmp", DictionaryHelper::StringEntity(NULL));
//We initialize the dictionary
//These keys are used to post data for the weather station :
weatherStationPostData.add("accessCode", DictionaryHelper::StringEntity(ACCESS_CODE));
weatherStationPostData.add("deviceType", DictionaryHelper::StringEntity(WEATHER_STATION_DEV_TYPE));
weatherStationPostData.add("packetUID", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("battery", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("light", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("bmp_tmp", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("pressure", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("humidity", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("compensated_humidity", DictionaryHelper::StringEntity(NULL));
weatherStationPostData.add("htu_tmp", DictionaryHelper::StringEntity(NULL));
//These keys are used to post data for the mailbox
mailboxPostData.add("accessCode", DictionaryHelper::StringEntity(ACCESS_CODE));
mailboxPostData.add("deviceType", DictionaryHelper::StringEntity(MAILBOX_DEV_TYPE));
mailboxPostData.add("packetUID", DictionaryHelper::StringEntity(NULL));
mailboxPostData.add("battery", DictionaryHelper::StringEntity(NULL));
mailboxPostData.add("event", DictionaryHelper::StringEntity(NULL));
Serial.printf("\nNRF 1 %s\n",NRF_1.begin() ? "started" : "error");
if(!NRF_1.isChipConnected())
@ -104,8 +115,6 @@ void setup() {
NRF_2.openReadingPipe(1, ADDR);
NRF_2.startListening();
memset(&p, 0, sizeof p);
//Setting the I2C pins and the PCF8574
Wire.begin(0,2);
Serial.printf("PCF %s\n", PCF.begin() ? "found" : "not found");
@ -139,12 +148,28 @@ void loop()
if(rx_ready)
{
Serial.printf("NRF 1 Received %u bytes with sig(%s) : \n",NRF_1.getPayloadSize(), NRF_1.testRPD()?"good":"bad");
NRF_1.read(&p, sizeof(p));
debugStruct(&p);
NRF_1.read(payload, sizeof(payload));
if(p.header == WEATHER_STATION)
memcpy(&dp, payload, sizeof(dp));
switch(dp.header)
{
insertIntoDB();
case WEATHER_STATION:
{
memcpy(&wsdp, payload, sizeof(wsdp));
debugStruct(&wsdp);
insertIntoDB(&wsdp);
}
break;
case CONNECTED_MAILBOX:
{
memcpy(&mdp, payload, sizeof(mdp));
debugStruct(&mdp);
insertIntoDB(&mdp);
}
break;
default:
break;
}
}
}
@ -166,12 +191,28 @@ void loop()
if(rx_ready)
{
Serial.printf("NRF 2 Received %u bytes with sig(%s) : \n",NRF_2.getPayloadSize(), NRF_2.testRPD()?"good":"bad");
NRF_2.read(&p, sizeof(p));
debugStruct(&p);
if(p.header == WEATHER_STATION)
NRF_2.read(payload, sizeof(payload));
memcpy(&dp, payload, sizeof(dp));
switch(dp.header)
{
insertIntoDB();
case WEATHER_STATION:
{
memcpy(&wsdp, payload, sizeof(wsdp));
debugStruct(&wsdp);
insertIntoDB(&wsdp);
}
break;
case CONNECTED_MAILBOX:
{
memcpy(&mdp, payload, sizeof(mdp));
debugStruct(&mdp);
insertIntoDB(&mdp);
}
break;
default:
break;
}
}
}
@ -230,36 +271,36 @@ void lostConnectionFunc(const WiFiEventStationModeDisconnected &event)
Serial.println("Lost connection, will try to reconnect ...");
}
void insertIntoDB(void)
void insertIntoDB(WeatherStationDataPacket *p)
{
char buffer[100] = "";
int result(0);
//We get the values and put it in our dictionary
sprintf(buffer ,"%u", p.id);
postData("packetUID")->setString(buffer);
sprintf(buffer ,"%u", p->id);
weatherStationPostData("packetUID")->setString(buffer);
sprintf(buffer, "%.5f", p.battery);
postData("battery")->setString(buffer);
sprintf(buffer, "%.5f", p->battery);
weatherStationPostData("battery")->setString(buffer);
sprintf(buffer, "%u", p.ldr);
postData("light")->setString(buffer);
sprintf(buffer, "%u", p->ldr);
weatherStationPostData("light")->setString(buffer);
sprintf(buffer, "%f", p.bmpTemp);
postData("bmp_tmp")->setString(buffer);
sprintf(buffer, "%f", p->bmpTemp);
weatherStationPostData("bmp_tmp")->setString(buffer);
sprintf(buffer, "%f", p.bmpPress);
postData("pressure")->setString(buffer);
sprintf(buffer, "%f", p->bmpPress);
weatherStationPostData("pressure")->setString(buffer);
sprintf(buffer, "%f", p.humidity);
postData("humidity")->setString(buffer);
sprintf(buffer, "%f", p->humidity);
weatherStationPostData("humidity")->setString(buffer);
sprintf(buffer, "%f", p.compensatedHumidity);
postData("compensated_humidity")->setString(buffer);
sprintf(buffer, "%f", p->compensatedHumidity);
weatherStationPostData("compensated_humidity")->setString(buffer);
sprintf(buffer, "%f", p.htuTemp);
postData("htu_tmp")->setString(buffer);
sprintf(buffer, "%f", p->htuTemp);
weatherStationPostData("htu_tmp")->setString(buffer);
if((result = DBHost.sendHttpQuery(HttpClient::HttpRequestMethod::POST, NULL, &postData)) == 0)
if((result = DBHost.sendHttpQuery(HttpClient::HttpRequestMethod::POST, NULL, &weatherStationPostData)) == 0)
{
Serial.println("Data posted successfully");
HttpClient::HTTP_CODE response = DBHost.isReplyAvailable(2000);
@ -268,13 +309,32 @@ void insertIntoDB(void)
else
{
Serial.printf("Failed to post data : error(%d)\n", result);
if(result == -125)
{
WiFi.disconnect();
WiFi.begin(SSID, PWD);
while(!WiFi.isConnected())
{
Serial.println("Reconnecting !");
}
if((result = DBHost.sendHttpQuery(HttpClient::HttpRequestMethod::POST, NULL, &weatherStationPostData)) == 0)
{
HttpClient::HTTP_CODE response = DBHost.isReplyAvailable(2000);
Serial.printf("Second send got response code : %u\n", response);
}
else
{
Serial.printf("Second send failed to post data : error(%d)\n", result);
}
}
}
DBHost.stop();
}
void debugStruct(DataPacket *p)
void debugStruct(WeatherStationDataPacket *p)
{
Serial.println("##############DATA##############");
Serial.println("##############WEATHER STATION DATA##############");
Serial.print("ID : ");
Serial.println(p->id);
@ -306,3 +366,70 @@ void debugStruct(DataPacket *p)
Serial.print(p->htuTemp);
Serial.println(" *C");
}
void insertIntoDB(MailboxDataPacket *p)
{
char buffer[100] = "";
int result(0);
//We get the values and put it in our dictionary
sprintf(buffer ,"%u", p->id);
mailboxPostData("packetUID")->setString(buffer);
sprintf(buffer, "%.5f", p->battery);
mailboxPostData("battery")->setString(buffer);
switch(p->mailbox_event)
{
case MAILBOX_LETTER:
mailboxPostData("event")->setString("LETTER");
break;
case MAILBOX_PACKAGE:
mailboxPostData("event")->setString("PACKAGE");
break;
case MAILBOX_COLLECTED:
mailboxPostData("event")->setString("COLLECTED");
break;
default:
break;
}
if((result = DBHost.sendHttpQuery(HttpClient::HttpRequestMethod::POST, NULL, &mailboxPostData)) == 0)
{
Serial.println("Data posted successfully");
HttpClient::HTTP_CODE response = DBHost.isReplyAvailable(5000); //We increase the timeout because sending a mail on the server side takes some time.
Serial.printf("Got response code : %u\n", response);
}
else
{
Serial.printf("Failed to post data : error(%d)\n", result);
}
DBHost.stop();
}
void debugStruct(MailboxDataPacket *p)
{
Serial.println("##############MAILBOX DATA##############");
Serial.print("ID : ");
Serial.println(p->id);
Serial.print("HEADER : ");
Serial.println(p->header);
Serial.printf("BATT : %.5f V\n", p->battery);
Serial.print("EVENT : ");
switch(p->mailbox_event)
{
case MAILBOX_LETTER:
Serial.println("LETTER");
break;
case MAILBOX_PACKAGE:
Serial.println("PACKAGE");
break;
case MAILBOX_COLLECTED:
Serial.println("COLLECTED");
break;
default:
break;
}
}