#include #include #include #include #include // Singleton instance of the radio driver RH_NRF24 nrf24; int idDHT11pin = 2; //Digital pin for comunications int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above) int deviceID = EEPROM.read(0); //declaration void dht11_wrapper(); // must be declared before the lib initialization // Lib instantiate idDHT11 DHT11(idDHT11pin, idDHT11intNumber, dht11_wrapper); void setup() { Serial.begin(9600); while (!Serial) ; // wait for serial port to connect. Needed for Leonardo only if (!nrf24.init()) { Serial.println("init failed"); } // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm if (!nrf24.setChannel(3)) { Serial.println("setChannel failed"); } if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm)) { Serial.println("setRF failed"); } Serial.println("Transmitter started"); } // This wrapper is in charge of calling // mus be defined like this for the lib work void dht11_wrapper() { DHT11.isrCallback(); } void loop() { Serial.println("Sending to gateway"); uint8_t data[4]; int result = DHT11.acquireAndWait(); switch (result) { case IDDHTLIB_OK: Serial.println("DHT11 Measurement OK"); break; case IDDHTLIB_ERROR_CHECKSUM: Serial.println("Error\n\r\tChecksum error"); break; case IDDHTLIB_ERROR_ISR_TIMEOUT: Serial.println("Error\n\r\tISR time out error"); break; case IDDHTLIB_ERROR_RESPONSE_TIMEOUT: Serial.println("Error\n\r\tResponse time out error"); break; case IDDHTLIB_ERROR_DATA_TIMEOUT: Serial.println("Error\n\r\tData time out error"); break; case IDDHTLIB_ERROR_ACQUIRING: Serial.println("Error\n\r\tAcquiring"); break; case IDDHTLIB_ERROR_DELTA: Serial.println("Error\n\r\tDelta time to small"); break; case IDDHTLIB_ERROR_NOTSTARTED: Serial.println("Error\n\r\tNot started"); break; default: Serial.println("Unknown error"); break; } data[0] = DHT11.getHumidity(); data[1] = DHT11.getCelsius(); data[2] = deviceID; Serial.println("------------- Measurements -------------"); Serial.print("Humidity: "); Serial.print(data[0]); Serial.print(", Temperature: "); Serial.print(data[1]); Serial.print(", ID: "); Serial.print(data[2]); Serial.println(); nrf24.send(data, sizeof(data)); nrf24.waitPacketSent(); // Now wait for a reply uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); if (nrf24.waitAvailableTimeout(1000)) { // Should be a reply message for us now if (nrf24.recv(buf, &len)) { Serial.print("got reply: "); Serial.println((char*)buf); } else { Serial.println("recv failed"); } } else { Serial.println("No reply."); } delay(3000); }