Add Main Door Mysensors
This commit is contained in:
109
MainDoorMyS/src/main.cpp
Normal file
109
MainDoorMyS/src/main.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <Arduino.h>
|
||||
#define MY_DEBUG
|
||||
|
||||
// Enable and select radio type attached
|
||||
#define MY_RF24_PA_LEVEL RF24_PA_HIGH
|
||||
#define MY_RADIO_RF24
|
||||
#if F_CPU == 8000000L
|
||||
#define MY_BAUD_RATE 38400
|
||||
#endif
|
||||
|
||||
#define MY_DEFAULT_LED_BLINK_PERIOD 300
|
||||
#define MY_WITH_LEDS_BLINKING_INVERSE
|
||||
#define MY_DEFAULT_ERR_LED_PIN 2 // Error led pin
|
||||
#define MY_DEFAULT_TX_LED_PIN 3 // the PCB, on board LED
|
||||
#define MY_DEFAULT_RX_LED_PIN 4 // Receive led pin
|
||||
|
||||
#include <MySensors.h>
|
||||
|
||||
#define TOP_LOCK A0
|
||||
#define INT_LOCK A1
|
||||
#define DOWN_LOCK A2
|
||||
#define SMALL_LOCK A3
|
||||
#define DOOR A4
|
||||
|
||||
#define TOP_LOCK_ID 0
|
||||
#define INT_LOCK_ID 1
|
||||
#define DOWN_LOCK_ID 2
|
||||
#define SMALL_LOCK_ID 3
|
||||
#define DOOR_ID 4
|
||||
|
||||
bool bpTop, bpInt, bpDown, bpSmall, bpDoor;
|
||||
|
||||
MyMessage msgTop(TOP_LOCK_ID, V_LOCK_STATUS);
|
||||
MyMessage msgInt(INT_LOCK_ID, V_LOCK_STATUS);
|
||||
MyMessage msgDown(DOWN_LOCK_ID, V_LOCK_STATUS);
|
||||
MyMessage msgSmall(SMALL_LOCK_ID, V_LOCK_STATUS);
|
||||
MyMessage msgDoor(DOOR_ID, V_LOCK_STATUS);
|
||||
|
||||
void before()
|
||||
{
|
||||
pinMode(TOP_LOCK, INPUT);
|
||||
pinMode(INT_LOCK, INPUT);
|
||||
pinMode(DOWN_LOCK, INPUT);
|
||||
pinMode(SMALL_LOCK, INPUT);
|
||||
}
|
||||
|
||||
void presentation()
|
||||
{
|
||||
// Send the sketch version information to the gateway and Controller
|
||||
sendSketchInfo("Main Door", "1.0");
|
||||
present(0, S_LOCK, "Top");
|
||||
present(1, S_LOCK, "Int");
|
||||
present(2, S_LOCK, "Down");
|
||||
present(3, S_LOCK, "Small");
|
||||
present(4, S_LOCK, "Door");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool bTop, bInt, bDown, bSmall, bDoor;
|
||||
bTop = digitalRead(TOP_LOCK);
|
||||
bInt = digitalRead(INT_LOCK);
|
||||
bDown = digitalRead(DOWN_LOCK);
|
||||
bSmall = digitalRead(SMALL_LOCK);
|
||||
bDoor = digitalRead(DOOR);
|
||||
if(bTop != bpTop){
|
||||
bpTop = bTop;
|
||||
sendData(msgTop, bDoor);
|
||||
}
|
||||
if(bInt != bpInt){
|
||||
bpInt = bInt;
|
||||
sendData(msgInt, bInt);
|
||||
}
|
||||
if(bDown != bpDown){
|
||||
bpDown = bDown;
|
||||
sendData(msgDown, bDown);
|
||||
}
|
||||
if(bSmall != bpSmall){
|
||||
bpSmall = bSmall;
|
||||
sendData(msgSmall, bSmall);
|
||||
}
|
||||
if(bDoor != bpDoor){
|
||||
bpDoor = bDoor;
|
||||
sendData(msgDoor, bDoor);
|
||||
}
|
||||
}
|
||||
|
||||
void sendData(MyMessage msg, bool status)
|
||||
{
|
||||
bool send_data = false;
|
||||
uint8_t count = 0;
|
||||
while(send_data == false){
|
||||
count++;
|
||||
Serial.print("Sending a message, try No."); // Выводим в монитор порта попытку отправки
|
||||
Serial.println(count); // и её номер
|
||||
send_data = send(msg.set(status));
|
||||
wait(1000, C_SET, V_STATUS);
|
||||
if(send_data)
|
||||
Serial.println("Message sent");
|
||||
if ((count == 3 )&&(send_data == 0)){ // Если сделано 3 попытки и нет подтверждения отправки
|
||||
count = 0; // Обнуляем счётчик
|
||||
send_data = 1; // Выходим из цикла
|
||||
Serial.println("Send failed"); // Выводим сообщение "Отправка не удалась"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user