08-08-2019, 15:42
(08-08-2019, 15:12)kaczakat napisał(a): W odbiorniku nie powinieneś mieć żadnego delaya.
Jeśli masz nowe dane:
-odbierasz,
-czyścisz ekran,
-wyświetlasz,
-nasłuchujesz nowych danych.
W nadajniku jest to obojętne jak zaplanujesz kiedy wysyłasz, delay na pierwsze testy też jest OK, ale odbiornik powinien być gotowy na akcję zawsze.
Kod:
#include <LiquidCrystal_I2C.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Timers.h>
Timer Timer1;
RF24 receive (7,8);
byte address [5] = "00001";
LiquidCrystal_I2C lcd(0x27, 16, 2);
struct package
{
int temperature = 0;
int humidity = 0;
int PM25 = 0;
int PM10 = 0;
};
typedef struct package Package;
Package data;
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Otrzymane dane");
receive.begin();
receive.openReadingPipe(0,address);
receive.setPALevel(RF24_PA_MIN);
receive.setDataRate(RF24_250KBPS);
receive.setChannel(100);
receive.startListening();
Timer1.begin(SECS(10));
}
void loop() {
if(Timer1.available())
{
lcd.clear();
delay(1);
Timer1.restart();
}
if (receive.available())
{
receive.read(&data, sizeof(data));
lcd.setCursor(0,0);
lcd.print(data.temperature);
lcd.print("C ");
lcd.print(data.humidity);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print(data.PM25);
lcd.print("ug/m3 ");
lcd.print(data.PM10);
lcd.print("ug/m3");
}
}