Files
AHome/MainDoorMyS/src/main.cpp

139 lines
4.1 KiB
C++

#include <Arduino.h>
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RF24_PA_LEVEL RF24_PA_HIGH
#define MY_RADIO_RF24
#define MY_RF24_CHANNEL (105)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif
#define MY_DEFAULT_LED_BLINK_PERIOD 100
#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 A3
#define SMALL_LOCK A2
#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
#define CUSTOM_ID 5
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);
MyMessage msgCust(CUSTOM_ID, V_VAR1);
void sendData(MyMessage msg, bool status);
void before()
{
pinMode(TOP_LOCK, INPUT_PULLUP);
pinMode(INT_LOCK, INPUT_PULLUP);
pinMode(DOWN_LOCK, INPUT_PULLUP);
pinMode(SMALL_LOCK, INPUT_PULLUP);
pinMode(DOOR, INPUT_PULLUP);
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Main Door", "1.0");
present(TOP_LOCK_ID, S_LOCK, "Top");
present(INT_LOCK_ID, S_LOCK, "Int");
present(DOWN_LOCK_ID, S_LOCK, "Down");
present(SMALL_LOCK_ID, S_LOCK, "Small");
present(DOOR_ID, S_LOCK, "Door");
present(CUSTOM_ID, S_CUSTOM);
}
void setup() {
bpDoor = !digitalRead(DOOR);
bpTop = !digitalRead(TOP_LOCK);
bpInt = !digitalRead(INT_LOCK);
bpSmall = !digitalRead(SMALL_LOCK);
bpDown = !digitalRead(DOWN_LOCK);
sendData(msgDoor, bpDoor);
wait(100);
sendData(msgTop, bpTop);
wait(100);
sendData(msgInt, bpInt);
wait(100);
sendData(msgDown, bpDown);
wait(100);
sendData(msgSmall, bpSmall);
}
void loop() {
static uint32_t cRun = millis();
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;
Serial.print(F("Top Lock-"));Serial.println(bTop);
sendData(msgTop, bTop);
}
if(bInt != bpInt){
bpInt = bInt;
Serial.print(F("Int Lock-"));Serial.println(bInt);
sendData(msgInt, bInt);
}
if(bDown != bpDown){
bpDown = bDown;
Serial.print(F("Down Lock-"));Serial.println(bDown);
sendData(msgDown, bDown);
}
if(bSmall != bpSmall){
bpSmall = bSmall;
Serial.print(F("Small Lock-"));Serial.println(bSmall);
sendData(msgSmall, bSmall);
}
if(bDoor != bpDoor){
bpDoor = bDoor;
Serial.print(F("Door-"));Serial.println(bDoor);
sendData(msgDoor, bDoor);
}
if((cRun + 29999) < millis()){
cRun = millis();
send(msgCust.set(cRun));
}
}
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"); // Выводим сообщение "Отправка не удалась"
}
}
}