1
1
// ble_sensor_mqtt_pub.c
2
2
// gcc -o ble_sensor_mqtt_pub ble_sensor_mqtt_pub.c -l bluetooth -l paho-mqtt3c
3
- // 202012131603
3
+ // 202102030607
4
4
//
5
5
// decode BLE temperature sensor temperature and humidity data from BLE advertising packets
6
6
// and publish to MQTT
18
18
//
19
19
20
20
#define VERSION_MAJOR 2
21
- #define VERSION_MINOR 12
21
+ #define VERSION_MINOR 13
22
22
// why is it so hard to get the base name of the program withOUT the .c extension!!!!!!!
23
23
24
24
#define PROGRAM_NAME "ble_sensor_mqtt_pub"
@@ -256,7 +256,7 @@ void connlost(void *context, char *cause)
256
256
257
257
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d MQTT Server Connection lost" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
258
258
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
259
- syslog (LOG_ERR , log_message );
259
+ syslog (LOG_ERR , "%s" , log_message );
260
260
fprintf (stderr , "MQTT Server Connection lost, cause: %s\n" , cause );
261
261
exit (1 );
262
262
}
@@ -344,7 +344,7 @@ int main(int argc, char *argv[])
344
344
345
345
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Couldn't enumerate HCI devices: %s" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , strerror (errno ));
346
346
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
347
- syslog (LOG_ERR , log_message );
347
+ syslog (LOG_ERR , "%s" , log_message );
348
348
fprintf (stderr , "Couldn't enumerate HCI devices: %s" , strerror (errno ));
349
349
exit (1 );
350
350
}
@@ -357,7 +357,7 @@ int main(int argc, char *argv[])
357
357
{
358
358
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Start program with four arguments, the bluetooth adapter number, scan type (0=passive, 1=active), BLE scan Window (0 for default), BLE scan Interval (0 for default)\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
359
359
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
360
- syslog (LOG_ERR , log_message );
360
+ syslog (LOG_ERR , "%s" , log_message );
361
361
fprintf (stderr , "Start program with four arguments, the bluetooth adapter number, scan type (0=passive, 1=active), BLE scan Window (0 for default), BLE scan Interval (0 for default)\n" );
362
362
exit (1 );
363
363
}
@@ -397,7 +397,7 @@ int main(int argc, char *argv[])
397
397
{
398
398
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Enter bluetooth adapter number between 0 and %u !!\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , hci_devs_num - 1 );
399
399
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
400
- syslog (LOG_ERR , log_message );
400
+ syslog (LOG_ERR , "%s" , log_message );
401
401
fprintf (stderr , "Enter bluetooth adapter number between 0 and %u !!\n" , hci_devs_num - 1 );
402
402
exit (1 );
403
403
}
@@ -411,7 +411,7 @@ int main(int argc, char *argv[])
411
411
openlog (PROGRAM_NAME , LOG_CONS | LOG_PID | LOG_NDELAY , LOG_LOCAL1 );
412
412
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Starting." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
413
413
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
414
- syslog (LOG_INFO , log_message );
414
+ syslog (LOG_INFO , "%s" , log_message );
415
415
416
416
// maximum number of sensors
417
417
#define MAXIMUM_UNITS 40
@@ -473,7 +473,7 @@ int main(int argc, char *argv[])
473
473
{
474
474
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Too many devices in configuration file, limit is : %d\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , MAXIMUM_UNITS );
475
475
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
476
- syslog (LOG_ERR , log_message );
476
+ syslog (LOG_ERR , "%s" , log_message );
477
477
fprintf (stderr , "Too many devices in configuration file, limit is : %d\n" , MAXIMUM_UNITS );
478
478
exit (1 );
479
479
@@ -528,7 +528,7 @@ int main(int argc, char *argv[])
528
528
{
529
529
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d No blank rows in configuration file are allowed\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
530
530
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
531
- syslog (LOG_ERR , log_message );
531
+ syslog (LOG_ERR , "%s" , log_message );
532
532
fprintf (stderr , "No blank rows in configuration file are allowed\n" );
533
533
exit (1 );
534
534
}
@@ -563,7 +563,7 @@ int main(int argc, char *argv[])
563
563
{
564
564
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d failed to connect to MQTT server" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
565
565
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
566
- syslog (LOG_ERR , log_message );
566
+ syslog (LOG_ERR , "%s" , log_message );
567
567
fprintf (stderr , "Failed to connect to MQTT server, return code %d\n" , rc );
568
568
exit (1 );
569
569
}
@@ -574,13 +574,13 @@ int main(int argc, char *argv[])
574
574
575
575
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Bluetooth Adapter : %u has MAC address : %s\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , bluetooth_adapter_number , bluetooth_adapter_mac );
576
576
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
577
- syslog (LOG_INFO , log_message );
577
+ syslog (LOG_INFO , "%s" , log_message );
578
578
fprintf (stdout , "Bluetooth Adapter : %u has MAC address : %s\n" , bluetooth_adapter_number , bluetooth_adapter_mac );
579
579
580
580
if ( bluetooth_device < 0 ) {
581
581
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d failed to open HCI device" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
582
582
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
583
- syslog (LOG_ERR , log_message );
583
+ syslog (LOG_ERR , "%s" , log_message );
584
584
fprintf (stderr , "Failed to open HCI device, return code %d\n" , bluetooth_device );
585
585
exit (1 );
586
586
}
@@ -589,17 +589,17 @@ int main(int argc, char *argv[])
589
589
590
590
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan type (0=passive, 1=active): %u\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_type );
591
591
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
592
- syslog (LOG_INFO , log_message );
592
+ syslog (LOG_INFO , "%s" , log_message );
593
593
fprintf (stdout , "Advertising scan type (0=passive, 1=active): %u\n" , ble_scan_type );
594
594
595
595
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan window : %u %.1f ms\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_window , ble_scan_window * 0.625 );
596
596
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
597
- syslog (LOG_INFO , log_message );
597
+ syslog (LOG_INFO , "%s" , log_message );
598
598
fprintf (stdout , "Advertising scan window : %4u, %4.1f ms\n" , ble_scan_window , ble_scan_window * 0.625 );
599
599
600
600
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Advertising scan interval : %u %.1f ms\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , ble_scan_interval , ble_scan_interval * 0.625 );
601
601
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
602
- syslog (LOG_INFO , log_message );
602
+ syslog (LOG_INFO , "%s" , log_message );
603
603
fprintf (stdout , "Advertising scan interval : %4u, %4.1f ms\n" , ble_scan_interval , ble_scan_interval * 0.625 );
604
604
605
605
@@ -621,7 +621,7 @@ int main(int argc, char *argv[])
621
621
hci_close_dev (bluetooth_device );
622
622
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to set scan parameters data" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
623
623
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
624
- syslog (LOG_ERR , log_message );
624
+ syslog (LOG_ERR , "%s" , log_message );
625
625
fprintf (stderr , "Failed to set scan parameters data, you must run this program as ROOT, return code %d\n" , ret );
626
626
exit (1 );
627
627
}
@@ -639,7 +639,7 @@ int main(int argc, char *argv[])
639
639
hci_close_dev (bluetooth_device );
640
640
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to set event mask" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
641
641
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
642
- syslog (LOG_ERR , log_message );
642
+ syslog (LOG_ERR , "%s" , log_message );
643
643
fprintf (stderr , "Failed to set event mask, return code %d\n" , ret );
644
644
exit (1 );
645
645
}
@@ -658,7 +658,7 @@ int main(int argc, char *argv[])
658
658
hci_close_dev (bluetooth_device );
659
659
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to enable scan" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
660
660
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
661
- syslog (LOG_ERR , log_message );
661
+ syslog (LOG_ERR , "%s" , log_message );
662
662
fprintf (stderr , "Failed to enable scan, return code %d\n" , ret );
663
663
exit (1 );
664
664
}
@@ -674,14 +674,14 @@ int main(int argc, char *argv[])
674
674
hci_close_dev (bluetooth_device );
675
675
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Could not set socket options" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
676
676
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
677
- syslog (LOG_ERR , log_message );
677
+ syslog (LOG_ERR , "%s" , log_message );
678
678
fprintf (stderr , "Could not set socket options, return code %d\n" , ret );
679
679
exit (1 );
680
680
}
681
681
682
682
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Scanning...." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
683
683
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
684
- syslog (LOG_INFO , log_message );
684
+ syslog (LOG_INFO , "%s" , log_message );
685
685
// fprintf(stdout, "%s v%2d.%02d\n", PROGRAM_NAME, VERSION_MAJOR, VERSION_MINOR);
686
686
fprintf (stdout , "Scanning....\n" );
687
687
fflush (stdout );
@@ -801,7 +801,7 @@ int main(int argc, char *argv[])
801
801
fprintf (stderr , "MQTT payload too long: %d\n" , payload_length );
802
802
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d MQTT payload too long: %d\n" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR , payload_length );
803
803
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
804
- syslog (LOG_ERR , log_message );
804
+ syslog (LOG_ERR , "%s" , log_message );
805
805
exit (1 );
806
806
}
807
807
@@ -1763,24 +1763,22 @@ int main(int argc, char *argv[])
1763
1763
// get rssi
1764
1764
int rssi_int = (signed char ) (int8_t )adv_info -> data [adv_info -> length ];
1765
1765
1766
- if (logging_level == LOG_DEBUG )
1767
- {
1768
- fprintf (stdout , "=========\n" );
1769
- fprintf (stdout , "Current local time and date: %s" , asctime (time_packet_received ) );
1770
- fprintf (stdout , "mac address = %s location = %s device type = %d " , addr , device_units_location [mac_index ], device_units_type [mac_index ]);
1771
- fprintf (stdout , "advertising_packet_type = %03d\n" , advertising_packet_type );
1772
- // print whole packet
1773
- printf ("==>0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 \n" );
1774
- printf ("==>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1775
- printf ("==> 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2\n" );
1776
- printf ("==> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1777
- printf ("==>" );
1778
- for (n = 0 ; n < bluetooth_adv_packet_length ; n ++ )
1779
- printf ("%02X" ,(unsigned char )ble_adv_buf [n ]);
1780
- printf ("\n" );
1781
- printf ("==>__________ad________________________mmmmmmmmmmmmtttthhbbzbzbccrr\n" );
1782
- fprintf (stdout , "rssi = %03d\n" , rssi_int );
1783
- }
1766
+
1767
+ fprintf (stdout , "=========\n" );
1768
+ fprintf (stdout , "Current local time and date: %s" , asctime (time_packet_received ) );
1769
+ fprintf (stdout , "mac address = %s location = %s device type = %d " , addr , device_units_location [mac_index ], device_units_type [mac_index ]);
1770
+ fprintf (stdout , "advertising_packet_type = %03d\n" , advertising_packet_type );
1771
+ // print whole packet
1772
+ printf ("==>0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 \n" );
1773
+ printf ("==>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1774
+ printf ("==> 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2\n" );
1775
+ printf ("==> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n" );
1776
+ printf ("==>" );
1777
+ for (n = 0 ; n < bluetooth_adv_packet_length ; n ++ )
1778
+ printf ("%02X" ,(unsigned char )ble_adv_buf [n ]);
1779
+ printf ("\n" );
1780
+ printf ("==>__________ad________________________mmmmmmmmmmmmtttthhbbzbzbccrr\n" );
1781
+ fprintf (stdout , "rssi = %03d\n" , rssi_int );
1784
1782
}
1785
1783
fflush (stdout );
1786
1784
}
@@ -1799,7 +1797,7 @@ int main(int argc, char *argv[])
1799
1797
fprintf (stdout , "\n<ctrl>-c signal received, exiting.\n" );
1800
1798
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d <ctrl>-c signal received, exiting." , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
1801
1799
send_remote_syslog_message (LOG_INFO , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
1802
- syslog (LOG_INFO , log_message );
1800
+ syslog (LOG_INFO , "%s" , log_message );
1803
1801
1804
1802
1805
1803
// Disable scanning.
@@ -1812,7 +1810,7 @@ int main(int argc, char *argv[])
1812
1810
hci_close_dev (bluetooth_device );
1813
1811
snprintf (log_message , LOGMESSAGESIZE , "%s v: %d.%d Failed to disable scan" , PROGRAM_NAME , VERSION_MAJOR , VERSION_MINOR );
1814
1812
send_remote_syslog_message (LOG_ERR , RSYSLOG_ADDRESS , PROGRAM_NAME , log_message );
1815
- syslog (LOG_ERR , log_message );
1813
+ syslog (LOG_ERR , "%s" , log_message );
1816
1814
fprintf (stdout , "Failed to disable scan, return code %d\n" , ret );
1817
1815
exit (1 );
1818
1816
}
0 commit comments