Compare commits

..

5 Commits

2 changed files with 31 additions and 12 deletions

View File

@ -1,7 +1,7 @@
/** /**
* Author : Anatole SCHRAMM-HENRY * Author : Anatole SCHRAMM-HENRY
* Created the : 11/07/2021 * Created the : 11/07/2021
* Last updated : 04/11/2025 * Last updated : 21/12/2025
* This is a quick and dirty gateway firmware to receive and save data from sensors to a database * This is a quick and dirty gateway firmware to receive and save data from sensors to a database
*/ */
@ -104,7 +104,7 @@ IRAM_ATTR void PCFIRQHandler(void *p)
PCF.digitalReadAll(pinStates); PCF.digitalReadAll(pinStates);
*(boolean *)p = true; *(boolean *)p = true;
/* If the IRQ pin is the DIO1, manually call the LoRa IRQ handler */ /* If the IRQ pin is the DIO0, manually call the LoRa IRQ handler */
if(pinStates[7] == HIGH) if(pinStates[7] == HIGH)
{ {
LoRa.handleDio0Rise(); LoRa.handleDio0Rise();
@ -117,6 +117,7 @@ void setup()
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY); Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
delay(1000); delay(1000);
Serial.println("\nSetup begin"); Serial.println("\nSetup begin");
Serial.printf("Size of mailbox data packets: %d\nSize of weather data packets: %d\n", sizeof mdp, sizeof wsdp);
//We set the WiFi part up : //We set the WiFi part up :
gotIp = WiFi.onStationModeGotIP(&(gotIpFunc)); gotIp = WiFi.onStationModeGotIP(&(gotIpFunc));
lostConnection = WiFi.onStationModeDisconnected(&(lostConnectionFunc)); lostConnection = WiFi.onStationModeDisconnected(&(lostConnectionFunc));
@ -220,19 +221,20 @@ void loop()
{ {
//Payload already retrieved in LoRa module IRQ or in the previous code part //Payload already retrieved in LoRa module IRQ or in the previous code part
memcpy(&packetHeader, payload, sizeof(packetHeader)); memcpy(&packetHeader, payload, sizeof(packetHeader));
//printPayloadBytes(payload, sizeof payload);
switch(packetHeader.header) switch(packetHeader.header)
{ {
case WEATHER_STATION: case WEATHER_STATION:
{ {
memcpy(&wsdp, payload, sizeof(wsdp)); memcpy(&wsdp, payload, sizeof(wsdp));
debugStruct(&wsdp); printStruct(&wsdp);
insertIntoDBError = insertIntoDB(&wsdp); insertIntoDBError = insertIntoDB(&wsdp);
} }
break; break;
case CONNECTED_MAILBOX: case CONNECTED_MAILBOX:
{ {
memcpy(&mdp, payload, sizeof(mdp)); memcpy(&mdp, payload, sizeof(mdp));
debugStruct(&mdp); printStruct(&mdp);
insertIntoDBError = insertIntoDB(&mdp); insertIntoDBError = insertIntoDB(&mdp);
} }
break; break;
@ -278,14 +280,14 @@ void loop()
case WEATHER_STATION: case WEATHER_STATION:
{ {
memcpy(&wsdp, payload, sizeof(wsdp)); memcpy(&wsdp, payload, sizeof(wsdp));
debugStruct(&wsdp); printStruct(&wsdp);
insertIntoDBError = insertIntoDB(&wsdp); insertIntoDBError = insertIntoDB(&wsdp);
} }
break; break;
case CONNECTED_MAILBOX: case CONNECTED_MAILBOX:
{ {
memcpy(&mdp, payload, sizeof(mdp)); memcpy(&mdp, payload, sizeof(mdp));
debugStruct(&mdp); printStruct(&mdp);
insertIntoDBError = insertIntoDB(&mdp); insertIntoDBError = insertIntoDB(&mdp);
} }
break; break;
@ -323,14 +325,14 @@ void loop()
case WEATHER_STATION: case WEATHER_STATION:
{ {
memcpy(&wsdp, payload, sizeof(wsdp)); memcpy(&wsdp, payload, sizeof(wsdp));
debugStruct(&wsdp); printStruct(&wsdp);
insertIntoDBError = insertIntoDB(&wsdp); insertIntoDBError = insertIntoDB(&wsdp);
} }
break; break;
case CONNECTED_MAILBOX: case CONNECTED_MAILBOX:
{ {
memcpy(&mdp, payload, sizeof(mdp)); memcpy(&mdp, payload, sizeof(mdp));
debugStruct(&mdp); printStruct(&mdp);
insertIntoDBError = insertIntoDB(&mdp); insertIntoDBError = insertIntoDB(&mdp);
} }
break; break;
@ -454,7 +456,7 @@ HttpClient::HttpQueryStatus insertIntoDB(WeatherStationDataPacket *p)
return result; return result;
} }
void debugStruct(WeatherStationDataPacket *p) void printStruct(WeatherStationDataPacket *p)
{ {
Serial.println("##############WEATHER STATION DATA##############"); Serial.println("##############WEATHER STATION DATA##############");
Serial.print("ID : "); Serial.print("ID : ");
@ -533,7 +535,7 @@ HttpClient::HttpQueryStatus insertIntoDB(MailboxDataPacket *p)
return result; return result;
} }
void debugStruct(MailboxDataPacket *p) void printStruct(MailboxDataPacket *p)
{ {
Serial.println("##############MAILBOX DATA##############"); Serial.println("##############MAILBOX DATA##############");
Serial.print("ID : "); Serial.print("ID : ");
@ -544,7 +546,7 @@ void debugStruct(MailboxDataPacket *p)
Serial.printf("BATT : %.5f V\n", p->battery); Serial.printf("BATT : %.5f V\n", p->battery);
Serial.print("EVENT : "); Serial.printf("EVENT : %d\n", p->mailbox_event);
switch(p->mailbox_event) switch(p->mailbox_event)
{ {
case MAILBOX_LETTER: case MAILBOX_LETTER:
@ -563,3 +565,20 @@ void debugStruct(MailboxDataPacket *p)
break; break;
} }
} }
void printPayloadBytes(const uint8_t *payload, size_t payload_size)
{
if(!payload || !payload_size)return;
uint8_t buffer[32];
size_t safeSize = (payload_size > sizeof(buffer)) ? sizeof(buffer) : payload_size;
memset(buffer, 0xFF, sizeof buffer);
memcpy(buffer, payload, safeSize);
if(safeSize < payload_size)
Serial.println("/!\\ Payload size was truncated /!\\");
for(size_t i = 0; i < safeSize; i++)
{
Serial.printf("[%d]=0x%02X\n",i ,buffer[i]);
}
}

View File

@ -30,7 +30,7 @@ typedef enum
MAILBOX_COLLECTED, MAILBOX_COLLECTED,
MAILBOX_TEST, MAILBOX_TEST,
MAILBOX_UNKNOWN, MAILBOX_UNKNOWN,
} MAILBOX_EVENT_e; } __attribute__((__packed__)) MAILBOX_EVENT_e;
typedef struct typedef struct
{ {