#include #include #include #include #include "nRF24L01.h" #include "RF24.h" #include "printf.h" #include "CRC8.h" #include "CRC16.h" const char *ssid = "*******"; //Internet ssid const char *password = "*******"; //Internet password const char *ntp_server = "pool.ntp.org"; uint8_t addrDTU[5] = {0x32, 0x88, 0x81, 0x72, 0x01}; // for test uint8_t addrWR[5] = {0x00, 0x02, 0x22, 0x72, 0x01}; RF24 radio(4, 5); CRC8 crc8; CRC16 crc16; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, ntp_server, 3600, 60000); uint8_t data[32]; uint8_t receive_payload[32]; uint8_t datalen = 0; bool replied = false; uint8_t step = 1; int channels[5] = {3, 23, 40, 61, 75}; uint8_t chn_cnt = 0; void setup() { Serial.begin(115200); printf_begin(); WiFi.begin(ssid, password); Serial.print("Connecting."); while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } Serial.println("connected"); timeClient.begin(); radio.begin(); radio.setAutoAck(true); radio.setPALevel(RF24_PA_MAX); radio.setDataRate (RF24_250KBPS); //radio.setPayloadSize(27); radio.enableDynamicPayloads(); radio.setChannel(23); radio.setCRCLength(RF24_CRC_16); radio.openWritingPipe(addrWR); radio.openReadingPipe(1, addrDTU); radio.printDetails(); crc8.setPolynome(0x01); crc8.setStartXOR(0); crc8.setEndXOR(0); crc16.setPolynome((uint16_t)0x18005); crc16.setStartXOR(0xFFFF); crc16.setEndXOR(0x0000); crc16.setReverseIn(true); crc16.setReverseOut(true); } void dump(const char* was, uint8_t* data, uint8_t len) { Serial.print(was); for (byte i = 0; i < len; i++) { Serial.print(data[i], HEX); Serial.print(' '); } Serial.println(); } void teleInit2() { unsigned long unix_epoch = timeClient.getEpochTime(); byte ntp[4]; ntp[3] = unix_epoch & 0xFF; ntp[2] = (unix_epoch >> 8) & 0xFF; ntp[1] = (unix_epoch >> 16) & 0xFF; ntp[0] = (unix_epoch >> 24) & 0xFF; //dump("ntp: ", ntp, 4); memset (data, 0, 32); data[0] = 0x15; data[1] = addrWR[3]; data[2] = addrWR[2]; data[3] = addrWR[1]; data[4] = addrWR[0]; data[5] = addrDTU[3]; data[6] = addrDTU[2]; data[7] = addrDTU[1]; data[8] = addrDTU[0]; data[9] = 0x80; data[10] = 0x0b; data[11] = 0x00; data[12] = ntp[0]; data[13] = ntp[1]; data[14] = ntp[2]; data[15] = ntp[3]; // for test //data[12] = 0x62; //data[13] = 0x09; //data[14] = 0x04; //data[15] = 0x94; crc16.restart(); crc16.add(&data[10], 14); data[24] = (crc16.getCRC() >> 8) & 0xFF; data[25] = crc16.getCRC() & 0xFF; crc8.restart(); crc8.add (data, 26); data[26] = crc8.getCRC(); //0x07; datalen = 27; } boolean send() { radio.stopListening(); return radio.write(data, datalen); } uint8_t receive () { uint8_t pipe = 0; radio.startListening(); // put in RX mode unsigned long start_timeout = millis(); // timer to detect no response while (!radio.available()) { // wait for response or timeout if (millis() - start_timeout > 400) // only wait 400 ms break; } radio.stopListening(); // put back in TX mode if (radio.available(&pipe)) { uint8_t bytes = radio.getPayloadSize(); radio.read(receive_payload, bytes); Serial.print(F("gelesen ")); Serial.print(bytes); Serial.print(F(" bytes auf Pipe ")); Serial.print(pipe); return bytes; } return 0; } void loop() { timeClient.update(); if (replied) return; radio.setChannel(channels[chn_cnt]); Serial.print("Set Channel: "); Serial.println(channels[chn_cnt]); if (step == 1) { teleInit2(); dump("send: ", data, datalen); if (!send()) Serial.println("Senden fehlg."); else { memset(receive_payload, 0, sizeof(receive_payload)); datalen = receive(); if (datalen) dump ("--------------empf.:", receive_payload, datalen); else Serial.println ("keine Antwort vom WR!"); } } delay(400); Serial.println(); if (chn_cnt == 4) { chn_cnt = 0; } else { chn_cnt++; } }