|
|
@@ -0,0 +1,417 @@
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * Header Files
|
|
|
+// *
|
|
|
+// *
|
|
|
+
|
|
|
+#define NewRemoteRcv 0
|
|
|
+#if NewRemoteRcv
|
|
|
+ #include <NewRemoteReceiver.h>
|
|
|
+#else
|
|
|
+ #include <RCSwitch.h>
|
|
|
+#endif
|
|
|
+
|
|
|
+#include <Thread.h>
|
|
|
+#include <Wire.h>
|
|
|
+#include <LiquidCrystal_I2C.h>
|
|
|
+
|
|
|
+LiquidCrystal_I2C lcd(0x27, 16, 2); // display type 16x2
|
|
|
+
|
|
|
+
|
|
|
+#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
|
|
|
+#include <avr/power.h>
|
|
|
+#include <avr/wdt.h>
|
|
|
+
|
|
|
+#define _THREADS 0 // 0 - Disable Threads; 1 - Enable Threads
|
|
|
+#define _InterruptLevel 0
|
|
|
+#define _InterruptPin 2 //Pin we are going to use to wake up the Arduino
|
|
|
+
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * Debugging Level
|
|
|
+// * 0 - Turn off Debugging Messages
|
|
|
+// * 0x0001 - General Level
|
|
|
+// * 0x0010 - Thread Level
|
|
|
+// * 0x0100 - RF SNIFFER Messages
|
|
|
+
|
|
|
+#define DEBUGLEVEL 1 // Print Messages
|
|
|
+#define BAUDRATE 115200 // Debug Port Baudrate
|
|
|
+
|
|
|
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
|
|
+#define WAIT_SERIAL_DEBUG 0
|
|
|
+
|
|
|
+#define _DEBUG(level,msg, ...) do { \
|
|
|
+ if ((DEBUGLEVEL & level) && (level != 0))\
|
|
|
+ Serial.print((msg), ##__VA_ARGS__);\
|
|
|
+ } while(0)
|
|
|
+
|
|
|
+#define _DEBUGLN(level,msg, ...) do { \
|
|
|
+ if ((DEBUGLEVEL & level) && (level != 0))\
|
|
|
+ Serial.println((msg), ##__VA_ARGS__);\
|
|
|
+ } while(0)
|
|
|
+
|
|
|
+int freeRam () {
|
|
|
+ extern int __heap_start, *__brkval;
|
|
|
+ int v;
|
|
|
+ return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
|
|
|
+}
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * Global Variables
|
|
|
+// *
|
|
|
+// *
|
|
|
+
|
|
|
+bool BoardRunning = false;
|
|
|
+
|
|
|
+Thread myThreadLed = Thread();
|
|
|
+Thread myThreadDisplay = Thread();
|
|
|
+
|
|
|
+#if _THREADS
|
|
|
+Thread myThreadRecv = Thread();
|
|
|
+Thread myThreadSend = Thread();
|
|
|
+Thread myThreadSendAliveMsg = Thread();
|
|
|
+#endif
|
|
|
+
|
|
|
+#if NewRemoteRcv
|
|
|
+ static unsigned int msgCounter = 0;
|
|
|
+#else
|
|
|
+ RCSwitch mySwitch = RCSwitch();
|
|
|
+#endif
|
|
|
+
|
|
|
+unsigned long previousMillis = 0;
|
|
|
+const long interval = 1000; // delay in milliseconds
|
|
|
+
|
|
|
+unsigned long switchReceivedValue = 0;
|
|
|
+
|
|
|
+
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * Functions Declaration and Implementation
|
|
|
+// *
|
|
|
+// *
|
|
|
+
|
|
|
+// *
|
|
|
+// * Thread - Toggle LED
|
|
|
+// *
|
|
|
+void threadLed(){
|
|
|
+ static bool ledStatus = false;
|
|
|
+ ledStatus = !ledStatus;
|
|
|
+
|
|
|
+ digitalWrite(LED_BUILTIN, ledStatus); //We use the led on pin 13 to indecate when Arduino is running
|
|
|
+
|
|
|
+ _DEBUG(2,"LED: ");
|
|
|
+ _DEBUGLN(2,millis());
|
|
|
+ switchReceivedValue = 0;
|
|
|
+}
|
|
|
+
|
|
|
+void threadDisplay(){
|
|
|
+ static bool displayBackgroundLight = false;
|
|
|
+ displayBackgroundLight = !displayBackgroundLight;
|
|
|
+
|
|
|
+ lcd.setCursor(0,0);
|
|
|
+ lcd.print("AZ-Delivery");
|
|
|
+ lcd.setCursor(0,1);
|
|
|
+ lcd.print("Enjoy - Arduino");
|
|
|
+
|
|
|
+ if (displayBackgroundLight)
|
|
|
+ lcd.backlight(); // turn on LCD backlight
|
|
|
+ else
|
|
|
+ lcd.noBacklight(); // turn off LCD backlight
|
|
|
+
|
|
|
+ _DEBUG(2,"Display: ");
|
|
|
+ _DEBUGLN(2,millis());
|
|
|
+ switchReceivedValue = 0;
|
|
|
+}
|
|
|
+
|
|
|
+#if _THREADS
|
|
|
+
|
|
|
+// *
|
|
|
+// * Thread - Read Button
|
|
|
+// *
|
|
|
+void threadButton(){
|
|
|
+ static bool buttonStatus = false;
|
|
|
+
|
|
|
+ _DEBUG(2,"Button: ");
|
|
|
+ _DEBUGLN(2,millis());
|
|
|
+}
|
|
|
+
|
|
|
+// *
|
|
|
+// * Thread - Receive Messages
|
|
|
+// *
|
|
|
+void threadRecv(){
|
|
|
+ static bool recvStatus = false;
|
|
|
+
|
|
|
+ _DEBUG(2,"Recv: ");
|
|
|
+ _DEBUGLN(2,millis());
|
|
|
+}
|
|
|
+
|
|
|
+// *
|
|
|
+// * Thread - Send Messages
|
|
|
+// *
|
|
|
+void threadSend(){
|
|
|
+ static bool sendStatus = false;
|
|
|
+
|
|
|
+ _DEBUG(2,"Send: ");
|
|
|
+ _DEBUGLN(2,millis());
|
|
|
+}
|
|
|
+
|
|
|
+// *
|
|
|
+// * Thread - Send Alive Message
|
|
|
+// *
|
|
|
+void threadSendAliveMsg(){
|
|
|
+ static char msg = 'o';
|
|
|
+ static int count = 0;
|
|
|
+
|
|
|
+ if (msg == 'o')
|
|
|
+ msg = '-';
|
|
|
+ else
|
|
|
+ msg = 'o';
|
|
|
+
|
|
|
+ count++;
|
|
|
+ if(!(count % 60)){
|
|
|
+ static int iHours = 0;
|
|
|
+ _DEBUG(1,"\n");
|
|
|
+ _DEBUG(1,iHours);
|
|
|
+ _DEBUG(1, " hours: ");
|
|
|
+ iHours++;
|
|
|
+ }
|
|
|
+ _DEBUG(1,msg);
|
|
|
+}
|
|
|
+#endif
|
|
|
+// *
|
|
|
+// * Function SetupWdtSleep
|
|
|
+// *
|
|
|
+// * Time | Macro | WDP0 WDP1 WDP2 WDP3
|
|
|
+// * 16ms WDTO_16MS 0 0 0 0
|
|
|
+// * 32ms WDTO_30MS 1 0 0 0
|
|
|
+// * 64ms WDTO_60MS 0 1 0 0
|
|
|
+// * 125ms WDTO_120MS 1 1 0 0
|
|
|
+// * 250ms WDTO_250MS 0 0 1 0
|
|
|
+// * 500ms WDTO_500MS 1 0 1 0
|
|
|
+// * 1s WDTO_1S 0 1 1 0
|
|
|
+// * 2s WDTO_2S 1 1 1 0
|
|
|
+// * 4s WDTO_4S 0 0 0 1
|
|
|
+// * 8s WDTO_8S 1 0 0 1
|
|
|
+// *
|
|
|
+// *
|
|
|
+// *
|
|
|
+void SetupWdtSleep() {
|
|
|
+ // Setup Watchdog Timers
|
|
|
+ MCUSR &= ~(1<<WDRF); // clear WDT reset
|
|
|
+ WDTCSR |= (1<<WDCE) | (1<<WDE); // Set WDCE, Prescaler
|
|
|
+ WDTCSR = WDTO_1S;
|
|
|
+ WDTCSR |= 1<<WDIE; // Enable Interrupt
|
|
|
+
|
|
|
+ Serial.println("Init Watchdog");
|
|
|
+}
|
|
|
+
|
|
|
+// *
|
|
|
+// * Function Going-To-Sleep
|
|
|
+// *
|
|
|
+void Going_To_Sleep(){
|
|
|
+ sleep_enable(); //Enabling sleep mode
|
|
|
+ set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting sleep mode (Deep Sleep for power saving)
|
|
|
+#if NewRemoteRcv
|
|
|
+#else
|
|
|
+ mySwitch.disableReceive(); // Disable RF Receiver
|
|
|
+ sleep_mode();// activating sleep mode
|
|
|
+ mySwitch.enableReceive(_InterruptLevel); // Enable RF Receiver
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+// *
|
|
|
+// * Function ISR Wake-Up
|
|
|
+// *
|
|
|
+ISR (WDT_vect)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * startup Code
|
|
|
+// *
|
|
|
+// *
|
|
|
+
|
|
|
+void setup() {
|
|
|
+ // put your setup code here, to run once:
|
|
|
+ if(DEBUGLEVEL){
|
|
|
+ Serial.begin(BAUDRATE);
|
|
|
+ _DEBUGLN(1, "*************************************************");
|
|
|
+ _DEBUG(1, "Board initialized successfully: ");
|
|
|
+ _DEBUGLN(1, __FILENAME__);
|
|
|
+ _DEBUG(1, "Build date: "); _DEBUGLN(1, __DATE__);
|
|
|
+ }
|
|
|
+
|
|
|
+ pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
|
|
|
+ pinMode(_InterruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
|
|
|
+ // digitalWrite(LED_BUILTIN,HIGH);//turning LED on
|
|
|
+
|
|
|
+ // Initialize LCD
|
|
|
+ lcd.init();
|
|
|
+ // lcd.backlight(); // turn on LCD backlight
|
|
|
+ // lcd.noBacklight(); // turn off LCD backlight
|
|
|
+
|
|
|
+ //
|
|
|
+ // Initialize Threads
|
|
|
+ //
|
|
|
+ myThreadLed.onRun(threadLed);
|
|
|
+ myThreadLed.setInterval(1500);
|
|
|
+
|
|
|
+ myThreadDisplay.onRun(threadDisplay);
|
|
|
+ myThreadDisplay.setInterval(1500);
|
|
|
+
|
|
|
+#if _THREADS
|
|
|
+
|
|
|
+ myThreadRecv.onRun(threadRecv);
|
|
|
+ myThreadRecv.setInterval(1700);
|
|
|
+
|
|
|
+ myThreadSend.onRun(threadSend);
|
|
|
+ myThreadSend.setInterval(1800);
|
|
|
+
|
|
|
+ myThreadSendAliveMsg.onRun(threadSendAliveMsg);
|
|
|
+ myThreadSendAliveMsg.setInterval(60000);
|
|
|
+#endif
|
|
|
+
|
|
|
+#if NewRemoteRcv
|
|
|
+ // Initialize receiver on interrupt 0 (= digital pin 2) for arduino uno, calls the callback "showCode"
|
|
|
+ // after 1 identical codes have been received in a row. (thus, keep the button pressed
|
|
|
+ // for a moment), on esp8266 use on interrupt 5 = digital pin 1
|
|
|
+ //
|
|
|
+ // See the interrupt-parameter of attachInterrupt for possible values (and pins)
|
|
|
+ // to connect the receiver.
|
|
|
+
|
|
|
+ // if you don't see codes try to reset your board after upload
|
|
|
+ NewRemoteReceiver::init(0, 2, showCode);
|
|
|
+ Serial.println("Receiver initialized");
|
|
|
+
|
|
|
+#else
|
|
|
+ // RFControl::startReceiving(0);
|
|
|
+ mySwitch.enableReceive(_InterruptLevel); // receives Data based on Interrupt
|
|
|
+ // attachInterrupt(0, wake_up, CHANGE);//attaching an interrupt to pin d2
|
|
|
+#endif
|
|
|
+
|
|
|
+ Serial.println("14CORE | RF Sniffer Test Code for Receiver");
|
|
|
+ Serial.println("RF Sniffer Initializing......");
|
|
|
+ delay(2000);
|
|
|
+ Serial.println("Starting.....................");
|
|
|
+ delay(2000);
|
|
|
+ // SetupWdtSleep();
|
|
|
+}
|
|
|
+
|
|
|
+// *****************************************************************************************
|
|
|
+// ************************************************************
|
|
|
+// **************************************
|
|
|
+// *
|
|
|
+// * loop Code
|
|
|
+// *
|
|
|
+// *
|
|
|
+
|
|
|
+void loop() {
|
|
|
+ // put your main code here, to run repeatedly:
|
|
|
+ unsigned long currentMillis = millis();
|
|
|
+
|
|
|
+ if (!BoardRunning){
|
|
|
+ _DEBUGLN(1, "Entering loop()");
|
|
|
+ _DEBUG(1, "Free RAM: ");
|
|
|
+ _DEBUGLN(1, freeRam());
|
|
|
+ _DEBUGLN(1, "*************************************************");
|
|
|
+ BoardRunning = true;
|
|
|
+ delay(1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(myThreadDisplay.shouldRun())
|
|
|
+ myThreadDisplay.run();
|
|
|
+
|
|
|
+ // if(myThreadLed.shouldRun())
|
|
|
+ // myThreadLed.run();
|
|
|
+
|
|
|
+#if _THREADS
|
|
|
+
|
|
|
+ if(myThreadRecv.shouldRun())
|
|
|
+ myThreadRecv.run();
|
|
|
+
|
|
|
+ if(myThreadSend.shouldRun())
|
|
|
+ myThreadSend.run();
|
|
|
+
|
|
|
+ if(myThreadSendAliveMsg.shouldRun())
|
|
|
+ myThreadSendAliveMsg.run();
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+#if NewRemoteRcv
|
|
|
+
|
|
|
+#else
|
|
|
+ //
|
|
|
+ //
|
|
|
+ //
|
|
|
+ if (mySwitch.available()) {
|
|
|
+
|
|
|
+ int value = mySwitch.getReceivedValue();
|
|
|
+ if (value == 0) {
|
|
|
+ Serial.println("Unknown encoding");
|
|
|
+ } else {
|
|
|
+ // digitalWrite(LED_BUILTIN,HIGH);// turning LED on
|
|
|
+ _DEBUG(4,millis());
|
|
|
+ _DEBUG(4,"\t");
|
|
|
+ _DEBUG(4,"Received 0x");
|
|
|
+ _DEBUG(4,mySwitch.getReceivedValue(),HEX);
|
|
|
+ _DEBUG(4," / ");
|
|
|
+ _DEBUG(4,mySwitch.getReceivedBitlength());
|
|
|
+ _DEBUG(4," Bit / Protocol: ");
|
|
|
+ _DEBUG(4,mySwitch.getReceivedProtocol());
|
|
|
+ _DEBUG(4," / Delay: ");
|
|
|
+ _DEBUGLN(4,mySwitch.getReceivedDelay());
|
|
|
+ // output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
|
|
|
+ // decoder(mySwitch.getReceivedValue(), mySwitch.getReceivedProtocol());
|
|
|
+ }
|
|
|
+ mySwitch.resetAvailable();
|
|
|
+ delay(150);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ if(currentMillis - previousMillis >= 200)
|
|
|
+ {
|
|
|
+ // digitalWrite(LED_BUILTIN, LOW);// turning LED off
|
|
|
+ // _DEBUGLN(4,"...going to sleep");
|
|
|
+ // Going_To_Sleep();
|
|
|
+ previousMillis = millis();
|
|
|
+ }
|
|
|
+} // loop
|
|
|
+
|
|
|
+#if NewRemoteRcv
|
|
|
+
|
|
|
+// Callback function is called only when a valid code is received.
|
|
|
+void showCode(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType) {
|
|
|
+
|
|
|
+ Serial.print("Counter: ");
|
|
|
+ Serial.println(msgCounter);
|
|
|
+
|
|
|
+ // Print the received code.
|
|
|
+ Serial.print("Code: ");
|
|
|
+ Serial.print(address);
|
|
|
+ Serial.print(" Period: ");
|
|
|
+ Serial.println(period);
|
|
|
+ Serial.print(" unit: ");
|
|
|
+ Serial.println(unit);
|
|
|
+ Serial.print(" groupBit: ");
|
|
|
+ Serial.println(groupBit);
|
|
|
+ Serial.print(" switchType: ");
|
|
|
+ Serial.println(switchType);
|
|
|
+
|
|
|
+ Serial.println("----------------------------------------------------");
|
|
|
+ msgCounter++;
|
|
|
+}
|
|
|
+#endif
|