#include #include #include "LPPDecode.h" // Set your AppEUI and AppKey const char *appEui = "XXXXXXXXXXXXXXXX"; const char *appKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; #define loraSerial Serial1 #define debugSerial Serial // Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915 #define freqPlan TTN_FP_EU868 TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan); TheThingsNode *node; CayenneLPP lpp(51); #define PORT_SETUP 1 #define PORT_INTERVAL 2 #define PORT_MOTION 3 #define PORT_BUTTON 4 void setup() { loraSerial.begin(57600); debugSerial.begin(9600); // Wait a maximum of 10s for Serial Monitor while (!debugSerial && millis() < 10000) ; // Config Node node = TheThingsNode::setup(); node->configLight(true); node->configInterval(true, 60000); node->configTemperature(true); node->onWake(wake); node->onInterval(interval); node->onSleep(sleep); node->onMotionStart(onMotionStart); node->onButtonRelease(onButtonRelease); // Test sensors and set LED to GREEN if it works node->showStatus(); node->setColor(TTN_GREEN); debugSerial.println("-- TTN: STATUS"); ttn.showStatus(); debugSerial.println("-- TTN: JOIN"); ttn.join(appEui, appKey); debugSerial.println("-- SEND: SETUP"); sendData(PORT_SETUP); } void loop() { node->loop(); } void interval() { node->setColor(TTN_BLUE); debugSerial.println("-- SEND: INTERVAL"); sendData(PORT_INTERVAL); } void wake() { node->setColor(TTN_GREEN); } void sleep() { node->setColor(TTN_BLACK); } void onMotionStart() { node->setColor(TTN_YELLOW); debugSerial.print("-- SEND: MOTION"); sendData(PORT_MOTION); } void onButtonRelease(unsigned long duration) { node->setColor(TTN_RED); debugSerial.print("-- SEND: BUTTON"); debugSerial.println(duration); sendData(PORT_BUTTON); } void sendData(uint8_t port) { // Wake RN2483 ttn.wake(); ttn.showStatus(); node->showStatus(); uint16_t battery = node->getBattery(); uint16_t light = node->getLight(); int16_t temperature = round(node->getTemperatureAsFloat()); lpp.reset(); lpp.addTemperature(1, temperature); lpp.addBarometricPressure(2, battery); lpp.addGPS(3, 51.41325875, -0.28359734, 25); lpp.addLuminosity(4, light); if (PORT_BUTTON==port) lpp.addDigitalInput(5, 0x01); else lpp.addDigitalInput(5, 0x00); // lpp.addDigitalOutput(6, 0.0); // lpp.addAnalogOutput(6, 0.0); // Send it off ttn.sendBytes(lpp.getBuffer(), lpp.getSize()), port; // Send single byte to poll for incoming messages ttn.poll(); /* float value(0.); int x; LPP_Analog(99, value); debugSerial.print("Down Value: "); debugSerial.println(value); int ival=(int) value/10; for (x=0;xsetColor(TTN_MAGENTA); delay(200); node->setColor(TTN_CYAN); delay(200); } node->setColor(TTN_BLACK); */ // Set RN2483 to sleep mode ttn.sleep(60000); // This one is not optionnal, remove it // and say bye bye to RN2983 sleep mode delay(50); } void message(const uint8_t *payload, size_t size, port_t port) { debugSerial.println("-- MESSAGE"); debugSerial.print("Received " + String(size) + " bytes on port " + String(port) + ":"); for (int i = 0; i < size; i++) { debugSerial.print(" " + String(payload[i])); } debugSerial.println(); }