#include #include #include #include #include #include #include "Message.h" /****************************************************************************************************/ /****************************************HARDWARE DEFINITIONS****************************************/ #define MAX_DIGITAL 9 /****************************************************************************************************/ /****************************************LCD CONFIGURATION****************************************/ //LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address LiquidCrystal_I2C lcd(0x20, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE); // Set the LCD I2C address /****************************************************************************************************/ /****************************************NODE CONFIGURATION****************************************/ uint8_t thisNodeAddress = 0; // 0 = MASTER /****************************************************************************************************/ /****************************************nRF CONFIGURATION****************************************/ RF24 masterRadio(9, 10); RF24Network masterNetwork(masterRadio); RF24Mesh masterMesh(masterRadio, masterNetwork); /****************************************************************************************************/ /****************************************GLOBAL VARIABLES****************************************/ bool nRFWriteFlag = false; // flag to signal data write on nRF bool changeFlag = false; // detect change in input state bool stateNode1; bool stateNode2; uint32_t timerNode1; uint32_t timerNode2; struct dataStruct { uint16_t fromAddress; uint16_t toAddress; byte messageCmd; uint8_t sensorAddress; bool state; }; typedef union{ byte buffer[32]; dataStruct dataBreakdown; }dataMerge; dataMerge commInData; dataMerge commOutData; void createOutgoingNRFPacket(uint16_t fromAddress_fn, uint16_t toAddress_fn, uint8_t sensorAddress_fn, bool sensorState_fn) { commOutData.dataBreakdown.fromAddress = fromAddress_fn; commOutData.dataBreakdown.toAddress = toAddress_fn; commOutData.dataBreakdown.sensorAddress = sensorAddress_fn; commOutData.dataBreakdown.state = sensorState_fn; } /****************************************************************************************************/ /****************************************setup()****************************************/ void setup() { // begin Serial Serial.begin(115200); initIOs(); // set current nodeID masterMesh.setNodeID(thisNodeAddress); // begin mesh masterMesh.begin(); lcd.clear(); } /****************************************************************************************************/ /****************************************loop()****************************************/ void loop() { lcd.setCursor(0, 0); lcd.print("00"); masterMesh.update(); masterMesh.DHCP(); lcd.setCursor(0, 1); lcd.print(millis()); if (masterNetwork.available()) { RF24NetworkHeader header; masterNetwork.peek(header); switch(header.type) { case MSG_RCV_INPUT_STATUS: masterNetwork.read(header, commInData.buffer, sizeof(commInData.buffer)); if (commInData.dataBreakdown.fromAddress == 1) { stateNode1 = commInData.dataBreakdown.state; timerNode1 = millis(); } else if (commInData.dataBreakdown.fromAddress == 2) { stateNode2 = commInData.dataBreakdown.state; timerNode2 = millis(); } lcdPrint();; switchIOs(); break; default: masterNetwork.read(header,0,0); Serial.println(header.type); break; } } } /****************************************************************************************************/ /****************************************initIOs()****************************************/ void initIOs() { for (uint8_t i = 3; i < MAX_DIGITAL; i++) { pinMode(i, OUTPUT); } } /****************************************************************************************************/ /****************************************switchIOs()****************************************/ void switchIOs() { uint16_t outputNum = commInData.dataBreakdown.fromAddress; if (outputNum >= 1 && outputNum <= 6) { outputNum += 2; bool newState = commInData.dataBreakdown.state; digitalWrite(outputNum, newState); } } /****************************************************************************************************/ /****************************************lcdPrint()****************************************/ void lcdPrint() { lcd.clear(); lcd.setCursor(0,0); lcd.print("node 1 = "); lcd.print(stateNode1); lcd.setCursor(0,1); lcd.print("node 2 = "); lcd.print(stateNode2); } void lcdprintError(uint8_t nodeNumber) { if (nodeNumber == 1) { lcd.setCursor(0,0); lcd.print("node 1 error"); } if (nodeNumber == 2) { lcd.setCursor(0,0); lcd.print("node 2 error"); } }