//Send temperature and humidity data using a DHT11 sensor to the Lora Gateway #include "TheThingsUno.h" // Set your DevAddr const byte devAddr[4] = { }; // Set your NwkSKey and AppSKey const byte nwkSKey[16] = { }; const byte appSKey[16] = { }; #define debugSerial Serial #define loraSerial Serial1 #define debugPrintLn(...) { if (debugSerial) debugSerial.println(__VA_ARGS__); } #define debugPrint(...) { if (debugSerial) debugSerial.print(__VA_ARGS__); } TheThingsUno ttu; int DHpin = 8; byte dat [4]; byte read_data () { byte data; for (int i = 0; i < 8; i ++) { if (digitalRead (DHpin) == LOW) { while (digitalRead (DHpin) == LOW); // wait for 50us delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1' if (digitalRead (DHpin) == HIGH) data |= (1 << (7-i)); // high front and low in the post while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver } } return data; } void start_test () { digitalWrite (DHpin, LOW); // bus down, send start signal delay (30); // delay greater than 18ms, so DHT11 start signal can be detected digitalWrite (DHpin, HIGH); delayMicroseconds (40); // Wait for DHT11 response pinMode (DHpin, INPUT); while (digitalRead (DHpin) == HIGH); delayMicroseconds (80); // DHT11 response, pulled the bus 80us if (digitalRead (DHpin) == LOW); delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered dat[i] = read_data (); pinMode (DHpin, OUTPUT); digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal } void setup () { debugSerial.begin(115200); loraSerial.begin(57600); delay(1000); ttu.init(loraSerial, debugSerial); ttu.reset(); //the device will configure the LoRa module ttu.personalize(devAddr, nwkSKey, appSKey); delay(6000); ttu.showStatus(); debugPrintLn("Setup for The Things Network complete"); delay(1000); pinMode (DHpin, OUTPUT); } void loop () { start_test (); debugPrint ("Current humidity = "); debugPrint (dat [0], DEC); // display the humidity-bit integer; debugPrint ('.'); debugPrint (dat [1], DEC); // display the humidity decimal places; debugPrintLn ('%'); debugPrint ("Current temperature = "); debugPrint (dat [2], DEC); // display the temperature of integer bits; debugPrint ('.'); debugPrint (dat [3], DEC); // display the temperature of decimal places; debugPrintLn ('C'); //send data ttu.sendBytes(dat, sizeof(dat)); delay (20000); }