├── README.md └── paradox_mg5050.ino /README.md: -------------------------------------------------------------------------------- 1 | # paradox_esp8266 2 | Interface to the MG5050 Paradox alarm system with a ESP8266 using default paradox bus protocol 3 | 4 | Need to start working on writing to the bus using the ESP , its semi working on the arduino already. See the other repo (https://github.com/liaan/paradox_rw_arduino) 5 | 6 | See https://github.com/Margriko/Paradox-ESPHome for HomeAssist implentation 7 | 8 | -------------------------------------------------------------------------------- /paradox_mg5050.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define CLK 0 // Keybus Yellow 6 | #define DTA 2 // Keybus Green 7 | 8 | //AP definitions 9 | #define AP_SSID ""***"" 10 | #define AP_PASSWORD "***" 11 | #define DEVICE_NAME "ParadoxEspInterface-01" 12 | #define MQ_SERVER "mqtt.local" 13 | #define MQ_SERVERPORT 1883 14 | #define MQTT_SUB_TOPIC "/Paradox/alarm/set" 15 | #define MQTT_PUB_TOPIC "/Paradox/alarm/EspInterface/" 16 | 17 | /*******************************/ 18 | WiFiClient wifiClient; 19 | PubSubClient mqtt(wifiClient); 20 | ADC_MODE(ADC_VCC); 21 | 22 | /*********** VARS ********************/ 23 | const int MqttZoneUpdateInterval = 1000; 24 | const int MqttAlarmUpdateInterval = 1000; 25 | const int MqttEspStatusUpdateInterval = 5000; 26 | 27 | String BusMessage = ""; 28 | byte ZoneStatus[33]; 29 | byte OldZoneStatus[33]; 30 | 31 | unsigned long LastClkSignal = 0; 32 | bool ClkPinTriggered = 0; 33 | 34 | unsigned long lastZoneUpdate = 0; 35 | unsigned long lastAlarmUpdate = 0; 36 | unsigned long lastEspStatus = 0; 37 | 38 | //unsigned long lastZoneUpdate = 0; 39 | 40 | typedef struct 41 | { 42 | int status; 43 | 44 | } struct_alarm_status; 45 | 46 | struct_alarm_status AlarmStatus; 47 | struct_alarm_status OldAlarmStatus; 48 | /*******************************/ 49 | 50 | void ICACHE_RAM_ATTR interuptClockFalling(); 51 | void loop(); 52 | void wifiConnect(); 53 | void MQTT_connect(); 54 | bool checkClockIdle(); 55 | void decodeMessage(String &msg); 56 | void sendZonesStatus(); 57 | void sendZonesStatus(bool ALL); 58 | void sendZoneStatus(String ZONE, int status); 59 | void sendAlarmStatus(); 60 | void sendZonesStatus(String ZONE, int status); 61 | void sendEspStatus(); 62 | bool send_mqtt(char *topic, char *value); 63 | bool send_mqtt(char *topic, String value); 64 | void printSerial(String &st); 65 | void printSerial(String &st, int Format); 66 | void ICACHE_RAM_ATTR readDataPin(); 67 | 68 | uint8_t *strToBinArray(String &st); 69 | uint8_t check_crc(String &st); 70 | unsigned int GetIntFromString(String str); 71 | 72 | /************* Setup ******************/ 73 | void setup() 74 | { 75 | WiFi.mode(WIFI_STA); 76 | 77 | pinMode(CLK, INPUT); 78 | pinMode(DTA, INPUT); 79 | Serial.begin(115200); 80 | 81 | Serial.println("Booting"); 82 | //## Interupt pin 83 | attachInterrupt(CLK, interuptClockFalling, FALLING); 84 | //##Interupt timer 85 | timer1_attachInterrupt(readDataPin); // Add ISR Function 86 | timer1_enable(TIM_DIV16,TIM_EDGE,TIM_SINGLE); 87 | 88 | 89 | 90 | Serial.println("Setting up MQTT!"); 91 | mqtt.setServer(MQ_SERVER, MQ_SERVERPORT); 92 | //mqtt.setCallback(do_mqtt_topic_receive); 93 | 94 | Serial.println("Ready!"); 95 | Serial.print("Current Voltage:"); 96 | Serial.println(ESP.getVcc()); 97 | } 98 | 99 | /* 100 | * Main Loop 101 | * 102 | * Will only do MQTT if there was a new message, no need to update anything if nothing changed. 103 | * 104 | * Full zone status happens every MqttZoneUpdateInterval 105 | * 106 | * Any Changes gets send immedetialy 107 | */ 108 | void loop() 109 | { 110 | unsigned long currentMillis = millis(); 111 | 112 | // ##Check if wifi still on 113 | if (WiFi.status() != WL_CONNECTED) 114 | { 115 | wifiConnect(); 116 | return; 117 | } 118 | // ##Check if Mqtt still connected 119 | if (mqtt.state() != MQTT_CONNECTED) 120 | { 121 | MQTT_connect(); 122 | return; 123 | } 124 | 125 | // ## Send all Zone statuss every interval 126 | if (currentMillis - lastEspStatus >= MqttEspStatusUpdateInterval) 127 | { 128 | Serial.println("Sending EspStatus Updates"); 129 | // ## Send Esp Status 130 | sendEspStatus(); 131 | } 132 | 133 | 134 | 135 | // ## Check if there anything new on the Bus 136 | if (!checkClockIdle() or BusMessage.length() < 2) 137 | { 138 | return; 139 | } 140 | 141 | // ## Save message and clear buffer 142 | String Message = BusMessage; 143 | // ## Clear old message 144 | BusMessage = ""; 145 | 146 | //Message = "110100010000000000000000000100010000000000000000000000000000010000000000000000000000000101110101"; 147 | 148 | // ##Copy New to Old 149 | memcpy(OldZoneStatus, ZoneStatus, sizeof(ZoneStatus)); 150 | memcpy(&OldAlarmStatus, &AlarmStatus, sizeof(struct_alarm_status)); 151 | 152 | // ## Decode the message 153 | decodeMessage(Message); 154 | 155 | // ## Send all Zone statuss every interval 156 | if (currentMillis - lastZoneUpdate >= MqttZoneUpdateInterval) 157 | { 158 | Serial.println("Sending Zone Updates"); 159 | // Send All zones status 160 | sendZonesStatus(); 161 | } 162 | 163 | // ## Send all Zone statuss every interval 164 | if (currentMillis - lastAlarmUpdate >= MqttAlarmUpdateInterval) 165 | { 166 | Serial.println("Sending Alarm Updates"); 167 | // ## Send Alarm Status 168 | sendAlarmStatus(); 169 | } 170 | 171 | // Do immedate Update if zone changed 172 | if (memcmp(OldZoneStatus, ZoneStatus, sizeof(ZoneStatus)) != 0) 173 | { 174 | 175 | for (int i = 0; i < 33; i++) 176 | { 177 | Serial.print(ZoneStatus[i], DEC); 178 | } 179 | 180 | Serial.println(""); 181 | for (int i = 0; i < 33; i++) 182 | { 183 | Serial.print(OldZoneStatus[i], DEC); 184 | } 185 | Serial.println(""); 186 | Serial.println("Not same"); 187 | 188 | sendZonesStatus(false); 189 | } 190 | 191 | // Do immedate Update if Alarm Status changed 192 | if (memcmp(&OldAlarmStatus, &AlarmStatus, sizeof(struct_alarm_status)) != 0) 193 | { 194 | 195 | sendAlarmStatus(); 196 | } 197 | 198 | // ## Do MQTT 199 | mqtt.loop(); 200 | } 201 | 202 | /***************** Send ESP Status *******************/ 203 | 204 | void sendEspStatus() 205 | { 206 | lastEspStatus = millis(); 207 | 208 | String topic; 209 | // ## Make topic 210 | topic = MQTT_PUB_TOPIC; 211 | topic += "esp-status/"; 212 | 213 | String voltage = (String)ESP.getVcc(); 214 | 215 | if (send_mqtt((char *)(topic + "voltage/").c_str(), voltage) == false) 216 | { 217 | Serial.println(F("Failed to send Esp voltage update")); 218 | } 219 | } 220 | 221 | /****************************** Send Zones Status *******************************/ 222 | void sendZonesStatus() 223 | { 224 | sendZonesStatus(true); 225 | } 226 | void sendZonesStatus(bool All) 227 | { 228 | 229 | for (int i = 0; i <= 31; i++) 230 | { 231 | if (All == true) 232 | { 233 | sendZoneStatus((String)(i + 1), ZoneStatus[i]); 234 | } 235 | else 236 | { 237 | if (ZoneStatus[i] != OldZoneStatus[i]) 238 | { 239 | sendZoneStatus((String)(i + 1), ZoneStatus[i]); 240 | } 241 | } 242 | } 243 | // Set last update 244 | lastZoneUpdate = millis(); 245 | } 246 | 247 | void sendZoneStatus(String ZONE, int status) 248 | { 249 | String topic; 250 | // ## Make topic 251 | topic = MQTT_PUB_TOPIC; 252 | topic += "zone/"; 253 | topic += ZONE; 254 | topic += "/value"; 255 | 256 | // Serial.print("Publishing Zone to Topic: "); Serial.print(topic);Serial.print(" Value off:");Serial.println(status,DEC); 257 | // ##Convert Int to Ascii 258 | status = status + '0'; 259 | if (send_mqtt((char *)topic.c_str(), (char *)&status) == false) 260 | { 261 | Serial.println(F("Failed to send Zone update")); 262 | } 263 | else 264 | { 265 | // Serial.println(F("OK!")); 266 | } 267 | } 268 | 269 | /****************************** Send Alarm Status *******************************/ 270 | void sendAlarmStatus() 271 | { 272 | lastAlarmUpdate = millis(); 273 | 274 | if (AlarmStatus.status == 0) 275 | { 276 | return; 277 | } 278 | 279 | String topic; 280 | // ## Make topic 281 | topic = MQTT_PUB_TOPIC; 282 | topic += "alarm/value"; 283 | 284 | // ##Convert Int to Ascii 285 | //int value = AlarmStatus.status +'0'; 286 | //AlarmStatus.status = 10; 287 | String value = (String)AlarmStatus.status; 288 | 289 | //Serial.print(AlarmStatus.status,DEC); 290 | if (send_mqtt((char *)topic.c_str(), value) == false) 291 | { 292 | Serial.println(F("Failed to send Alarm update")); 293 | } 294 | } 295 | /********************************************** WIFI connect *******************************************************/ 296 | void wifiConnect() 297 | { 298 | Serial.print("Connecting to AP:"); 299 | Serial.print(AP_SSID); 300 | 301 | WiFi.hostname(DEVICE_NAME); 302 | WiFi.begin(AP_SSID, AP_PASSWORD); 303 | 304 | int repeat = 0; 305 | 306 | while (WiFi.status() != WL_CONNECTED) 307 | { 308 | 309 | delay(1000); 310 | Serial.print("."); 311 | repeat++; 312 | 313 | if (repeat > 10) 314 | { 315 | Serial.print("Failed to connect.. "); 316 | //ESP.restart(); 317 | 318 | // wifi_no_connect(); 319 | break; 320 | return; 321 | ; 322 | } 323 | } 324 | 325 | Serial.println(""); 326 | Serial.println("WiFi connected"); 327 | Serial.println("IP address: "); 328 | Serial.println(WiFi.localIP()); 329 | Serial.println("Hostname: "); 330 | Serial.println(WiFi.hostname()); 331 | } 332 | /***********************************************************************************************************************/ 333 | 334 | /****************************************** Send MQTT *******************************************************************/ 335 | bool send_mqtt(char *topic, char *value) 336 | { 337 | //Serial.print("Sending mqtt" );Serial.print(topic);Serial.print(":");Serial.println(value); 338 | return mqtt.publish(topic, value); 339 | } 340 | bool send_mqtt(char *topic, String value) 341 | { 342 | //Serial.print("Sending mqtt" );Serial.print(topic);Serial.print(":");Serial.println(value); 343 | return mqtt.publish(topic, String(value).c_str()); 344 | } 345 | /****************************************** MQTT *******************************************************************/ 346 | 347 | void MQTT_connect() 348 | { 349 | 350 | String clientName; 351 | 352 | clientName = DEVICE_NAME; 353 | clientName += String(micros() & 0xffff, 16); 354 | 355 | Serial.print("Connecting to "); 356 | Serial.print(MQ_SERVER); 357 | Serial.print(" as "); 358 | Serial.println(clientName); 359 | 360 | int8_t retry_count = 0; 361 | 362 | // Stop if already connected. 363 | if (mqtt.connected()) 364 | { 365 | return; 366 | } 367 | 368 | Serial.print("Connecting to MQTT... "); 369 | 370 | while (mqtt.connected() != true) 371 | { // connect will return 0 for connected 372 | 373 | if (mqtt.connect((char *)clientName.c_str())) 374 | { 375 | 376 | Serial.println("Connected to MQTT broker"); 377 | Serial.print("Pub Topic is: "); 378 | Serial.println(MQTT_PUB_TOPIC); 379 | 380 | if (mqtt.publish(MQTT_PUB_TOPIC, "Hello, Johnny 5 is alive!")) 381 | { 382 | Serial.println("Publish ok"); 383 | // Serial.print("Subscribing to: "); 384 | //Serial.println(MQTT_SUB_TOPIC); 385 | 386 | //Subscribe to topic 387 | // mqtt.subscribe(MQTT_SUB_TOPIC); 388 | } 389 | else 390 | { 391 | Serial.println("Publish failed"); 392 | } 393 | } 394 | else 395 | { 396 | Serial.println("MQTT connect failed"); 397 | Serial.print("State:"); 398 | Serial.println(mqtt.state()); 399 | Serial.println("Waiting a bit to try again."); 400 | //abort(); 401 | } 402 | retry_count++; 403 | if (retry_count > 5) 404 | { 405 | //mqtt_no_connect(); 406 | break; 407 | return; 408 | } 409 | delay(2000); // wait 2 seconds 410 | } 411 | Serial.println("MQTT Connected!"); 412 | } 413 | 414 | /*******************************************************************************************************/ 415 | /****************************** Process Zone Status connect *****************************************/ 416 | /** 417 | * All ok 418 | * 11010000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001 01100011 419 | * Zone 1 420 | * 11010000 00000000 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001 10111001 421 | * Zone 1 and 2 and 3 422 | * 11010000 00000000 01010100 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001 1010 0001 423 | */ 424 | void processZoneStatus(String &msg) 425 | { 426 | //Zone 1 = bit 17 427 | //Zone 2 = bit 19 428 | //Zone 3 = bit 21 429 | 430 | // Serial.println("ProcessingZones"); 431 | 432 | for (int i = 0; i < 32; i++) 433 | { 434 | //Serial.println(17+(i*2)); 435 | 436 | if (msg[17 + (i * 2)] == '1') 437 | { 438 | ZoneStatus[i] = 1; 439 | //Serial.println("Zone:"+(String)(i+1)+"Active"); 440 | } 441 | else 442 | { 443 | 444 | ZoneStatus[i] = 0; 445 | } 446 | // Serial.print("ZoneStatus:"+(String)(i+1)+":");Serial.println(ZoneStatus[i+1],BIN); 447 | } 448 | } 449 | 450 | /****************************** Process Alarm (D1) Status connect *****************************************/ 451 | /* 452 | * Might be first 5 bytes are Partition1 and 2nd 5 bytes is partion 2?? 453 | * Alarm Not set: 11010001 00000000 00000000 00010001 00000000 00000000 00000000 01000100 00000000 00000000 00000001 01001111 454 | * Alarm Set: 11010001 00000000 01000000 00010001 00000000 00000000 00000000 00000100 00000000 00000000 00000001 01110101 455 | * Alarm STay 11010001 00000000 00000000 00010001 00000000 00000000 00000100 00000100 00000000 00000000 00000001 10110000 456 | * Alarm Sleep 11010001 00000000 00000100 00010001 00000000 00000000 00000000 00000100 00000000 00000000 00000001 00001101 457 | * 458 | * 459 | */ 460 | 461 | void processAlarmStatus(String &msg) 462 | { 463 | 464 | // AlarmStatus.status = (msg[(8 + 8 +8 +8 + 8 8 + 8 + 1)] == '1') ? 1 : 0; 465 | 466 | //If set , then get status 467 | if (msg[((8 * 7) + 1)] == '0') 468 | { 469 | 470 | //Sleep 471 | if (msg[((8 * 2) + 5)] == '1') 472 | { 473 | 474 | AlarmStatus.status = 20; 475 | } 476 | //Stay 477 | if (msg[((8 * 6) + 5)] == '1') 478 | { 479 | AlarmStatus.status = 30; 480 | } 481 | 482 | //Full Arm 483 | if (msg[((8 * 2) + 1)] == '1') 484 | { 485 | //Exit Delay 486 | if (msg[((8 * 2) + 0)] == '1') 487 | { 488 | AlarmStatus.status = 40; 489 | } 490 | else 491 | //Full Alarm 492 | if (msg[((8 * 2) + 0)] == '0') 493 | { 494 | AlarmStatus.status = 49; 495 | } 496 | else 497 | { 498 | 499 | AlarmStatus.status = 45; 500 | } 501 | } 502 | } 503 | else 504 | { 505 | //Not Set 506 | AlarmStatus.status = 10; 507 | } 508 | 509 | //Only print if not set 510 | if (AlarmStatus.status != 10) 511 | { 512 | Serial.print("Alarm Status: "); 513 | Serial.println(AlarmStatus.status); 514 | printSerial(msg, BIN); 515 | } 516 | } 517 | /*********************************************************************************************************/ 518 | 519 | /****************************** decodeMessage **************************************** 520 | * 521 | * Check Command type and call approriate function 522 | * 523 | */ 524 | void decodeMessage(String &msg) 525 | { 526 | 527 | int cmd = GetIntFromString(msg.substring(0, 8)); 528 | 529 | //Some commands have trailing "00 00 00 00" so remove 530 | if (cmd == 0xD0 || cmd == 0xD1) 531 | { 532 | //Strip last 00's 533 | msg = msg.substring(0, msg.length() - (4 * 8) - 1); 534 | 535 | if (!check_crc(msg)) 536 | { 537 | Serial.println("CRC Faied:"); 538 | printSerial(msg, HEX); 539 | return; 540 | } 541 | } 542 | else 543 | { 544 | 545 | if (!check_crc(msg)) 546 | { 547 | Serial.println("CRC Faied:"); 548 | printSerial(msg, HEX); 549 | return; 550 | } 551 | } 552 | 553 | switch (cmd) 554 | { 555 | case 0xd0: //Zone Status Message 556 | 557 | processZoneStatus(msg); 558 | 559 | break; 560 | case 0xD1: //Seems like Alarm status 561 | processAlarmStatus(msg); 562 | 563 | break; 564 | case 0xD2: //Action Message; 565 | 566 | break; 567 | 568 | case 0x20: // 569 | 570 | break; 571 | case 0xE0: // Status 572 | 573 | break; 574 | default: 575 | //Do Nothing 576 | break; 577 | ; 578 | } 579 | //Serial.print("Cmd="); 580 | //Serial.println(cmd,HEX); 581 | } 582 | 583 | /** 584 | * CRC8 585 | * 586 | * Do Maxim crc8 calc 587 | */ 588 | uint8_t crc8(uint8_t *addr, uint8_t len) 589 | { 590 | uint8_t crc = 0; 591 | 592 | for (uint8_t i = 0; i < len; i++) 593 | { 594 | uint8_t inbyte = addr[i]; 595 | for (uint8_t j = 0; j < 8; j++) 596 | { 597 | uint8_t mix = (crc ^ inbyte) & 0x01; 598 | crc >>= 1; 599 | if (mix) 600 | crc ^= 0x8C; 601 | 602 | inbyte >>= 1; 603 | } 604 | } 605 | return crc; 606 | } 607 | 608 | /** 609 | * Check CRC 610 | * Check if CRC is valid 611 | * 612 | */ 613 | uint8_t check_crc(String &st) 614 | { 615 | 616 | // printSerial(st); 617 | // 618 | 619 | String val = ""; 620 | int Bytes = (st.length()) / 8; 621 | uint8_t calcCRCByte; 622 | 623 | //Serial.print("Bytes :");Serial.println(Bytes); 624 | //printSerialHex(st); 625 | 626 | //Make byte array 627 | uint8_t *BinnaryStr = strToBinArray(st); 628 | 629 | uint8_t CRC = BinnaryStr[Bytes - 1]; 630 | 631 | calcCRCByte = crc8(BinnaryStr, (int)Bytes - 1); 632 | 633 | //Serial.print("Crc :");Serial.print((int)CRC,HEX); 634 | 635 | //Serial.print("CrcCalc :");Serial.print((int)calcCRCByte,HEX); 636 | //Serial.println(""); 637 | 638 | return calcCRCByte == CRC; 639 | } 640 | 641 | /* 642 | * Check if clock idle, means end of message 643 | * Each messasge is split by 10 Millisecond (10 000 microsecond) delay, 644 | * So assume message been send if 4 clock signals (500us x 4 x 2 = 4000) is low 645 | * 646 | */ 647 | bool checkClockIdle() 648 | { 649 | 650 | unsigned long currentMicros = micros(); 651 | //time diff in 652 | long idletime = (currentMicros - LastClkSignal); 653 | 654 | if (idletime > 8000) 655 | { 656 | 657 | //Serial.println("Idle:"+(String)idletime +":"+currentMicros+":"+ LastClkSignal); 658 | return true; 659 | } 660 | else 661 | { 662 | 663 | return false; 664 | } 665 | } 666 | 667 | /** 668 | * Get int from String 669 | * 670 | * Convert the Binary 10001000 to a int 671 | * 672 | */ 673 | unsigned int GetIntFromString(String str) 674 | { 675 | int r = 0; 676 | // Serial.print("Received:"); Serial.println(str); 677 | int length = str.length(); 678 | 679 | for (int j = 0; j < length; j++) 680 | { 681 | if (str[length - j - 1] == '1') 682 | { 683 | r |= 1 << j; 684 | } 685 | } 686 | 687 | return r; 688 | } 689 | void ICACHE_RAM_ATTR interuptClockFalling() 690 | { 691 | 692 | //## Set last Clock time 693 | LastClkSignal = micros(); 694 | //Set pin triggered 695 | ClkPinTriggered = true; 696 | //Trigger data read timer 697 | timer1_write(750); // 750 / 5 ticks per us from TIM_DIV16 == 150microseconds interval (ithink) 698 | } 699 | 700 | void ICACHE_RAM_ATTR readDataPin() 701 | { 702 | 703 | //If not triggered, just return .. this should never happen, but who knows 704 | if (ClkPinTriggered == false) 705 | { 706 | return; 707 | }else{ 708 | 709 | } 710 | 711 | //Serial.println("Reading Data Pin"); 712 | 713 | /* 714 | * Code need to be updated to ignore the response from the keypad (Rising Edge Comms). 715 | * 716 | * Panel to other is on Clock Falling edge, Reply is after keeping DATA low (it seems) and then reply on Rising edge 717 | */ 718 | //delayMicroseconds(150); 719 | //Just add small delay to make sure DATA is already set, each clock is 500 ~microseconds, Seem to have about 50ms delay before Data goes high when keypad responce and creating garbage . 720 | 721 | //Just wait 150ms since last clk signal to stabilise .. should not happen as we triggered via timer 722 | while (micros() - LastClkSignal < 150) 723 | { 724 | } 725 | 726 | //Append pin state to bus message .. probably should make it binary, but we have enough memory to make debugging simpler 727 | if (!digitalRead(DTA)) 728 | BusMessage += "1"; 729 | else 730 | BusMessage += "0"; 731 | 732 | //Set pin to not triggered 733 | ClkPinTriggered = false; 734 | 735 | if (BusMessage.length() > 200) 736 | { 737 | //Serial.println("String to long"); 738 | //Serial.println((String) BusMessage); 739 | BusMessage = ""; 740 | // printSerialHex(BusMessage); 741 | return; // Do not overflow the arduino's little ram 742 | } 743 | } 744 | 745 | /** 746 | * waitCLKChange 747 | * 748 | * Wait for 5000ms 749 | * 750 | */ 751 | unsigned long waitCLKchange(int currentState) 752 | { 753 | unsigned long c = 0; 754 | while (digitalRead(CLK) == currentState) 755 | { 756 | delayMicroseconds(10); 757 | c += 10; 758 | if (c > 10000) 759 | break; 760 | } 761 | return c; 762 | } 763 | 764 | void printSerial(String &st) 765 | { 766 | 767 | printSerial(st, 1000); 768 | } 769 | 770 | void printSerial(String &st, int Format) 771 | { 772 | 773 | //Get number of Bytes 774 | int Bytes = (st.length()) / 8; 775 | 776 | String val = ""; 777 | 778 | for (int i = 0; i < Bytes; i++) 779 | { 780 | val = st.substring((i * 8), ((i * 8)) + 8); 781 | if (Format == 1000) 782 | { 783 | Serial.print(GetIntFromString(val)); 784 | } 785 | else if (Format == BIN) 786 | { 787 | Serial.print(val); 788 | } 789 | else 790 | { 791 | Serial.print(GetIntFromString(val), Format); 792 | } 793 | Serial.print(" "); 794 | } 795 | Serial.println(""); 796 | } 797 | 798 | /** 799 | * Convert str to binary array 800 | * 801 | */ 802 | uint8_t *strToBinArray(String &st) 803 | { 804 | int Bytes = (st.length()) / 8; 805 | uint8_t Data[Bytes]; 806 | 807 | String val = ""; 808 | 809 | for (int i = 0; i < Bytes; i++) 810 | { 811 | //String kk = "012345670123456701234567"; 812 | val = st.substring((i * 8), ((i * 8)) + 8); 813 | 814 | Data[i] = GetIntFromString(val); 815 | } 816 | 817 | return Data; 818 | } 819 | --------------------------------------------------------------------------------