My setup:
The receiver is an UNO receiving data via NRF24L01+ while connected to a laptop USB and an LCD which is being used for verification purposes (to make sure fresh data is being sent).
The sender is a nano with an NRF24 and a DHT11 powered from 2x18650 batteries being controlled by a bms which is wired to a solar panel. The nano is about 20 cms away from the UNO for now and is programmed to post every 30 minutes.
Here is are both Tx and Rx. The issue is that data does get sent every 30 minutes and received by the Rx, but barely any of it gets posted due to errors in the "posting data" part of the code. Last night I posted from 8pm to today at 6am and it only posted 3 new entries when it should have posted about 20 entries.
Im also posting the resulting code in the SM for the Rx unit which is the one that has the Thingspeak code. As you can see in the results, sometimes everything goes fine and other times it Fails.:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//UNO 0,1,9,10 - NC
//UNO 2 - ESP Tx
//UNO 3 - ESP Rx
//UNO 7 - NRF-CE
//UNO 8 - NRF-CSN
//UNO11 - NRF-MOSI
//UNO12 - NRF-MISO
//UNO13 - NRF-SCK
//UNOGND- ESP-GND
//UNO3.3- RAIL - NRF-3.3V - ESP3.3V
//UNO5V - LCD-Vcc
//UNOGND- NRF-GND
//UNOGND- LCD-GND
//UNO A4- LCD-SDA
//UNO A5- LCD-SCL
//
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "myssid";
String PASS = "mypwd";
String API = "mykey";
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";
String field2 = "field2";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266Radio(RX,TX);
#define CE_PIN 7
#define CSN_PIN 8
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
struct package{
float humedad = 0;
float temperatura = 0;
};
typedef struct package Package;
Package dataReceived;
bool newData = false;
void setup() {
Serial.begin(9600);
setupRadio();
esp8266Radio.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void setupRadio(){
pinMode(10,OUTPUT);
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
lcd.init();
lcd.init();
lcd.backlight();
}
void loop() {
getData();
showData();
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1)){
esp8266Radio.println(command);
if(esp8266Radio.find(readReplay)){
found = true;
break;
}
countTimeCommand++;
}
if(found == true){
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false){
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived.humedad);
Serial.println(dataReceived.temperatura);
lcd.setCursor(1,0);
lcd.print(dataReceived.humedad);
lcd.setCursor(1,1);
lcd.print(dataReceived.temperatura);
newData = false;
postDataCloud();
}
}
void postDataCloud(){
String getData = "GET /update?api_key="+ API +"&"+ field1 +"="+String(dataReceived.humedad)+"&"+ field2 +"="+String(dataReceived.temperatura);
Serial.println(getData);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266Radio.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
SENDER NANO
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define DHTTYPE DHT11
#define DHT11_PIN 6
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','x','A','A','A'};
DHT dht(DHT11_PIN, DHTTYPE);
struct package{
float humedad = 0;
float temperatura = 0;
};
typedef struct package Package;
Package data;
RF24 radio(CE_PIN, CSN_PIN);
int nbr_remaining;
#define led 13
ISR(WDT_vect){
wdt_reset();
}
void configure_wdt(void){
cli();
MCUSR = 0;
WDTCSR |= 0b00011000;
WDTCSR = 0b01000000 | 0b100001;
sei();
}
void sleep(int ncycles) {
nbr_remaining = ncycles;
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
power_adc_disable();
while (nbr_remaining > 0){
sleep_mode();
sleep_disable();
nbr_remaining = nbr_remaining - 1;
}
power_all_enable();
}
void setup(){
pinMode(led, OUTPUT);
pinMode(10,OUTPUT);
dht.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5);
radio.openWritingPipe(slaveAddress);
digitalWrite(led, LOW);
delay(1000);
configure_wdt();
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void loop(){
sleep(225);
send();
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void send() {
data.humedad = dht.readHumidity();
data.temperatura = dht.readTemperature();
bool rslt;
rslt = radio.write( &data, sizeof(data) );
Serial.print("Data Sent ");
Serial.print("data.humedad");
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}else {
Serial.println(" Tx failed");
}
}
void updateMessage() {
}