Hi all. I have a problem sending a array of data thru a RF24 transmitter. It worls when i use integer (int data[5]
but i dont get any decimals of course. I have read long time ago that you can multiply by 100 before sending and after receiving divide the result by 100 and get around the decimal problem.
I am a newbie so you have other ideas about making the code better, please do.
/*This Sketch is the transmitter part of transmitter No:1 of a nRF24L01 system.
The transmitter has an array that can hold 5 differens values and send them via a nRF24L01
transmitter to a receiver, (Se receiver Sketch). You can use more sensors, just extend the array.
Orginal code from Gökhan Göl http://www.gokhangol.com/arduino/nrf24l01-sensor-istasyonu-weather-station/
Changed to use with 3pcs BME280 temp, moist, pressure sensors.
Changes and code by oletborg@gmail.com Sweden 24 october 2017
*/
// PIN connections ********************************************************************************************
// MOSI:11Â Â Â Â Â Â // Connect pin MOSI on RF24 to Arduino pin 11
// MISO:12Â Â Â Â Â Â // Connect pin MISI on RF24 to Arduino pin 12
// SCK :13Â Â Â Â Â Â // Connect pin SCK on RF24 to Arduino pin 13
// RF_CS 7Â Â Â Â Â Â // Connect pin CS on RF24 to Arduino pin 7
// RF_CSN 8Â Â Â Â Â Â // Connect pin CSN on RF24 to Arduino pin 8
// DHT11PIN 2Â Â Â Â Â // Connect the DHT11 pin11 on the sensor to Arduino pin digital 2
// LDR A0Â Â Â Â Â Â Â // Connect the light LDR sensor to Arduino pin analog A0
// GAS A1Â Â Â Â Â Â Â // Connect the GAS sensor to Arduino pin analog A1
// RAIN A2Â Â Â Â Â Â // Connect the RAIN sensor to Arduino pin analog A2
// Define pins and variables ************************************************************************************
#include <SPI.h>Â Â Â // Add SPI library
#include <RF24.h>Â Â Â // Add nrf24l01 communication library
//#include <dht11.h>Â // Add the dht11 library.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h> //Radio
#define DHT11PIN 2Â Â // Pin number defined for DHT11 temperature and humidity sensor
#define ldr A0Â Â Â Â // The pin number defined for the ldr sensor
#define gas A1Â Â Â Â // the pin number defined for the gas sensor
#define rain A2Â Â Â Â // the pin number defined for the rain sensor
#define RF_CS 7Â Â Â Â // The CS pin on RF24 (in some models, it is written as CE) connected to Arduino pin 7
#define RF_CSN 8Â Â Â // RF24 CSN is connected to Arduino pin 8
//dht11 DHT11;
//BME280****************************************************************************
#define BME_SCKÂ 13Â // (SCL)
#define BME_MISO 12Â // (SDO)
#define BME_MOSI 11Â // (SDA)
#define BME_CS1Â 4Â // (CSB) Chip select for sensor 1
#define BME_CS2Â 5Â // (CSB) Chip select for sensor 2
#define BME_CS3Â 6Â // (CSB) Chip select for sensor 3
#define SEALEVELPRESSURE_HPA (1005.87)
Adafruit_BME280 bme1(BME_CS1); // hardware SPI
Adafruit_BME280 bme2(BME_CS2); // hardware SPI
Adafruit_BME280 bme3(BME_CS3); // hardware SPI
RF24 radio(RF_CS, RF_CSN); // RF24 define the CS and SSN
const uint64_t pipes[3] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL, 0xF0F0F0F0E3LL }; // The two different pipes we use
int data[5];
void setup() //***********************************************************************************
{
 Serial.begin(9600); // communication started at serial communication speed 9600
// Test communication for BME280******************************************************************
 delay(1000);
 Serial.println(F("BME280 test"));
 bool status;
 status = bme1.begin();
  if (!status) {
  Serial.println("Could not find a valid BME280 sensor, check wiring!");
 while (1);
}
Serial.println("Sensor Temp ,Moist ,Light ,Gas ,Rain");
 radio.begin();           // rf24 communication started
 radio.setDataRate(RF24_250KBPS);  // Transmitter speed to lowest get the safest and long distance
 //radio.setChannel(108);      // Above most Wifi Channels
 radio.setPALevel(RF24_PA_MAX);
 radio.openWritingPipe(pipes[0]);  // opens the address needed for transmitter. That is the part that sends the address 0xe7e7e7e7e7LL.
 radio.openReadingPipe(1, pipes[1]); // Can be used if you want the module to act as a receiver to.
 radio.startListening();      // Checks whether data is coming from radio modules.
 radio.printDetails();       // This is a debug where you can se the response of your RF24 module.
}
void loop()Â // ************************************************************************************
{
// int kontrol=DHT11.read(DHT11PIN);
 //temp1 = bme1.readTemperature();
 data[0]=bme1.readTemperature();
 data[1]=bme1.readHumidity();
 data[2]=analogRead(ldr);     // Read LDR light sensor 0-1023
 data[2]=map(data[2],0,1023,0,100); // Convert reading of light to 0= dark to 100% full daylight
 data[3]=analogRead(gas);     // Read the gas sensor
 data[4]=analogRead(rain);     // Read rain sensor
Â
// Print the data from the sensors to the serial monitor. ******************************************
 Serial.print("Temp: ");   // Print Heading text. Temp:
 Serial.print(data[0]);   // Print Temperature data from dht11 sensor in celcius
 Serial.print("\t ");    // Print a tab.
 Serial.print("Moist %: "); // Print Heading text Moist %:
 Serial.print(data[1]);   // Print Moisture value from the dht11 sensor in %
 Serial.print("\t");     // Print a tab.
 Serial.print("Light %: "); // Print Heading text. Light %:
 Serial.print(data[2]);   // Print data from LDR sensor
 Serial.print("\t");     // Print a tab.
 Serial.print("Gas: ");   // Print Heading text. Gas:
 Serial.print(data[3]);   // Print data from the GAS sensor
 Serial.print("\t");     // Print a tab.
 Serial.print("Rain: ");   // Print Heading text. Rain:
 Serial.println(data[4]);  // Print data from the GAS sensor
Â
// Sending all sensor values to the receiver. *****************************************************
 radio.stopListening();
 radio.write(data,sizeof(data)); //the measurement data is transmitted from the 5 variables
 radio.startListening();
 delay(1000);
}
