├── AlarmSystemBoard_RevB.pdf └── AlarmSystem.ino /AlarmSystemBoard_RevB.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vigasan/SecutityAlarm/HEAD/AlarmSystemBoard_RevB.pdf -------------------------------------------------------------------------------- /AlarmSystem.ino: -------------------------------------------------------------------------------- 1 | /************************************************************************************************************************************************** 2 | * File name : AlarmSystem.ino 3 | * Compiler : 4 | * Autor : Vigasan 5 | * Created : 13/02/2023 6 | * Modified : 7 | * Last modified : 8 | * 9 | * 10 | * Description : 11 | * 12 | * Other info : Alarm System Application for ESP32 13 | **************************************************************************************************************************************************/ 14 | 15 | 16 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 17 | /*------------------------------------Include Files----------------------------------------------------------------------------------------------*/ 18 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 19 | #include 20 | #include // Remember to edit this file and change MQTT_MAX_PACKET_SIZE from default 256 to 600 21 | #include 22 | 23 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 24 | /*------------------------------------Local definitions------------------------------------------------------------------------------------------*/ 25 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 26 | 27 | #define PERIOD_MILLSEC_1000 1000 28 | #define PERIOD_MILLSEC_500 500 29 | #define PERIOD_MILLSEC_250 250 30 | 31 | #define DELTA_VOLTAGE 0.05 32 | 33 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 34 | // COMMUNICATION PROTOCOL DEFINES 35 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 36 | #define LODWORD(l) (*( (unsigned long*)(&l))) 37 | #define HIDWORD(l) (*( ( (unsigned long*) (&l) ) + 1 )) 38 | #define MAKEDWORD(hi,lo) ((unsigned long)(((unsigned long)(lo)) | (((unsigned long)(hi))<<16))) 39 | //Word manipulation 40 | #define LOWORD(l) (*( (word*)(&l))) 41 | #define HIWORD(l) (*( ( (word*) (&l) ) + 1 )) 42 | #define MAKEWORD(hi,lo) ((word)(((word)(lo)) | (((word)(hi))<<8))) 43 | //Byte manipulation 44 | #define LOBYTE(w) (*( (byte*) (&w))) 45 | #define HIBYTE(w) (*( ( (byte*) (&w) ) + 1 )) 46 | #define UPBYTE(w) (*( ( (byte*) (&w) ) + 2 )) 47 | #define MSBYTE(w) (*( ( (byte*) (&w) ) + 3 )) 48 | 49 | 50 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 51 | // PROTOCOL FRAME DEFINES 52 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 53 | #define GPL_ST_IDLE 0 54 | #define GPL_ST_CMD 1 55 | #define GPL_ST_NUM_BYTE 2 56 | #define GPL_ST_DATA 3 57 | #define GPL_ST_CHECKSUM 4 58 | 59 | #define GPL_START_FRAME 0x8A 60 | #define GPL_ESCAPE_CHAR 0x8B 61 | #define GPL_XOR_CHAR 0x20 62 | 63 | #define GPL_CMD_TIMEOUT 1000 // Frame Timeout 64 | 65 | #define GPL_BYTE_SOF 0 66 | #define GPL_BYTE_CMD 1 67 | #define GPL_BYTE_LENGTH 2 68 | #define GPL_BYTE_FIRST_DATA 3 69 | 70 | #define GPL_NUM_EXTRA_BYTES 4 71 | 72 | #define LEN_IN_Serial2_BUFFER 100 73 | #define LEN_OUT_Serial2_BUFFER 30 74 | #define LEN_OUT_FRAME_BUFFER 30 75 | 76 | 77 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 78 | // CMD ID FOR PROTOCOL DATA EXCHANGE 79 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 80 | 81 | //------------------------------------------------------ Global CMDs --------------------------------------------------- 82 | #define CMD_CONFIRM 0 83 | #define CMD_IS_BOOTLOADER 1 84 | #define CMD_PROGRAM_VERSION 2 85 | #define CMD_GOTO_BOOTLOADER 3 86 | #define CMD_DEVICE_ID 4 87 | #define CMD_DELAYED_OPERATION 5 88 | #define CMD_HW_VERSION 6 89 | #define CMD_QUIT_BOOTLOADER 7 90 | #define CMD_FLASH_DATA 8 91 | #define CMD_EEPROM_DATA 9 92 | 93 | //------------------------------------------------------ Application Specific CMD s-------------------------------------- 94 | #define CMD_GET_AIN1 10 95 | #define CMD_GET_AIN2 11 96 | #define CMD_GET_AIN3 12 97 | #define CMD_GET_AIN4 13 98 | #define CMD_GET_AIN5 14 99 | #define CMD_GET_AIN6 15 100 | #define CMD_GET_AIN7 16 101 | #define CMD_GET_AIN8 17 102 | #define CMD_GET_DIN1 18 103 | #define CMD_GET_DIN2 19 104 | #define CMD_GET_DIN3 20 105 | #define CMD_GET_DIN4 21 106 | #define CMD_SET_THR_AIN1 22 107 | #define CMD_GET_THR_AIN1 23 108 | #define CMD_SET_THR_AIN2 24 109 | #define CMD_GET_THR_AIN2 25 110 | #define CMD_SET_THR_AIN3 26 111 | #define CMD_GET_THR_AIN3 27 112 | #define CMD_SET_THR_AIN4 28 113 | #define CMD_GET_THR_AIN4 29 114 | #define CMD_SET_THR_AIN5 30 115 | #define CMD_GET_THR_AIN5 31 116 | #define CMD_SET_THR_AIN6 32 117 | #define CMD_GET_THR_AIN6 33 118 | #define CMD_SET_THR_AIN7 34 119 | #define CMD_GET_THR_AIN7 35 120 | #define CMD_SET_THR_AIN8 36 121 | #define CMD_GET_THR_AIN8 37 122 | #define CMD_GET_INPUTS 38 123 | #define CMD_SET_AUX_POWER 39 124 | #define CMD_GET_AUX_POWER 40 125 | #define CMD_SET_TEST_BATTERY 41 126 | #define CMD_GET_TEST_BATTERY 42 127 | #define CMD_SET_RELAY2 43 128 | #define CMD_GET_RELAY2 44 129 | #define CMD_SET_RELAY3 45 130 | #define CMD_GET_RELAY3 46 131 | #define CMD_SET_RELAY4 47 132 | #define CMD_GET_RELAY4 48 133 | #define CMD_SET_RELAY5 49 134 | #define CMD_GET_RELAY5 50 135 | #define CMD_SET_SYSTEM_STATUS 51 136 | #define CMD_GET_SYSTEM_STATUS 52 137 | #define CMD_SET_GUI_STATUS 53 138 | #define CMD_GET_V_EXT 54 139 | #define CMD_GET_V_BATT 55 140 | #define CMD_GET_V_BOARD 56 141 | #define CMD_SET_BUS_POWER 57 142 | #define CMD_GET_BUS_POWER 58 143 | #define CMD_SET_DATE_TIME 59 144 | #define CMD_GET_DATE_TIME 60 145 | #define CMD_INIT_SYSTEM 61 146 | #define CMD_EXT_POWER_STATUS 62 147 | #define CMD_OUTPUTS 63 148 | 149 | 150 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 151 | /*------------------------------------MQTT DISCOVERY PARAMETERS----------------------------------------------------------------------------------*/ 152 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 153 | const char* g_ssid = "Wifi Name"; // Wifi SSID Name 154 | const char* g_password = "Wifi Passoword"; // Wifi Password 155 | const char* g_mqtt_server = "192.168.1.25"; // IP Address of MQTT Broker (Maybe same of Home Assistant) 156 | const char* g_mqttUser = "mosquitto"; // MQTT Broker User 157 | const char* g_mqttPsw = "psw"; // MQTT Broker Password 158 | int g_mqttPort = 1883; // MQTT Broker Port 159 | 160 | const char* g_mqtt_DeviceName = "IOTMB_Alarm"; // Your Device Name 161 | 162 | String g_TopicState_ain1 = "IOTMB_Alarm/ain1/state"; // State Topic for entity input ain1 163 | String g_TopicState_ain2 = "IOTMB_Alarm/ain2/state"; // State Topic for entity input ain2 164 | String g_TopicState_ain3 = "IOTMB_Alarm/ain3/state"; // State Topic for entity input ain3 165 | String g_TopicState_ain4 = "IOTMB_Alarm/ain4/state"; // State Topic for entity input ain4 166 | String g_TopicState_ain5 = "IOTMB_Alarm/ain5/state"; // State Topic for entity input ain5 167 | String g_TopicState_ain6 = "IOTMB_Alarm/ain6/state"; // State Topic for entity input ain6 168 | String g_TopicState_ain7 = "IOTMB_Alarm/ain7/state"; // State Topic for entity input ain7 169 | String g_TopicState_ain8 = "IOTMB_Alarm/ain8/state"; // State Topic for entity input ain8 170 | 171 | String g_TopicState_din1 = "IOTMB_Alarm/din1/state"; // State Topic for entity input din1 172 | String g_TopicState_din2 = "IOTMB_Alarm/din2/state"; // State Topic for entity input din2 173 | String g_TopicState_din3 = "IOTMB_Alarm/din3/state"; // State Topic for entity input din3 174 | String g_TopicState_din4 = "IOTMB_Alarm/din4/state"; // State Topic for entity input din4 175 | 176 | String g_TopicRelay1 = "IOTMB_Alarm/relay1"; // Topic for entity relay1 177 | String g_TopicRelay2 = "IOTMB_Alarm/relay2"; // Topic for entity relay2 178 | String g_TopicRelay3 = "IOTMB_Alarm/relay3"; // Topic for entity relay3 179 | String g_TopicRelay4 = "IOTMB_Alarm/relay4"; // Topic for entity relay4 180 | String g_TopicRelayBell = "IOTMB_Alarm/relayBell"; // Topic for entity relayBell (Alarm Siren) 181 | String g_TopicSwitchBus = "IOTMB_Alarm/switchBus"; // Topic for entity switch Power Bus 182 | String g_TopicSwitchTestBattery = "IOTMB_Alarm/switchTestBatt"; // Topic for entity switch Test Battery 183 | 184 | String g_TopicPowerState = "IOTMB_Alarm/power/state"; // State Topic for entity Power (Check for blackout) 185 | String g_TopicVoltBattery = "IOTMB_Alarm/battery/state"; // State Topic for entity Battery 186 | 187 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 188 | /*------------------------------------Generic global variables-----------------------------------------------------------------------------------*/ 189 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 190 | WiFiClient g_WiFiClient; 191 | PubSubClient g_mqttPubSub(g_WiFiClient); 192 | int g_mqttCounterConn = 0; 193 | String g_UniqueId; 194 | bool g_InitSystem = true; 195 | byte g_InputBuffer[LEN_IN_Serial2_BUFFER]; 196 | byte g_OutputBuffer[LEN_OUT_Serial2_BUFFER]; 197 | byte g_FrameBuffer[LEN_OUT_FRAME_BUFFER]; 198 | byte dataReceived = 0; 199 | byte gplStatus = GPL_ST_IDLE; 200 | byte xored = 0x00; 201 | byte checkSum; 202 | int numByte = 0, i = 0; 203 | double g_VoltagePowerSupply = 0.0; 204 | double g_VoltageBoard = 0.0; 205 | double g_VoltageBattery = 0.0; 206 | word g_Inputs = 0; 207 | byte g_Outputs = 0; 208 | double g_oldVoltagePowerSupply = 1.0; 209 | double g_oldVoltageBoard = 1.0; 210 | double g_oldVoltageBattery = 1.0; 211 | word g_oldInputs = 1; 212 | byte g_oldOutputs = 1; 213 | int g_canPublish = 0; 214 | double delta = 0.0; 215 | unsigned long g_Time = 0; 216 | word g_aIn1 = 0; 217 | word g_old_aIn1 = 0xFFFF; 218 | word g_aIn2 = 0; 219 | word g_old_aIn2 = 0xFFFF; 220 | word g_aIn3 = 0; 221 | word g_old_aIn3 = 0xFFFF; 222 | word g_aIn4 = 0; 223 | word g_old_aIn4 = 0xFFFF; 224 | word g_aIn5 = 0; 225 | word g_old_aIn5 = 0xFFFF; 226 | word g_aIn6 = 0; 227 | word g_old_aIn6 = 0xFFFF; 228 | word g_aIn7 = 0; 229 | word g_old_aIn7 = 0xFFFF; 230 | word g_aIn8 = 0; 231 | word g_old_aIn8 = 0xFFFF; 232 | word g_dIn1 = 0; 233 | word g_old_dIn1 = 0xFFFF; 234 | word g_dIn2 = 0; 235 | word g_old_dIn2 = 0xFFFF; 236 | word g_dIn3 = 0; 237 | word g_old_dIn3 = 0xFFFF; 238 | word g_dIn4 = 0; 239 | word g_old_dIn4 = 0xFFFF; 240 | byte g_Relay1 = 0; 241 | byte g_oldRelay1 = 0xFF; 242 | byte g_Relay2 = 0; 243 | byte g_oldRelay2 = 0xFF; 244 | byte g_Relay3 = 0; 245 | byte g_oldRelay3 = 0xFF; 246 | byte g_Relay4 = 0; 247 | byte g_oldRelay4 = 0xFF; 248 | byte g_RelayBell = 0; 249 | byte g_oldRelayBell = 0xFF; 250 | byte g_SwitchBus = 0; 251 | byte g_oldSwitchBus = 0xFF; 252 | byte g_SwitchTestBattery = 0; 253 | byte g_oldSwitchTestBattery = 0xFF; 254 | String g_PowerStatus = "OFF"; 255 | String g_oldPowerStatus = "ON"; 256 | 257 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 258 | /*------------------------------------ SETUP ----------------------------------------------------------------------------------------------------*/ 259 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 260 | void setup() 261 | { 262 | Serial.begin(115200); 263 | Serial2.begin(9600); 264 | delay(100); //Take some time to open up the Serial2 Monitor 265 | 266 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 267 | // Configurazione I/O 268 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 269 | 270 | Serial.println(""); 271 | Serial.println(""); 272 | Serial.println("-------- IOTMB ALARM ----------"); 273 | 274 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 275 | // Wifi Init 276 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 277 | setup_wifi(); 278 | 279 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 280 | // MQTT Init 281 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 282 | g_mqttPubSub.setServer(g_mqtt_server, g_mqttPort); 283 | g_mqttPubSub.setCallback(MqttReceiverCallback); 284 | } 285 | 286 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 287 | /*------------------------------------ LOOP -----------------------------------------------------------------------------------------------------*/ 288 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 289 | void loop() 290 | { 291 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 292 | // MQTT Connection 293 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 294 | if(WiFi.status() == WL_CONNECTED) 295 | { 296 | if(!g_mqttPubSub.connected()) 297 | MqttReconnect(); 298 | else 299 | g_mqttPubSub.loop(); 300 | } else 301 | { 302 | Serial.println("WiFi NOT connected!!!"); 303 | setup_wifi(); 304 | } 305 | 306 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 307 | // MQTT Discovery Init 308 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 309 | if(g_InitSystem) 310 | { 311 | delay(1000); 312 | g_InitSystem = false; 313 | 314 | MqttHomeAssistantDiscovery(); 315 | 316 | delay(1000); 317 | Serial.println("INIT BOARD..."); 318 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_INIT_SYSTEM, 0); 319 | } 320 | 321 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 322 | // Serial2 - Communication with ATMega2560 323 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 324 | if (Serial2.available() > 0) 325 | { 326 | dataReceived = (byte)Serial2.read(); 327 | if(dataReceived == GPL_ESCAPE_CHAR) 328 | { 329 | xored = GPL_XOR_CHAR; 330 | } else 331 | { 332 | dataReceived ^= xored; 333 | xored = 0x00; 334 | 335 | switch(gplStatus) 336 | { 337 | case GPL_ST_IDLE: 338 | { 339 | if(dataReceived == GPL_START_FRAME) 340 | { 341 | i = 0; 342 | g_InputBuffer[i++] = dataReceived; 343 | checkSum = dataReceived; 344 | gplStatus = GPL_ST_CMD; 345 | } 346 | } break; 347 | 348 | case GPL_ST_CMD: 349 | { 350 | g_InputBuffer[i++] = dataReceived; 351 | checkSum += dataReceived; 352 | gplStatus = GPL_ST_NUM_BYTE; 353 | } break; 354 | 355 | case GPL_ST_NUM_BYTE: 356 | { 357 | numByte = dataReceived; 358 | if(numByte > 0) 359 | { 360 | g_InputBuffer[i++] = dataReceived; 361 | checkSum += dataReceived; 362 | gplStatus = GPL_ST_DATA; 363 | } else 364 | { 365 | gplStatus = GPL_ST_IDLE; 366 | } 367 | } break; 368 | 369 | case GPL_ST_DATA: 370 | { 371 | g_InputBuffer[i++] = dataReceived; 372 | checkSum += dataReceived; 373 | if(--numByte == 0) 374 | gplStatus = GPL_ST_CHECKSUM; 375 | } break; 376 | 377 | case GPL_ST_CHECKSUM: 378 | { 379 | if(dataReceived == checkSum) 380 | { 381 | gplStatus = GPL_ST_IDLE; 382 | g_InputBuffer[i++] = dataReceived; 383 | 384 | switch(g_InputBuffer[GPL_BYTE_CMD]) 385 | { 386 | case CMD_GET_INPUTS: 387 | { 388 | Serial.print("Received Inputs: "); 389 | g_Inputs = GPL_GetWord(g_InputBuffer); 390 | Serial.println(g_Inputs); 391 | 392 | g_aIn1 = ((g_Inputs & 0x0001) > 0) ? 1 : 0; 393 | if(g_aIn1 != g_old_aIn1) 394 | { 395 | g_old_aIn1 = g_aIn1; 396 | MqttPublishStatus_aIn1(); 397 | } 398 | 399 | g_aIn2 = ((g_Inputs & 0x0002) > 0) ? 1 : 0; 400 | if(g_aIn2 != g_old_aIn2) 401 | { 402 | g_old_aIn2 = g_aIn2; 403 | MqttPublishStatus_aIn2(); 404 | } 405 | 406 | g_aIn3 = ((g_Inputs & 0x0004) > 0) ? 1 : 0; 407 | if(g_aIn3 != g_old_aIn3) 408 | { 409 | g_old_aIn3 = g_aIn3; 410 | MqttPublishStatus_aIn3(); 411 | } 412 | 413 | g_aIn4 = ((g_Inputs & 0x0008) > 0) ? 1 : 0; 414 | if(g_aIn4 != g_old_aIn4) 415 | { 416 | g_old_aIn4 = g_aIn4; 417 | MqttPublishStatus_aIn4(); 418 | } 419 | 420 | g_aIn5 = ((g_Inputs & 0x0010) > 0) ? 1 : 0; 421 | if(g_aIn5 != g_old_aIn5) 422 | { 423 | g_old_aIn5 = g_aIn5; 424 | MqttPublishStatus_aIn5(); 425 | } 426 | 427 | g_aIn6 = ((g_Inputs & 0x0020) > 0) ? 1 : 0; 428 | if(g_aIn6 != g_old_aIn6) 429 | { 430 | g_old_aIn6 = g_aIn6; 431 | MqttPublishStatus_aIn6(); 432 | } 433 | 434 | g_aIn7 = ((g_Inputs & 0x0040) > 0) ? 1 : 0; 435 | if(g_aIn7 != g_old_aIn7) 436 | { 437 | g_old_aIn7 = g_aIn7; 438 | MqttPublishStatus_aIn7(); 439 | } 440 | 441 | g_aIn8 = ((g_Inputs & 0x0080) > 0) ? 1 : 0; 442 | if(g_aIn8 != g_old_aIn8) 443 | { 444 | g_old_aIn8 = g_aIn8; 445 | MqttPublishStatus_aIn8(); 446 | } 447 | 448 | g_dIn1 = ((g_Inputs & 0x0100) > 0) ? 1 : 0; 449 | if(g_dIn1 != g_old_dIn1) 450 | { 451 | g_old_dIn1 = g_dIn1; 452 | MqttPublishStatus_dIn1(); 453 | } 454 | 455 | g_dIn2 = ((g_Inputs & 0x0200) > 0) ? 1 : 0; 456 | if(g_dIn2 != g_old_dIn2) 457 | { 458 | g_old_dIn2 = g_dIn2; 459 | MqttPublishStatus_dIn2(); 460 | } 461 | 462 | g_dIn3 = ((g_Inputs & 0x0400) > 0) ? 1 : 0; 463 | if(g_dIn3 != g_old_dIn3) 464 | { 465 | g_old_dIn3 = g_dIn3; 466 | MqttPublishStatus_dIn3(); 467 | } 468 | 469 | g_dIn4 = ((g_Inputs & 0x0800) > 0) ? 1 : 0; 470 | if(g_dIn4 != g_old_dIn4) 471 | { 472 | g_old_dIn4 = g_dIn4; 473 | MqttPublishStatus_dIn4(); 474 | } 475 | } break; 476 | 477 | case CMD_OUTPUTS: 478 | { 479 | Serial.print("Received Outputs: "); 480 | g_Outputs = GPL_GetByte(g_InputBuffer); 481 | Serial.println(g_Outputs); 482 | 483 | g_Relay1 = ((g_Outputs & 0x01) > 0) ? 1 : 0; 484 | if(g_Relay1 != g_oldRelay1) 485 | { 486 | g_oldRelay1 = g_Relay1; 487 | MqttPublishStatus_Relay1(); 488 | } 489 | 490 | g_Relay2 = ((g_Outputs & 0x02) > 0) ? 1 : 0; 491 | if(g_Relay2 != g_oldRelay2) 492 | { 493 | g_oldRelay2 = g_Relay2; 494 | MqttPublishStatus_Relay2(); 495 | } 496 | 497 | g_Relay3 = ((g_Outputs & 0x04) > 0) ? 1 : 0; 498 | if(g_Relay3 != g_oldRelay3) 499 | { 500 | g_oldRelay3 = g_Relay3; 501 | MqttPublishStatus_Relay3(); 502 | } 503 | 504 | g_Relay4 = ((g_Outputs & 0x08) > 0) ? 1 : 0; 505 | if(g_Relay4 != g_oldRelay4) 506 | { 507 | g_oldRelay4 = g_Relay4; 508 | MqttPublishStatus_Relay4(); 509 | } 510 | 511 | g_RelayBell = ((g_Outputs & 0x10) > 0) ? 1 : 0; 512 | if(g_RelayBell != g_oldRelayBell) 513 | { 514 | g_oldRelayBell = g_RelayBell; 515 | MqttPublishStatus_RelayBell(); 516 | } 517 | 518 | g_SwitchBus = ((g_Outputs & 0x40) > 0) ? 1 : 0; 519 | if(g_SwitchBus != g_oldSwitchBus) 520 | { 521 | g_oldSwitchBus = g_SwitchBus; 522 | MqttPublishStatus_SwitchBus(); 523 | } 524 | 525 | g_SwitchTestBattery = ((g_Outputs & 0x20) > 0) ? 1 : 0; 526 | if(g_SwitchTestBattery != g_oldSwitchTestBattery) 527 | { 528 | g_oldSwitchTestBattery = g_SwitchTestBattery; 529 | MqttPublishStatus_SwitchTestBattery(); 530 | } 531 | } break; 532 | 533 | case CMD_EXT_POWER_STATUS: 534 | { 535 | Serial.print("Received Power Status: "); 536 | if(GPL_GetByte(g_InputBuffer) == 1) 537 | g_PowerStatus = "ON"; 538 | else 539 | g_PowerStatus = "OFF"; 540 | Serial.println(g_PowerStatus); 541 | MqttPublishStatus_Power(); 542 | } break; 543 | 544 | case CMD_GET_V_BATT: 545 | { 546 | Serial.print("Received V Batt: "); 547 | g_VoltageBattery = (double)(15 * GPL_GetWord(g_InputBuffer)) / 1024; 548 | Serial.println(g_VoltageBattery); 549 | MqttPublishStatus_Battery(); 550 | } break; 551 | } 552 | } 553 | 554 | } break; 555 | } 556 | } 557 | } 558 | } 559 | 560 | 561 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 562 | /*------------------------------------ Public Functions -----------------------------------------------------------------------------------------*/ 563 | /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ 564 | 565 | void setup_wifi() 566 | { 567 | int counter = 0; 568 | byte mac[6]; 569 | delay(10); 570 | // We start by connecting to a WiFi network 571 | Serial.println(); 572 | Serial.print("Connecting to "); 573 | Serial.println(g_ssid); 574 | 575 | WiFi.begin(g_ssid, g_password); 576 | 577 | WiFi.macAddress(mac); 578 | g_UniqueId = String(mac[0],HEX) +String(mac[1],HEX) +String(mac[2],HEX) +String(mac[3],HEX) + String(mac[4],HEX) + String(mac[5],HEX); 579 | 580 | Serial.print("Unique ID: "); 581 | Serial.println(g_UniqueId); 582 | 583 | while(WiFi.status() != WL_CONNECTED && counter++ < 5) 584 | { 585 | delay(500); 586 | Serial.print("."); 587 | } 588 | 589 | if(WiFi.status() == WL_CONNECTED) 590 | { 591 | Serial.println("WiFi connected"); 592 | Serial.print("IP address: "); 593 | Serial.println(WiFi.localIP()); 594 | } else 595 | { 596 | Serial.println("WiFi NOT connected!!!"); 597 | } 598 | } 599 | 600 | void MqttReconnect() 601 | { 602 | // Loop until we're MqttReconnected 603 | while (!g_mqttPubSub.connected() && (g_mqttCounterConn++ < 4)) 604 | { 605 | Serial.print("Attempting MQTT connection..."); 606 | if (g_mqttPubSub.connect(g_mqtt_DeviceName, g_mqttUser, g_mqttPsw)) 607 | { 608 | Serial.println("connected"); 609 | 610 | // ESP32 Subscribe following topics 611 | // Home assistant status 612 | g_mqttPubSub.subscribe("homeassistant/status"); 613 | 614 | g_mqttPubSub.subscribe((g_TopicRelay1 + "/set").c_str()); 615 | g_mqttPubSub.subscribe((g_TopicRelay2 + "/set").c_str()); 616 | g_mqttPubSub.subscribe((g_TopicRelay3 + "/set").c_str()); 617 | g_mqttPubSub.subscribe((g_TopicRelay4 + "/set").c_str()); 618 | g_mqttPubSub.subscribe((g_TopicRelayBell + "/set").c_str()); 619 | g_mqttPubSub.subscribe((g_TopicSwitchBus + "/set").c_str()); 620 | g_mqttPubSub.subscribe((g_TopicSwitchTestBattery + "/set").c_str()); 621 | 622 | delay(500); 623 | } else 624 | { 625 | Serial.print("failed, rc="); 626 | Serial.print(g_mqttPubSub.state()); 627 | Serial.println(" try again in 3 seconds"); 628 | delay(3000); 629 | } 630 | } 631 | g_mqttCounterConn = 0; 632 | } 633 | 634 | void MqttReceiverCallback(char* topic, byte* inFrame, unsigned int length) 635 | { 636 | Serial.print("Message arrived on topic: "); 637 | Serial.print(topic); 638 | Serial.print(". Message: "); 639 | byte state = 0; 640 | String payload; 641 | String topicMsg; 642 | 643 | for (int i = 0; i < length; i++) 644 | { 645 | Serial.print((char)inFrame[i]); 646 | payload += (char)inFrame[i]; 647 | } 648 | Serial.println(); 649 | 650 | if(payload == "ON") 651 | state = 1; 652 | else if(payload == "OFF") 653 | state = 0; 654 | 655 | 656 | if(String(topic) == String("homeassistant/status")) 657 | { 658 | if(payload == "online") 659 | { 660 | MqttHomeAssistantDiscovery(); 661 | delay(1000); 662 | Serial.println("INIT BOARD..."); 663 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_INIT_SYSTEM, 0); 664 | } 665 | } 666 | 667 | if(String(topic) == String(g_TopicRelay1 + "/set")) 668 | { 669 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY2, state); 670 | } 671 | 672 | if(String(topic) == String(g_TopicRelay2 + "/set")) 673 | { 674 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY3, state); 675 | } 676 | 677 | if(String(topic) == String(g_TopicRelay3 + "/set")) 678 | { 679 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY4, state); 680 | } 681 | 682 | if(String(topic) == String(g_TopicRelay4 + "/set")) 683 | { 684 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_RELAY5, state); 685 | } 686 | 687 | if(String(topic) == String(g_TopicRelayBell + "/set")) 688 | { 689 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_AUX_POWER, state); 690 | } 691 | 692 | if(String(topic) == String(g_TopicSwitchBus + "/set")) 693 | { 694 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_BUS_POWER, state); 695 | } 696 | 697 | if(String(topic) == String(g_TopicSwitchTestBattery + "/set")) 698 | { 699 | GPL_SendByte(ID_ESP32, ID_MOTHER_BOARD, CMD_SET_TEST_BATTERY, state); 700 | } 701 | } 702 | 703 | void MqttHomeAssistantDiscovery() 704 | { 705 | String discoveryTopic; 706 | String payload; 707 | String strPayload; 708 | 709 | if(g_mqttPubSub.connected()) 710 | { 711 | Serial.println("SEND HOME ASSISTANT DISCOVERY!!!"); 712 | StaticJsonDocument<600> payload; 713 | JsonObject device; 714 | JsonArray identifiers; 715 | 716 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 717 | // Analog Input 1 718 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 719 | discoveryTopic = "homeassistant/binary_sensor/ain1/config"; // Discovery Topic for entity ain1 720 | 721 | payload["name"] = "IOTMB_Alarm.ain1"; 722 | payload["uniq_id"] = g_UniqueId + "_ain1"; 723 | payload["stat_t"] = g_TopicState_ain1; 724 | payload["dev_cla"] = "door"; 725 | device = payload.createNestedObject("device"); 726 | device["name"] = g_mqtt_DeviceName; 727 | device["model"] = "IOTMB_RevA"; 728 | device["manufacturer"] = "VLC"; 729 | identifiers = device.createNestedArray("identifiers"); 730 | identifiers.add(g_UniqueId); 731 | 732 | serializeJsonPretty(payload, Serial); 733 | Serial.println(" "); 734 | serializeJson(payload, strPayload); 735 | 736 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 737 | delay(100); 738 | 739 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 740 | // Analog Input 2 741 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 742 | discoveryTopic = "homeassistant/binary_sensor/ain2/config"; 743 | payload.clear(); 744 | device.clear(); 745 | identifiers.clear(); 746 | strPayload.clear(); 747 | 748 | payload["name"] = "IOTMB_Alarm.ain2"; 749 | payload["uniq_id"] = g_UniqueId + "_ain2"; 750 | payload["stat_t"] = g_TopicState_ain2; 751 | payload["dev_cla"] = "door"; 752 | device = payload.createNestedObject("device"); 753 | device["name"] = g_mqtt_DeviceName; 754 | device["model"] = "IOTMB_RevA"; 755 | device["manufacturer"] = "VLC"; 756 | identifiers = device.createNestedArray("identifiers"); 757 | identifiers.add(g_UniqueId); 758 | 759 | serializeJsonPretty(payload, Serial); 760 | Serial.println(" "); 761 | serializeJson(payload, strPayload); 762 | 763 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 764 | delay(100); 765 | 766 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 767 | // Analog Input 3 768 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 769 | discoveryTopic = "homeassistant/binary_sensor/ain3/config"; 770 | payload.clear(); 771 | device.clear(); 772 | identifiers.clear(); 773 | strPayload.clear(); 774 | 775 | payload["name"] = "IOTMB_Alarm.ain3"; 776 | payload["uniq_id"] = g_UniqueId + "_ain3"; 777 | payload["stat_t"] = g_TopicState_ain3; 778 | payload["dev_cla"] = "door"; 779 | device = payload.createNestedObject("device"); 780 | device["name"] = g_mqtt_DeviceName; 781 | device["model"] = "IOTMB_RevA"; 782 | device["manufacturer"] = "VLC"; 783 | identifiers = device.createNestedArray("identifiers"); 784 | identifiers.add(g_UniqueId); 785 | 786 | serializeJsonPretty(payload, Serial); 787 | Serial.println(" "); 788 | serializeJson(payload, strPayload); 789 | 790 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 791 | delay(100); 792 | 793 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 794 | // Analog Input 4 795 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 796 | discoveryTopic = "homeassistant/binary_sensor/ain4/config"; 797 | payload.clear(); 798 | device.clear(); 799 | identifiers.clear(); 800 | strPayload.clear(); 801 | 802 | payload["name"] = "IOTMB_Alarm.ain4"; 803 | payload["uniq_id"] = g_UniqueId + "_ain4"; 804 | payload["stat_t"] = g_TopicState_ain4; 805 | payload["dev_cla"] = "door"; 806 | device = payload.createNestedObject("device"); 807 | device["name"] = g_mqtt_DeviceName; 808 | device["model"] = "IOTMB_RevA"; 809 | device["manufacturer"] = "VLC"; 810 | identifiers = device.createNestedArray("identifiers"); 811 | identifiers.add(g_UniqueId); 812 | 813 | serializeJsonPretty(payload, Serial); 814 | Serial.println(" "); 815 | serializeJson(payload, strPayload); 816 | 817 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 818 | delay(100); 819 | 820 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 821 | // Analog Input 5 822 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 823 | discoveryTopic = "homeassistant/binary_sensor/ain5/config"; 824 | payload.clear(); 825 | device.clear(); 826 | identifiers.clear(); 827 | strPayload.clear(); 828 | 829 | payload["name"] = "IOTMB_Alarm.ain5"; 830 | payload["uniq_id"] = g_UniqueId + "_ain5"; 831 | payload["stat_t"] = g_TopicState_ain5; 832 | payload["dev_cla"] = "door"; 833 | device = payload.createNestedObject("device"); 834 | device["name"] = g_mqtt_DeviceName; 835 | device["model"] = "IOTMB_RevA"; 836 | device["manufacturer"] = "VLC"; 837 | identifiers = device.createNestedArray("identifiers"); 838 | identifiers.add(g_UniqueId); 839 | 840 | serializeJsonPretty(payload, Serial); 841 | Serial.println(" "); 842 | serializeJson(payload, strPayload); 843 | 844 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 845 | delay(100); 846 | 847 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 848 | // Analog Input 6 849 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 850 | discoveryTopic = "homeassistant/binary_sensor/ain6/config"; 851 | payload.clear(); 852 | device.clear(); 853 | identifiers.clear(); 854 | strPayload.clear(); 855 | 856 | payload["name"] = "IOTMB_Alarm.ain6"; 857 | payload["uniq_id"] = g_UniqueId + "_ain6"; 858 | payload["stat_t"] = g_TopicState_ain6; 859 | payload["dev_cla"] = "door"; 860 | device = payload.createNestedObject("device"); 861 | device["name"] = g_mqtt_DeviceName; 862 | device["model"] = "IOTMB_RevA"; 863 | device["manufacturer"] = "VLC"; 864 | identifiers = device.createNestedArray("identifiers"); 865 | identifiers.add(g_UniqueId); 866 | 867 | serializeJsonPretty(payload, Serial); 868 | Serial.println(" "); 869 | serializeJson(payload, strPayload); 870 | 871 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 872 | delay(100); 873 | 874 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 875 | // Analog Input 7 876 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 877 | discoveryTopic = "homeassistant/binary_sensor/ain7/config"; 878 | payload.clear(); 879 | device.clear(); 880 | identifiers.clear(); 881 | strPayload.clear(); 882 | 883 | payload["name"] = "IOTMB_Alarm.ain7"; 884 | payload["uniq_id"] = g_UniqueId + "_ain7"; 885 | payload["stat_t"] = g_TopicState_ain7; 886 | payload["dev_cla"] = "door"; 887 | device = payload.createNestedObject("device"); 888 | device["name"] = g_mqtt_DeviceName; 889 | device["model"] = "IOTMB_RevA"; 890 | device["manufacturer"] = "VLC"; 891 | identifiers = device.createNestedArray("identifiers"); 892 | identifiers.add(g_UniqueId); 893 | 894 | serializeJsonPretty(payload, Serial); 895 | Serial.println(" "); 896 | serializeJson(payload, strPayload); 897 | 898 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 899 | delay(100); 900 | 901 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 902 | // Analog Input 8 903 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 904 | discoveryTopic = "homeassistant/binary_sensor/ain8/config"; 905 | payload.clear(); 906 | device.clear(); 907 | identifiers.clear(); 908 | strPayload.clear(); 909 | 910 | payload["name"] = "IOTMB_Alarm.ain8"; 911 | payload["uniq_id"] = g_UniqueId + "_ain8"; 912 | payload["stat_t"] = g_TopicState_ain8; 913 | payload["dev_cla"] = "door"; 914 | device = payload.createNestedObject("device"); 915 | device["name"] = g_mqtt_DeviceName; 916 | device["model"] = "IOTMB_RevA"; 917 | device["manufacturer"] = "VLC"; 918 | identifiers = device.createNestedArray("identifiers"); 919 | identifiers.add(g_UniqueId); 920 | 921 | serializeJsonPretty(payload, Serial); 922 | Serial.println(" "); 923 | serializeJson(payload, strPayload); 924 | 925 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 926 | delay(100); 927 | 928 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 929 | // Digital Input 1 930 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 931 | discoveryTopic = "homeassistant/binary_sensor/din1/config"; 932 | payload.clear(); 933 | device.clear(); 934 | identifiers.clear(); 935 | strPayload.clear(); 936 | 937 | payload["name"] = "IOTMB_Alarm.din1"; 938 | payload["uniq_id"] = g_UniqueId + "_din1"; 939 | payload["stat_t"] = g_TopicState_din1; 940 | device = payload.createNestedObject("device"); 941 | device["name"] = g_mqtt_DeviceName; 942 | device["model"] = "IOTMB_RevA"; 943 | device["manufacturer"] = "VLC"; 944 | identifiers = device.createNestedArray("identifiers"); 945 | identifiers.add(g_UniqueId); 946 | 947 | serializeJsonPretty(payload, Serial); 948 | Serial.println(" "); 949 | serializeJson(payload, strPayload); 950 | 951 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 952 | delay(100); 953 | 954 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 955 | // Digital Input 2 956 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 957 | discoveryTopic = "homeassistant/binary_sensor/din2/config"; 958 | payload.clear(); 959 | device.clear(); 960 | identifiers.clear(); 961 | strPayload.clear(); 962 | 963 | payload["name"] = "IOTMB_Alarm.din2"; 964 | payload["uniq_id"] = g_UniqueId + "_din2"; 965 | payload["stat_t"] = g_TopicState_din2; 966 | device = payload.createNestedObject("device"); 967 | device["name"] = g_mqtt_DeviceName; 968 | device["model"] = "IOTMB_RevA"; 969 | device["manufacturer"] = "VLC"; 970 | identifiers = device.createNestedArray("identifiers"); 971 | identifiers.add(g_UniqueId); 972 | 973 | serializeJsonPretty(payload, Serial); 974 | Serial.println(" "); 975 | serializeJson(payload, strPayload); 976 | 977 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 978 | delay(100); 979 | 980 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 981 | // Digital Input 3 982 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 983 | discoveryTopic = "homeassistant/binary_sensor/din3/config"; 984 | payload.clear(); 985 | device.clear(); 986 | identifiers.clear(); 987 | strPayload.clear(); 988 | 989 | payload["name"] = "IOTMB_Alarm.din3"; 990 | payload["uniq_id"] = g_UniqueId + "_din3"; 991 | payload["stat_t"] = g_TopicState_din3; 992 | device = payload.createNestedObject("device"); 993 | device["name"] = g_mqtt_DeviceName; 994 | device["model"] = "IOTMB_RevA"; 995 | device["manufacturer"] = "VLC"; 996 | identifiers = device.createNestedArray("identifiers"); 997 | identifiers.add(g_UniqueId); 998 | 999 | serializeJsonPretty(payload, Serial); 1000 | Serial.println(" "); 1001 | serializeJson(payload, strPayload); 1002 | 1003 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1004 | delay(100); 1005 | 1006 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1007 | // Digital Input 4 1008 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1009 | discoveryTopic = "homeassistant/binary_sensor/din4/config"; 1010 | payload.clear(); 1011 | device.clear(); 1012 | identifiers.clear(); 1013 | strPayload.clear(); 1014 | 1015 | payload["name"] = "IOTMB_Alarm.din4"; 1016 | payload["uniq_id"] = g_UniqueId + "_din4"; 1017 | payload["stat_t"] = g_TopicState_din4; 1018 | device = payload.createNestedObject("device"); 1019 | device["name"] = g_mqtt_DeviceName; 1020 | device["model"] = "IOTMB_RevA"; 1021 | device["manufacturer"] = "VLC"; 1022 | identifiers = device.createNestedArray("identifiers"); 1023 | identifiers.add(g_UniqueId); 1024 | 1025 | serializeJsonPretty(payload, Serial); 1026 | Serial.println(" "); 1027 | serializeJson(payload, strPayload); 1028 | 1029 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1030 | delay(100); 1031 | 1032 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1033 | // Relay Sirena 1034 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1035 | discoveryTopic = "homeassistant/switch/relayBell/config"; 1036 | payload.clear(); 1037 | device.clear(); 1038 | identifiers.clear(); 1039 | strPayload.clear(); 1040 | 1041 | payload["name"] = "IOTMB_Alarm.Bell"; 1042 | payload["uniq_id"] = g_UniqueId + "_bell"; 1043 | payload["stat_t"] = g_TopicRelayBell + "/state"; 1044 | payload["cmd_t"] = g_TopicRelayBell + "/set"; 1045 | device = payload.createNestedObject("device"); 1046 | device["name"] = "IOTMB_Alarm"; 1047 | device["model"] = "IOTMB_RevA"; 1048 | device["manufacturer"] = "VLC"; 1049 | identifiers = device.createNestedArray("identifiers"); 1050 | identifiers.add(g_UniqueId); 1051 | 1052 | serializeJsonPretty(payload, Serial); 1053 | Serial.println(" "); 1054 | serializeJson(payload, strPayload); 1055 | 1056 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1057 | delay(100); 1058 | 1059 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1060 | // Relay OUT1 1061 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1062 | discoveryTopic = "homeassistant/switch/relay1/config"; 1063 | payload.clear(); 1064 | device.clear(); 1065 | identifiers.clear(); 1066 | strPayload.clear(); 1067 | 1068 | payload["name"] = "IOTMB_Alarm.relay1"; 1069 | payload["uniq_id"] = g_UniqueId + "_relay1"; 1070 | payload["stat_t"] = g_TopicRelay1 + "/state"; 1071 | payload["cmd_t"] = g_TopicRelay1 + "/set"; 1072 | device = payload.createNestedObject("device"); 1073 | device["name"] = "IOTMB_Alarm"; 1074 | device["model"] = "IOTMB_RevA"; 1075 | device["manufacturer"] = "VLC"; 1076 | identifiers = device.createNestedArray("identifiers"); 1077 | identifiers.add(g_UniqueId); 1078 | 1079 | serializeJsonPretty(payload, Serial); 1080 | Serial.println(" "); 1081 | serializeJson(payload, strPayload); 1082 | 1083 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1084 | delay(100); 1085 | 1086 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1087 | // Relay OUT2 1088 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1089 | discoveryTopic = "homeassistant/switch/relay2/config"; 1090 | payload.clear(); 1091 | device.clear(); 1092 | identifiers.clear(); 1093 | strPayload.clear(); 1094 | 1095 | payload["name"] = "IOTMB_Alarm.relay2"; 1096 | payload["uniq_id"] = g_UniqueId + "_relay2"; 1097 | payload["stat_t"] = g_TopicRelay2 + "/state"; 1098 | payload["cmd_t"] = g_TopicRelay2 + "/set"; 1099 | device = payload.createNestedObject("device"); 1100 | device["name"] = "IOTMB_Alarm"; 1101 | device["model"] = "IOTMB_RevA"; 1102 | device["manufacturer"] = "VLC"; 1103 | identifiers = device.createNestedArray("identifiers"); 1104 | identifiers.add(g_UniqueId); 1105 | 1106 | serializeJsonPretty(payload, Serial); 1107 | Serial.println(" "); 1108 | serializeJson(payload, strPayload); 1109 | 1110 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1111 | delay(100); 1112 | 1113 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1114 | // Relay OUT3 1115 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1116 | discoveryTopic = "homeassistant/switch/relay3/config"; 1117 | payload.clear(); 1118 | device.clear(); 1119 | identifiers.clear(); 1120 | strPayload.clear(); 1121 | 1122 | payload["name"] = "IOTMB_Alarm.relay3"; 1123 | payload["uniq_id"] = g_UniqueId + "_relay3"; 1124 | payload["stat_t"] = g_TopicRelay3 + "/state"; 1125 | payload["cmd_t"] = g_TopicRelay3 + "/set"; 1126 | device = payload.createNestedObject("device"); 1127 | device["name"] = "IOTMB_Alarm"; 1128 | device["model"] = "IOTMB_RevA"; 1129 | device["manufacturer"] = "VLC"; 1130 | identifiers = device.createNestedArray("identifiers"); 1131 | identifiers.add(g_UniqueId); 1132 | 1133 | serializeJsonPretty(payload, Serial); 1134 | Serial.println(" "); 1135 | serializeJson(payload, strPayload); 1136 | 1137 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1138 | delay(100); 1139 | 1140 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1141 | // Relay OUT4 1142 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1143 | discoveryTopic = "homeassistant/switch/relay4/config"; 1144 | payload.clear(); 1145 | device.clear(); 1146 | identifiers.clear(); 1147 | strPayload.clear(); 1148 | 1149 | payload["name"] = "IOTMB_Alarm.relay4"; 1150 | payload["uniq_id"] = g_UniqueId + "_relay4"; 1151 | payload["stat_t"] = g_TopicRelay4 + "/state"; 1152 | payload["cmd_t"] = g_TopicRelay4 + "/set"; 1153 | device = payload.createNestedObject("device"); 1154 | device["name"] = "IOTMB_Alarm"; 1155 | device["model"] = "IOTMB_RevA"; 1156 | device["manufacturer"] = "VLC"; 1157 | identifiers = device.createNestedArray("identifiers"); 1158 | identifiers.add(g_UniqueId); 1159 | 1160 | serializeJsonPretty(payload, Serial); 1161 | Serial.println(" "); 1162 | serializeJson(payload, strPayload); 1163 | 1164 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1165 | delay(100); 1166 | 1167 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1168 | // Switch Power Bus 1169 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1170 | discoveryTopic = "homeassistant/switch/switchBus/config"; 1171 | payload.clear(); 1172 | device.clear(); 1173 | identifiers.clear(); 1174 | strPayload.clear(); 1175 | 1176 | payload["name"] = "IOTMB_Alarm.Bus"; 1177 | payload["uniq_id"] = g_UniqueId + "_switchBus"; 1178 | payload["stat_t"] = g_TopicSwitchBus + "/state"; 1179 | payload["cmd_t"] = g_TopicSwitchBus + "/set"; 1180 | device = payload.createNestedObject("device"); 1181 | device["name"] = "IOTMB_Alarm"; 1182 | device["model"] = "IOTMB_RevA"; 1183 | device["manufacturer"] = "VLC"; 1184 | identifiers = device.createNestedArray("identifiers"); 1185 | identifiers.add(g_UniqueId); 1186 | 1187 | serializeJsonPretty(payload, Serial); 1188 | Serial.println(" "); 1189 | serializeJson(payload, strPayload); 1190 | 1191 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1192 | delay(100); 1193 | 1194 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1195 | // Switch Test Battery 1196 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1197 | discoveryTopic = "homeassistant/switch/switchTestBatt/config"; 1198 | payload.clear(); 1199 | device.clear(); 1200 | identifiers.clear(); 1201 | strPayload.clear(); 1202 | 1203 | payload["name"] = "IOTMB_Alarm.TestBattery"; 1204 | payload["uniq_id"] = g_UniqueId + "_switcheTestBatt"; 1205 | payload["stat_t"] = g_TopicSwitchTestBattery + "/state"; 1206 | payload["cmd_t"] = g_TopicSwitchTestBattery + "/set"; 1207 | device = payload.createNestedObject("device"); 1208 | device["name"] = "IOTMB_Alarm"; 1209 | device["model"] = "IOTMB_RevA"; 1210 | device["manufacturer"] = "VLC"; 1211 | identifiers = device.createNestedArray("identifiers"); 1212 | identifiers.add(g_UniqueId); 1213 | 1214 | serializeJsonPretty(payload, Serial); 1215 | Serial.println(" "); 1216 | serializeJson(payload, strPayload); 1217 | 1218 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1219 | delay(100); 1220 | 1221 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1222 | // Binary Power Status 1223 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1224 | payload.clear(); 1225 | device.clear(); 1226 | identifiers.clear(); 1227 | strPayload.clear(); 1228 | 1229 | discoveryTopic = "homeassistant/binary_sensor/powerState/config"; 1230 | 1231 | payload["name"] = "IOTMB_Alarm.powerstate"; 1232 | payload["uniq_id"] = g_UniqueId + "_binary"; 1233 | payload["stat_t"] = g_TopicPowerState; 1234 | //payload["val_tpl"] = "{{ value_json.pwrStatus | is_defined }}"; 1235 | payload["dev_cla"] = "power"; 1236 | device = payload.createNestedObject("device"); 1237 | device["name"] = "IOTMB_Alarm"; 1238 | device["model"] = "IOTMB_RevA"; 1239 | device["manufacturer"] = "VLC"; 1240 | identifiers = device.createNestedArray("identifiers"); 1241 | identifiers.add(g_UniqueId); 1242 | 1243 | serializeJsonPretty(payload, Serial); 1244 | Serial.println(" "); 1245 | serializeJson(payload, strPayload); 1246 | 1247 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1248 | delay(100); 1249 | 1250 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1251 | // Sensor Battery Voltage 1252 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1253 | discoveryTopic = "homeassistant/sensor/battVoltage/config"; 1254 | payload.clear(); 1255 | device.clear(); 1256 | identifiers.clear(); 1257 | strPayload.clear(); 1258 | 1259 | payload["name"] = "IOTMB_Alarm.voltBatt"; 1260 | payload["uniq_id"] = g_UniqueId + "_battVolt"; 1261 | payload["stat_t"] = g_TopicVoltBattery; 1262 | payload["unit_of_meas"] = "V"; 1263 | //payload["val_tpl"] = "{{ value_json.battVolt | is_defined }}"; 1264 | device = payload.createNestedObject("device"); 1265 | device["name"] = "IOTMB_Alarm"; 1266 | device["model"] = "IOTMB_RevA"; 1267 | device["manufacturer"] = "VLC"; 1268 | identifiers = device.createNestedArray("identifiers"); 1269 | identifiers.add(g_UniqueId); 1270 | 1271 | serializeJsonPretty(payload, Serial); 1272 | Serial.println(" "); 1273 | serializeJson(payload, strPayload); 1274 | 1275 | g_mqttPubSub.publish(discoveryTopic.c_str(), strPayload.c_str()); 1276 | delay(100); 1277 | } 1278 | } 1279 | 1280 | void MqttPublishStatus_aIn1() 1281 | { 1282 | String strPayload; 1283 | if(g_mqttPubSub.connected()) 1284 | { 1285 | if(g_aIn1 == 0) 1286 | strPayload = "OFF"; 1287 | else 1288 | strPayload = "ON"; 1289 | 1290 | g_mqttPubSub.publish(g_TopicState_ain1.c_str(), strPayload.c_str()); 1291 | } 1292 | } 1293 | 1294 | void MqttPublishStatus_aIn2() 1295 | { 1296 | String strPayload; 1297 | if(g_mqttPubSub.connected()) 1298 | { 1299 | if(g_aIn2 == 0) 1300 | strPayload = "OFF"; 1301 | else 1302 | strPayload = "ON"; 1303 | 1304 | g_mqttPubSub.publish(g_TopicState_ain2.c_str(), strPayload.c_str()); 1305 | } 1306 | } 1307 | 1308 | void MqttPublishStatus_aIn3() 1309 | { 1310 | String strPayload; 1311 | if(g_mqttPubSub.connected()) 1312 | { 1313 | if(g_aIn3 == 0) 1314 | strPayload = "OFF"; 1315 | else 1316 | strPayload = "ON"; 1317 | 1318 | g_mqttPubSub.publish(g_TopicState_ain3.c_str(), strPayload.c_str()); 1319 | } 1320 | } 1321 | 1322 | void MqttPublishStatus_aIn4() 1323 | { 1324 | String strPayload; 1325 | if(g_mqttPubSub.connected()) 1326 | { 1327 | if(g_aIn4 == 0) 1328 | strPayload = "OFF"; 1329 | else 1330 | strPayload = "ON"; 1331 | 1332 | g_mqttPubSub.publish(g_TopicState_ain4.c_str(), strPayload.c_str()); 1333 | } 1334 | } 1335 | 1336 | void MqttPublishStatus_aIn5() 1337 | { 1338 | String strPayload; 1339 | if(g_mqttPubSub.connected()) 1340 | { 1341 | if(g_aIn5 == 0) 1342 | strPayload = "OFF"; 1343 | else 1344 | strPayload = "ON"; 1345 | 1346 | g_mqttPubSub.publish(g_TopicState_ain5.c_str(), strPayload.c_str()); 1347 | } 1348 | } 1349 | 1350 | void MqttPublishStatus_aIn6() 1351 | { 1352 | String strPayload; 1353 | if(g_mqttPubSub.connected()) 1354 | { 1355 | if(g_aIn6 == 0) 1356 | strPayload = "OFF"; 1357 | else 1358 | strPayload = "ON"; 1359 | 1360 | g_mqttPubSub.publish(g_TopicState_ain6.c_str(), strPayload.c_str()); 1361 | } 1362 | } 1363 | 1364 | void MqttPublishStatus_aIn7() 1365 | { 1366 | String strPayload; 1367 | if(g_mqttPubSub.connected()) 1368 | { 1369 | if(g_aIn7 == 0) 1370 | strPayload = "OFF"; 1371 | else 1372 | strPayload = "ON"; 1373 | 1374 | g_mqttPubSub.publish(g_TopicState_ain7.c_str(), strPayload.c_str()); 1375 | } 1376 | } 1377 | 1378 | void MqttPublishStatus_aIn8() 1379 | { 1380 | String strPayload; 1381 | if(g_mqttPubSub.connected()) 1382 | { 1383 | if(g_aIn8 == 0) 1384 | strPayload = "OFF"; 1385 | else 1386 | strPayload = "ON"; 1387 | 1388 | g_mqttPubSub.publish(g_TopicState_ain8.c_str(), strPayload.c_str()); 1389 | } 1390 | } 1391 | 1392 | void MqttPublishStatus_dIn1() 1393 | { 1394 | String strPayload; 1395 | if(g_mqttPubSub.connected()) 1396 | { 1397 | if(g_dIn1 == 0) 1398 | strPayload = "OFF"; 1399 | else 1400 | strPayload = "ON"; 1401 | 1402 | g_mqttPubSub.publish(g_TopicState_din1.c_str(), strPayload.c_str()); 1403 | } 1404 | } 1405 | 1406 | void MqttPublishStatus_dIn2() 1407 | { 1408 | String strPayload; 1409 | if(g_mqttPubSub.connected()) 1410 | { 1411 | if(g_dIn2 == 0) 1412 | strPayload = "OFF"; 1413 | else 1414 | strPayload = "ON"; 1415 | 1416 | g_mqttPubSub.publish(g_TopicState_din2.c_str(), strPayload.c_str()); 1417 | } 1418 | } 1419 | 1420 | void MqttPublishStatus_dIn3() 1421 | { 1422 | String strPayload; 1423 | if(g_mqttPubSub.connected()) 1424 | { 1425 | if(g_dIn3 == 0) 1426 | strPayload = "OFF"; 1427 | else 1428 | strPayload = "ON"; 1429 | 1430 | g_mqttPubSub.publish(g_TopicState_din3.c_str(), strPayload.c_str()); 1431 | } 1432 | } 1433 | 1434 | void MqttPublishStatus_dIn4() 1435 | { 1436 | String strPayload; 1437 | if(g_mqttPubSub.connected()) 1438 | { 1439 | if(g_dIn4 == 0) 1440 | strPayload = "OFF"; 1441 | else 1442 | strPayload = "ON"; 1443 | 1444 | g_mqttPubSub.publish(g_TopicState_din4.c_str(), strPayload.c_str()); 1445 | } 1446 | } 1447 | 1448 | void MqttPublishStatus_Relay1() 1449 | { 1450 | String strPayload; 1451 | if(g_mqttPubSub.connected()) 1452 | { 1453 | if(g_Relay1 == 0) 1454 | strPayload = "OFF"; 1455 | else 1456 | strPayload = "ON"; 1457 | 1458 | g_mqttPubSub.publish((g_TopicRelay1 + "/state").c_str(), strPayload.c_str()); 1459 | } 1460 | } 1461 | 1462 | void MqttPublishStatus_Relay2() 1463 | { 1464 | String strPayload; 1465 | if(g_mqttPubSub.connected()) 1466 | { 1467 | if(g_Relay2 == 0) 1468 | strPayload = "OFF"; 1469 | else 1470 | strPayload = "ON"; 1471 | 1472 | g_mqttPubSub.publish((g_TopicRelay2 + "/state").c_str(), strPayload.c_str()); 1473 | } 1474 | } 1475 | 1476 | void MqttPublishStatus_Relay3() 1477 | { 1478 | String strPayload; 1479 | if(g_mqttPubSub.connected()) 1480 | { 1481 | if(g_Relay3 == 0) 1482 | strPayload = "OFF"; 1483 | else 1484 | strPayload = "ON"; 1485 | 1486 | g_mqttPubSub.publish((g_TopicRelay3 + "/state").c_str(), strPayload.c_str()); 1487 | } 1488 | } 1489 | 1490 | void MqttPublishStatus_Relay4() 1491 | { 1492 | String strPayload; 1493 | if(g_mqttPubSub.connected()) 1494 | { 1495 | if(g_Relay4 == 0) 1496 | strPayload = "OFF"; 1497 | else 1498 | strPayload = "ON"; 1499 | 1500 | g_mqttPubSub.publish((g_TopicRelay4 + "/state").c_str(), strPayload.c_str()); 1501 | } 1502 | } 1503 | 1504 | void MqttPublishStatus_RelayBell() 1505 | { 1506 | String strPayload; 1507 | if(g_mqttPubSub.connected()) 1508 | { 1509 | if(g_RelayBell == 0) 1510 | strPayload = "OFF"; 1511 | else 1512 | strPayload = "ON"; 1513 | 1514 | g_mqttPubSub.publish((g_TopicRelayBell + "/state").c_str(), strPayload.c_str()); 1515 | } 1516 | } 1517 | 1518 | void MqttPublishStatus_SwitchBus() 1519 | { 1520 | String strPayload; 1521 | if(g_mqttPubSub.connected()) 1522 | { 1523 | if(g_SwitchBus == 0) 1524 | strPayload = "OFF"; 1525 | else 1526 | strPayload = "ON"; 1527 | 1528 | g_mqttPubSub.publish((g_TopicSwitchBus + "/state").c_str(), strPayload.c_str()); 1529 | } 1530 | } 1531 | 1532 | void MqttPublishStatus_SwitchTestBattery() 1533 | { 1534 | String strPayload; 1535 | if(g_mqttPubSub.connected()) 1536 | { 1537 | if(g_SwitchTestBattery == 0) 1538 | strPayload = "OFF"; 1539 | else 1540 | strPayload = "ON"; 1541 | 1542 | g_mqttPubSub.publish((g_TopicSwitchTestBattery + "/state").c_str(), strPayload.c_str()); 1543 | } 1544 | } 1545 | 1546 | void MqttPublishStatus_Power() 1547 | { 1548 | String strPayload; 1549 | if(g_mqttPubSub.connected()) 1550 | { 1551 | g_mqttPubSub.publish(g_TopicPowerState.c_str(), g_PowerStatus.c_str()); 1552 | } 1553 | } 1554 | 1555 | void MqttPublishStatus_Battery() 1556 | { 1557 | String strPayload; 1558 | if(g_mqttPubSub.connected()) 1559 | { 1560 | g_mqttPubSub.publish(g_TopicVoltBattery.c_str(), String(g_VoltageBattery).c_str()); 1561 | } 1562 | } 1563 | 1564 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1565 | // Serial Communication Protocol Functions 1566 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1567 | byte CalculateChecksum(byte numByte) 1568 | { 1569 | byte rv = 0, index; 1570 | for(index = 0; index < numByte; index++) 1571 | { 1572 | rv += g_FrameBuffer[index]; 1573 | } 1574 | return rv; 1575 | } 1576 | 1577 | void GplSendData(byte* pBuff, int length) 1578 | { 1579 | int i; 1580 | byte dataToSend = 0; 1581 | 1582 | g_OutputBuffer[dataToSend++] = GPL_START_FRAME; 1583 | 1584 | for(i = 1; i < length; i++) 1585 | { 1586 | if(pBuff[i] == GPL_START_FRAME || pBuff[i] == GPL_ESCAPE_CHAR) 1587 | { 1588 | g_OutputBuffer[dataToSend++] = GPL_ESCAPE_CHAR; 1589 | g_OutputBuffer[dataToSend++] = pBuff[i] ^ GPL_XOR_CHAR; 1590 | } else 1591 | g_OutputBuffer[dataToSend++] = pBuff[i]; 1592 | } 1593 | 1594 | Serial2.write(g_OutputBuffer, dataToSend); 1595 | } 1596 | 1597 | /*************************************************************************************************************************************************/ 1598 | //NAME: GPL_SendByte 1599 | //DESCRPTION: Send a byte 1600 | // (byte) Command 1601 | // (byte) Data 1602 | //RETURN: void 1603 | //NOTE: 1604 | /*************************************************************************************************************************************************/ 1605 | void GPL_SendByte(byte idSender, byte idReceiver, byte cmd, byte data) 1606 | { 1607 | g_FrameBuffer[0] = GPL_START_FRAME; // Start Frame 1608 | g_FrameBuffer[1] = cmd; // Comando 1609 | g_FrameBuffer[2] = 0x01; // Lunghezza campo dati 1610 | g_FrameBuffer[3] = data; // Data 1611 | g_FrameBuffer[4] = CalculateChecksum(6); // Checksum 1612 | 1613 | GplSendData(g_FrameBuffer, 5); 1614 | } 1615 | 1616 | /*************************************************************************************************************************************************/ 1617 | //NAME: GPL_SendWord 1618 | //DESCRPTION: Send a 2 byte word 1619 | // (byte) Command 1620 | // (word) Data 1621 | //RETURN: void 1622 | //NOTE: 1623 | /*************************************************************************************************************************************************/ 1624 | void GPL_SendWord(byte idSender, byte idReceiver, byte cmd, word data) 1625 | { 1626 | g_FrameBuffer[0] = GPL_START_FRAME; // Start Frame 1627 | g_FrameBuffer[1] = cmd; // Comando 1628 | g_FrameBuffer[2] = 0x02; // Lunghezza campo dati 1629 | g_FrameBuffer[3] = HIBYTE(data); // Data 1630 | g_FrameBuffer[4] = LOBYTE(data); 1631 | g_FrameBuffer[5] = CalculateChecksum(7); // Checksum 1632 | 1633 | GplSendData(g_FrameBuffer, 6); 1634 | } 1635 | 1636 | /*************************************************************************************************************************************************/ 1637 | //NAME: GPL_SendDWord 1638 | //DESCRPTION: Send a 4 byte word 1639 | // (byte) Command 1640 | // (unsigned long) Data 1641 | //RETURN: void 1642 | //NOTE: 1643 | /*************************************************************************************************************************************************/ 1644 | void GPL_SendDWord(byte idSender, byte idReceiver, byte cmd, unsigned long data) 1645 | { 1646 | g_FrameBuffer[0] = GPL_START_FRAME; // Start Frame 1647 | g_FrameBuffer[1] = cmd; // Comando 1648 | g_FrameBuffer[2] = 0x04; // Lunghezza campo dati 1649 | g_FrameBuffer[3] = MSBYTE(data); // Data 1650 | g_FrameBuffer[4] = UPBYTE(data); 1651 | g_FrameBuffer[5] = HIBYTE(data); 1652 | g_FrameBuffer[6] = LOBYTE(data); 1653 | g_FrameBuffer[7] = CalculateChecksum(9); // Checksum 1654 | 1655 | GplSendData(g_FrameBuffer, 8); 1656 | } 1657 | 1658 | /*************************************************************************************************************************************************/ 1659 | //NAME: GPL_SendMessage 1660 | //DESCRPTION: Send a buffer data 1661 | // (byte) Command 1662 | // (byte*) Buffer 1663 | // (int) Buffer length 1664 | //RETURN: void 1665 | //NOTE: 1666 | /*************************************************************************************************************************************************/ 1667 | void GPL_SendMessage(byte idSender, byte idReceiver, byte cmd, byte* pBuff, int length) 1668 | { 1669 | int i = 0; 1670 | g_FrameBuffer[0] = GPL_START_FRAME; // Start Frame 1671 | g_FrameBuffer[1] = cmd; // Comando 1672 | g_FrameBuffer[2] = length; // Lunghezza campo dati 1673 | for(i = 0; i < length; i++) 1674 | g_FrameBuffer[i + 3] = pBuff[i]; 1675 | g_FrameBuffer[length + 3] = CalculateChecksum(length + 3); // Checksum 1676 | 1677 | GplSendData(g_FrameBuffer, length + 4); 1678 | } 1679 | 1680 | /*************************************************************************************************************************************************/ 1681 | //NAME: GPL_SendFrame 1682 | //DESCRPTION: Send a Protocol Frame 1683 | //INPUT: (byte*) Pointer to Frame buffer 1684 | //RETURN: void 1685 | //NOTE: 1686 | /*************************************************************************************************************************************************/ 1687 | void GPL_SendFrame(byte* pBuff) 1688 | { 1689 | int i = 0, length; 1690 | length = pBuff[GPL_BYTE_LENGTH] + GPL_NUM_EXTRA_BYTES; 1691 | for(i = 0; i < length; i++) 1692 | g_FrameBuffer[i] = pBuff[i]; 1693 | 1694 | GplSendData(g_FrameBuffer, length); 1695 | } 1696 | 1697 | /*************************************************************************************************************************************************/ 1698 | //NAME: GPL0_GetByte 1699 | //DESCRPTION: Get Frame Data Byte 1700 | //INPUT: (byte*) Pointer to Frame buffer 1701 | //RETURN: (INT8U) Byte in the frame 1702 | //NOTE: 1703 | /*************************************************************************************************************************************************/ 1704 | byte GPL_GetByte(byte* frame) 1705 | { 1706 | byte rv = 0; 1707 | if(frame) 1708 | { 1709 | rv = frame[GPL_BYTE_FIRST_DATA]; 1710 | } 1711 | return rv; 1712 | } 1713 | 1714 | /*************************************************************************************************************************************************/ 1715 | //NAME: GPL0_GetWord 1716 | //DESCRPTION: Get Frame Data Word (2 Byte) 1717 | //INPUT: (byte*) Pointer to Frame buffer 1718 | //RETURN: (INT16U) Word in the frame 1719 | //NOTE: 1720 | /*************************************************************************************************************************************************/ 1721 | word GPL_GetWord(byte* frame) 1722 | { 1723 | word rv = 0; 1724 | if(frame) 1725 | { 1726 | rv = MAKEWORD(frame[GPL_BYTE_FIRST_DATA], frame[GPL_BYTE_FIRST_DATA + 1]); 1727 | } 1728 | return rv; 1729 | } 1730 | 1731 | /*************************************************************************************************************************************************/ 1732 | //NAME: GPL0_GetDWord 1733 | //DESCRPTION: Get Frame Data Unsigned Long (4 Byte) 1734 | //INPUT: (byte*) Puntatore al buffer del frame 1735 | //RETURN: (unsigned long) Unsigned Long in the frame 1736 | //NOTE: 1737 | /*************************************************************************************************************************************************/ 1738 | unsigned long GPL_GetDWord(byte* frame) 1739 | { 1740 | unsigned long rv = 0; 1741 | word hiWord = 0, loWord = 0; 1742 | if(frame) 1743 | { 1744 | hiWord = MAKEWORD(frame[GPL_BYTE_FIRST_DATA], frame[GPL_BYTE_FIRST_DATA + 1]); 1745 | loWord = MAKEWORD(frame[GPL_BYTE_FIRST_DATA + 2], frame[GPL_BYTE_FIRST_DATA + 3]); 1746 | rv = (unsigned long)(((unsigned long)(loWord)) | (((unsigned long)(hiWord))<<16)); 1747 | } 1748 | return rv; 1749 | } 1750 | --------------------------------------------------------------------------------