After not much development on my nrf24L01 AdaFruit.IO and Azure IOT Hub field gateways for a while some new nRF24L01 devices arrived in the post last week.
This sample client is an Arduino Nano clone with an Arduino Nano radio shield for NRF24L01+.
I use the shield’s onboard SHA204A crypto and authentication chip, and a Seeedstudio Temperature & Humidity sensor with the data uploaded to adafruit.io.
/* Copyright ® 2018 September devMobile Software, All Rights Reserved THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. You can do what you want with this code, acknowledgment would be nice. http://www.devmobile.co.nz */ #include <RF24.h> #include <sha204_library.h> #include <TH02_dev.h> // RF24 radio( ChipeEnable , ChipSelect ) RF24 radio(9, 10); const byte FieldGatewayChannel = 15 ; const byte FieldGatewayAddress[] = {"Base1"}; const rf24_datarate_e RadioDataRate = RF24_250KBPS; const rf24_pa_dbm_e RadioPALevel = RF24_PA_HIGH; // Payload configuration const int PayloadSizeMaximum = 32 ; char payload[PayloadSizeMaximum] = ""; const byte DeviceIdPlusCsvSensorReadings = 1 ; const byte SensorReadingSeperator = ',' ; // ATSHA204 secure authentication, validation with crypto and hashing (only using for unique serial number) atsha204Class sha204(A3); const int DeviceSerialNumberLength = 9 ; uint8_t deviceSerialNumber[DeviceSerialNumberLength] = {""}; const int LoopSleepDelaySeconds = 10 ; void setup() { Serial.begin(9600); Serial.println("Setup called"); // Retrieve the serial number then display it nicely sha204.getSerialNumber(deviceSerialNumber); Serial.print("SNo:"); for (int i = 0; i < sizeof( deviceSerialNumber) ; i++) { // Add a leading zero if ( deviceSerialNumber[i] < 16) { Serial.print("0"); } Serial.print(deviceSerialNumber[i], HEX); Serial.print(" "); } Serial.println(); // Configure the Seeedstudio TH02 temperature & humidity sensor Serial.println("TH02 setup"); TH02.begin(); delay(100); // Configure the nRF24 module Serial.println("nRF24 setup"); radio.begin(); radio.setChannel(FieldGatewayChannel); radio.openWritingPipe(FieldGatewayAddress); radio.setDataRate(RadioDataRate) ; radio.setPALevel(RadioPALevel); radio.enableDynamicPayloads(); Serial.println("Setup done"); } void loop() { int payloadLength = 0 ; float temperature ; float humidity ; Serial.println("Loop called"); memset( payload, 0, sizeof( payload)); // prepare the payload header with PayloadMessageType (top nibble) and DeviceID length (bottom nibble) payload[0] = (DeviceIdPlusCsvSensorReadings << 4) | sizeof(deviceSerialNumber) ; payloadLength += 1; // Copy the device serial number into the payload memcpy( &payload[payloadLength], deviceSerialNumber, sizeof( deviceSerialNumber)); payloadLength += sizeof( deviceSerialNumber) ; // Read the temperature, humidity & battery voltage values then display nicely temperature = TH02.ReadTemperature(); Serial.print("T:"); Serial.print( temperature, 1 ) ; Serial.print( "C" ) ; humidity = TH02.ReadHumidity(); Serial.print(" H:"); Serial.print( humidity, 0 ) ; Serial.println( "%" ) ; // Copy the temperature into the payload payload[ payloadLength] = 't'; payloadLength += 1 ; dtostrf(temperature, 6, 1, &payload[payloadLength]); payloadLength += 6; payload[ payloadLength] = ','; payloadLength += 1 ; // Copy the humidity into the payload payload[ payloadLength] = 'h'; payloadLength += 1 ; dtostrf(humidity, 4, 0, &payload[payloadLength]); payloadLength += 4; // Powerup the nRF24 chipset then send the payload to base station Serial.print( "Payload length:"); Serial.println( payloadLength ); Serial.println( "nRF24 write" ) ; boolean result = radio.write(payload, payloadLength); if (result) Serial.println("Write Ok..."); else Serial.println("Write failed."); Serial.println("Loop done"); delay(LoopSleepDelaySeconds * 1000l); }
Arduino monitor output
Prototype hardware
Bill of materials (prices as at Sep 2018)
- Arduino Nano clone USD4.70
- Easy Sensors Arduino Nano Radio Shield for nRF24L01 USD13
- Seeedstudio Temperature and Humidity Sensor Pro USD11.50
- Seeedstudio 4 pin Male Jumper to Grove 4 pin Conversion Cable USD2.90
Adafruit.IO temperature display when I moved the sensor outside.