diff --git a/src/tests/definition.h b/src/tests/definition.h new file mode 100644 index 0000000..b67eca3 --- /dev/null +++ b/src/tests/definition.h @@ -0,0 +1,20 @@ +#ifndef DEFINITION_H +#define DEFINITION_H + +#include "packet_format.h" + +//Payload structure +typedef struct +{ + uint16_t id; + HEADER_e header : 6; + unsigned int ldr : 10; + float battery; + float bmpTemp; + float bmpPress; + float humidity; + float compensatedHumidity; + float htuTemp; +} DataPacket __attribute__((__packed__)); + +#endif //DEFINITION_H diff --git a/src/tests/packet_format.h b/src/tests/packet_format.h new file mode 100644 index 0000000..5b35254 --- /dev/null +++ b/src/tests/packet_format.h @@ -0,0 +1,6 @@ +#ifndef PACKET_FORMAT_H +#define PACKET_FORMAT_H + +enum HEADER_e {WEATHER_STATION = 0}; + +#endif //PACKET_FORMAT_H diff --git a/src/tests/tests.ino b/src/tests/tests.ino new file mode 100644 index 0000000..078b9f0 --- /dev/null +++ b/src/tests/tests.ino @@ -0,0 +1,149 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created the : 13/06/2021 + * This TEST program aims to verify all the basic functions of the gateway by displaying some output to a serial console : + * - Testing that both NRF work correctly + * - Testing the LED status lights + * - Testing the NRF IRQs detection with the PCF8574 + */ + +#include +#include +#include +#include "RF24.h" +#include "definition.h" + + +#define NRF_1_CE 15 //chip enable +#define NRF_2_CE 16 //chip enable + +#define NRF_1_CS 5 //chip select +#define NRF_2_CS 4 //chip select + +#define CHAN 108 //0-125 +#define RECV_CHECK 100 //We test every 100 ms if we received something +#define WIFI_CHECK_TIMEOUT 20000 + +DataPacket p; + +RF24 NRF_1(NRF_1_CE, NRF_1_CS); +RF24 NRF_2(NRF_2_CE, NRF_2_CS); +PCF8574 PCF(0x20); + +const uint8_t ADDR[] = "WEST1"; +uint64_t timeStamp(0), recvSlot(0), packetTimeout(0); +uint32_t freeMem(0); +uint16_t biggestContigMemBlock(0); +uint8_t frag(0); + +void setup() { + //We do not need to read on the serial bus + Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY); + Serial.println("Setup begin"); + //To prevent wear and tear on the flash mem + WiFi.persistent(false); + WiFi.disconnect(true); + WiFi.softAPdisconnect(true); + + Serial.printf("NRF 1 %s\n",NRF_1.begin() ? "started" : "error"); + if(!NRF_1.isChipConnected()) + Serial.println("NRF 1 is missing"); + else + Serial.println("NRF 1 is detected"); + + NRF_1.setChannel(CHAN); + NRF_1.setPALevel(RF24_PA_MIN); + NRF_1.setDataRate(RF24_250KBPS); + NRF_1.setRetries(8,15); + NRF_1.openReadingPipe(1, ADDR); + NRF_1.startListening(); + + Serial.printf("NRF 2 %s\n",NRF_2.begin() ? "started" : "error"); + if(!NRF_2.isChipConnected()) + Serial.println("NRF 2 is missing"); + else + Serial.println("NRF 2 is detected"); + + NRF_2.setChannel(CHAN); + NRF_2.setPALevel(RF24_PA_MIN); + NRF_2.setDataRate(RF24_250KBPS); + NRF_2.setRetries(8,15); + 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"); + PCF.pinMode(PCF8574::P2, OUTPUT); + PCF.pinMode(PCF8574::P3, OUTPUT); + PCF.pinMode(PCF8574::P4, OUTPUT); + Serial.println("End setup"); +} + +void loop() { + // put your main code here, to run repeatedly: + if(millis() - recvSlot > RECV_CHECK) + { + if(NRF_1.available()) //Then we read the incoming data + { + NRF_1.read(&p, sizeof(p)); + Serial.printf("NRF 1 Received : \n"); + debugStruct(&p); + } + if(NRF_2.available()) //Then we read the incoming data + { + NRF_2.read(&p, sizeof(p)); + Serial.printf("NRF 2 Received : \n"); + debugStruct(&p); + } + recvSlot = millis(); + } + + //Here we check if we are still connected + if(millis() - timeStamp > WIFI_CHECK_TIMEOUT) + { + PCF.togglePin(PCF8574::P2); + PCF.togglePin(PCF8574::P3); + PCF.togglePin(PCF8574::P4); + ESP.getHeapStats(&freeMem, &biggestContigMemBlock, &frag); + printf("Memory Info :\n - Free Mem > %u\n - Heap frag > %u\n - Max block > %u\n", freeMem, frag, biggestContigMemBlock); + timeStamp = millis(); + } +} + +void debugStruct(DataPacket *p) +{ + Serial.println("##############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("LDR : "); + Serial.println(p->ldr); + + Serial.print("BMP TEMP : "); + Serial.print(p->bmpTemp); + Serial.println(" *C"); + + Serial.print("BMP PRESS : "); + Serial.print(p->bmpPress); + Serial.println(" Pa"); + + Serial.print("HUM : "); + Serial.print(p->humidity); + Serial.println(" %"); + + Serial.print("COM HUM : "); + Serial.print(p->compensatedHumidity); + Serial.println(" %"); + + Serial.print("HTU TEMP : "); + Serial.print(p->htuTemp); + Serial.println(" *C"); +}