Showing 4 changed files with 120 additions and 32 deletions
+17 -8
BLE_iBeacon.ino
... ...
@@ -19,9 +19,13 @@
19 19
 
20 20
 //Uncomment the board you are using
21 21
 // heltec.h : Wemos TTGO LoRa, Heltech LoRa
22
-// M5Stick.h : Orange M5 Stick
23
-//#include <heltec.h>
24
-#include <M5StickC.h>
22
+// M5Stick.h : Orange M5 Stick C
23
+#ifdef ARDUINO_M5Stick_C
24
+  #include <M5StickC.h>
25
+#elif ARDUINO_HELTEC_WIFI_LORA_32
26
+  #include <heltec.h>
27
+#endif //ARDUINO_M5Stick_C & ARDUINO_HELTEC_WIFI_LORA_32
28
+
25 29
 #include "sys/time.h"
26 30
 
27 31
 #include "BLEDevice.h"
... ...
@@ -32,11 +36,13 @@
32 36
 #include "beacon.h"
33 37
 #include "display.h"
34 38
 #include "button.h"
39
+#include "serial.h"
35 40
 
36 41
 #include <EEPROM.h>
37 42
 #define EEPROM_SIZE 1 // To save BeaconType upon restart
38 43
 
39 44
 //#define DISABLE_BT //For Battery charging test
45
+//#define DEBUG_PAYLOAD //TO print the payload to be sent
40 46
 
41 47
 BLEAdvertising *pAdvertising;
42 48
 
... ...
@@ -69,23 +75,24 @@ void setBeacon() {
69 75
     advertising_data[AD_IDX_INSTANCE_ALL+AD_SHIFT+2], advertising_data[AD_IDX_INSTANCE_ALL+AD_SHIFT+3],
70 76
     advertising_data[AD_IDX_INSTANCE_ALL+AD_SHIFT+4], advertising_data[AD_IDX_INSTANCE_ALL+AD_SHIFT+5]);
71 77
   DisplayButtonStatus();
72
-  
78
+
79
+  #ifdef DEBUG_PAYLOAD
73 80
   uint8_t *ptr=(uint8_t*)strServiceData.c_str();
74 81
   for(int i = 0; i < 36; i++ ) {
75 82
     Serial.printf("%02x", ptr[i]);
76 83
   }
77 84
   Serial.println("");
85
+  #endif //DEBUG_PAYLOAD
78 86
   oAdvertisementData.addData(strServiceData);
79 87
   pAdvertising->setAdvertisementData(oAdvertisementData);
80 88
   pAdvertising->setScanResponseData(oScanResponseData);
81 89
 }
82 90
 
83 91
 void setup() {
84
-  #ifdef _HELTEC_H_
92
+  #ifdef ARDUINO_HELTEC_WIFI_LORA_32
85 93
   Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, true /*Serial Enable*/, false /*LoRa use PABOOST*/, 0 /*LoRa RF working band*/);
86 94
   Heltec.display->clear();
87
-  #endif //_HELTEC_H_
88
-  #ifdef _M5STICKC_H_
95
+  #elif ARDUINO_M5Stick_C
89 96
   M5.begin();
90 97
   M5.Lcd.setRotation(3);
91 98
   double BatteryOrUSB = M5.Axp.GetIusbinData();
... ...
@@ -94,7 +101,7 @@ void setup() {
94 101
     backlight_level = MIN_BACKLIGHT;//Minimum backlight
95 102
   }
96 103
   M5.Axp.ScreenBreath(backlight_level);
97
-  #endif //_M5STICKC_H_
104
+  #endif //ARDUINO_M5Stick_C & ARDUINO_HELTEC_WIFI_LORA_32
98 105
   Serial.begin(115200);
99 106
   EEPROM.begin(EEPROM_SIZE);
100 107
 
... ...
@@ -183,6 +190,7 @@ void loop() {
183 190
   ad_running = 1;
184 191
   DisplayButtonStatus();
185 192
   pAdvertising->start();
193
+  Serial.println("Advertizing started instance : "+String(instanceStr));
186 194
   Serial.println("Advertizing started type : "+beaconTypeStr);
187 195
   delay(BEACON_ADV_DURATION);
188 196
   pAdvertising->stop();
... ...
@@ -195,6 +203,7 @@ void loop() {
195 203
   
196 204
   //PRG Button
197 205
   pollPrg();
206
+  pollSerial();
198 207
   
199 208
   #ifndef DISABLE_BT
200 209
   delay(BEACON_SLEEP_ADV);
+2 -2
button.h
... ...
@@ -8,9 +8,9 @@ int buttonState = 0;         // variable for reading the pushbutton status
8 8
 #ifdef _HELTEC_H_
9 9
 const int BUTTON_PRG = 0;     // PRG Button
10 10
 #endif //_HELTEC_H_
11
-#ifdef _M5STICKC_H_
11
+#ifdef ARDUINO_M5Stick_C
12 12
 const int BUTTON_PRG = M5_BUTTON_HOME; // Home Button
13
-#endif //_M5STICKC_H_
13
+#endif //ARDUINO_M5Stick_C
14 14
 const int ledPin =  13;      // the number of the LED pin
15 15
  
16 16
 void pollPrg() 
+20 -22
display.h
... ...
@@ -9,26 +9,25 @@
9 9
 #define LINE3 24
10 10
 #define LINE4 36
11 11
 #define LINE5 48
12
-#ifndef _M5STICKC_H_
13
-  #define LINEBOTTOM 54
14
-#else
12
+#ifdef ARDUINO_M5Stick_C
15 13
   #define LINE6 60 //M5Only
16 14
   #define LINEBOTTOM 72
17 15
   //Battery see : http://community.m5stack.com/topic/1361/ischarging-and-getbatterylevel/5
18 16
   #define VMAX 4000.0f // Observed Maximum: 4161.3 after 7h charge
19 17
   #define VMIN 2973.0f // Observed Minimum: 2973.0
20 18
   #define COLUMN_BAT 80
21
-#endif //_M5STICKC_H_  
19
+#elif ARDUINO_HELTEC_WIFI_LORA_32
20
+  #define LINEBOTTOM 54
21
+#endif //ARDUINO_M5Stick_C & ARDUINO_HELTEC_WIFI_LORA_32
22 22
 
23 23
 //-----------------------------------------------------------------------------------
24
-#ifdef _M5STICKC_H_
25
-//uint8_t backlight_level = 15; //15 High Luminosity, 7 lowest luminosity
26
-#define MIN_BACKLIGHT 7
27
-#define MID_BACKLIGHT 11
28
-#define MAX_BACKLIGHT 15
29
-#define DEFAULT_BACKLIGHT MID_BACKLIGHT
30
-uint8_t backlight_level = DEFAULT_BACKLIGHT; //15 High Luminosity, 7 lowest luminosity
31
-#endif //_M5STICKC_H_
24
+#ifdef ARDUINO_M5Stick_C
25
+  #define MIN_BACKLIGHT 7
26
+  #define MID_BACKLIGHT 11
27
+  #define MAX_BACKLIGHT 15
28
+  #define DEFAULT_BACKLIGHT MID_BACKLIGHT
29
+  uint8_t backlight_level = DEFAULT_BACKLIGHT; //15 High Luminosity, 7 lowest luminosity
30
+#endif //ARDUINO_M5Stick_C
32 31
 //-----------------------------------------------------------------------------------
33 32
 
34 33
 
... ...
@@ -39,7 +38,7 @@ char macStr[18] = "";
39 38
 int ad_running = 0;
40 39
 uint32_t beacon_count = 0;
41 40
 
42
-#ifdef _M5STICKC_H_
41
+#ifdef ARDUINO_M5Stick_C
43 42
 float getBatteryLevel(void)
44 43
 {
45 44
   uint16_t vbatData = M5.Axp.GetVbatData();
... ...
@@ -47,10 +46,10 @@ float getBatteryLevel(void)
47 46
   //return 100.0 * ((vbat - 3000) / (4070 - 3000));
48 47
   return 100.0 * ((vbat - VMIN) / (VMAX - VMIN));
49 48
 }
50
-#endif //_M5STICKC_H_
49
+#endif //ARDUINO_M5Stick_C
51 50
 //---------------------------------------------------------------------
52 51
 void DisplayButtonStatus() {
53
-  #ifdef _HELTEC_H_
52
+  #ifdef ARDUINO_HELTEC_WIFI_LORA_32
54 53
   Heltec.display->clear();
55 54
   Heltec.display->drawString(0, LINE1, "BT: "+String(macStr));
56 55
   Heltec.display->drawString(0, LINE2, "I : "+String(instanceStr));
... ...
@@ -61,13 +60,12 @@ void DisplayButtonStatus() {
61 60
     Heltec.display->drawString(120, LINEBOTTOM, running);
62 61
   }
63 62
   Heltec.display->display();
64
-  #endif //_HELTEC_H_
65
-  #ifdef _M5STICKC_H_
63
+  #elif ARDUINO_M5Stick_C //ARDUINO_TTGO_LoRa32_V1
66 64
   //M5.Lcd.fillScreen(BLACK);
67 65
   M5.Lcd.setTextColor(0x7bef, BLACK);
68 66
   M5.Lcd.setCursor(0, LINE1, 1);M5.Lcd.println("BT: "+String(macStr));
69 67
   M5.Lcd.setCursor(0, LINE2, 1);M5.Lcd.println("I : "+String(instanceStr));
70
-  M5.Lcd.setCursor(0, LINE4, 1);M5.Lcd.setTextColor(GREEN, BLACK);  M5.Lcd.println(beaconTypeStr);
68
+  M5.Lcd.setCursor(0, LINE4, 2);M5.Lcd.setTextColor(GREEN, BLACK);  M5.Lcd.println(beaconTypeStr);
71 69
   M5.Lcd.setCursor(0, LINE6, 1);M5.Lcd.setTextColor(0x7bef, BLACK);  M5.Lcd.println( "Cnt: "+String(beacon_count));
72 70
   if( 1 == ad_running ) {
73 71
     M5.Lcd.setCursor(150, LINEBOTTOM, 1);M5.Lcd.setTextColor(WHITE, GREEN); M5.Lcd.println(String("X"));
... ...
@@ -83,15 +81,15 @@ void DisplayButtonStatus() {
83 81
   M5.Lcd.fillRect(0, LINEBOTTOM, 50, 10, BLACK);
84 82
   M5.Lcd.fillRect(COLUMN_BAT, LINEBOTTOM, 60, 10, BLACK);
85 83
   M5.Lcd.printf("%.1fmV",BatteryValue);
86
-  if( BatteryPercent > 95 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.3f%%",BatteryPercent); }
84
+  if( BatteryPercent == 100 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.0f%%",BatteryPercent); }
85
+  else if( BatteryPercent > 95 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.3f%%",BatteryPercent); }
87 86
   else if( BatteryPercent > 90 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.2f%%",BatteryPercent); }
88 87
   else if( BatteryPercent > 60 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.1f%%",BatteryPercent); }
89 88
   else if( BatteryPercent > 60 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(GREEN, BLACK);M5.Lcd.printf("%.1f%%",BatteryPercent); }
90 89
   else if( BatteryPercent > 40 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(0xff80, BLACK);M5.Lcd.printf("%.0f%%",BatteryPercent); } //Yellow
91 90
   else if( BatteryPercent > 30 ) { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(0xfbe4, BLACK);M5.Lcd.printf("%.0f%%",BatteryPercent); } //Orange
92 91
   else { M5.Lcd.setCursor(COLUMN_BAT, LINEBOTTOM, 1);M5.Lcd.setTextColor(RED, BLACK);M5.Lcd.printf("%.0f%%",BatteryPercent); } //Red
93
-
94
-  Serial.println(BatteryValue);
95
-  #endif //_M5STICKC_H_
92
+  //Serial.println(BatteryValue);
93
+  #endif //ARDUINO_M5Stick_C & ARDUINO_HELTEC_WIFI_LORA_32
96 94
 }
97 95
 #endif //_DISPLAY_H
+81
serial.h
... ...
@@ -0,0 +1,81 @@
1
+#ifndef _SERIAL_H
2
+#define _SERIAL_H
3
+
4
+#include "beacon.h"
5
+
6
+#define SERIAL_MAX_LINES 32
7
+void serialHelp() {
8
+  Serial.println("Available commands: ");
9
+  Serial.println("- help: Display this screen");
10
+  Serial.println("- beacon: Display beacon details");
11
+  Serial.println("- type X: Set type of Beacon");
12
+}
13
+
14
+void serialBeacon() {
15
+  String serialOut = "{\"command\": \"beacon\", \"status\": { ";
16
+  serialOut += "\"mac\": \""+String(macStr)+"\", ";
17
+  serialOut += "\"instance\": \""+String(instanceStr)+"\", ";
18
+  serialOut += "\"count\": \""+String(beacon_count)+"\", ";
19
+  serialOut += "\"type\": \""+beaconTypeStr+"\"";
20
+  serialOut += "}";
21
+  serialOut += "}";
22
+  Serial.println(serialOut);
23
+}
24
+
25
+void serialType(EN_BEACON_TYPE type) {
26
+  beacon_type = type;
27
+  switch( type ) {
28
+  case EN_BEACON_TYPE_entry:
29
+    beaconTypeStr="Entry     ";
30
+    break;
31
+  case EN_BEACON_TYPE_hazard:
32
+    beaconTypeStr="Hazard    ";
33
+    break;
34
+  case EN_BEACON_TYPE_safe:
35
+    beaconTypeStr="Safe       ";
36
+    break;
37
+  case EN_BEACON_TYPE_exit:
38
+    beaconTypeStr="Exit      ";
39
+    break;
40
+  case EN_BEACON_TYPE_other:
41
+    beaconTypeStr="Other     ";
42
+    break;
43
+  default:
44
+    beaconTypeStr="Safe      ";
45
+    break;
46
+  }
47
+  String serialOut = "{\"command\": \"beacon\", \"status\": { ";
48
+  serialOut += "\"type\": \""+String(beaconTypeStr)+"\"";
49
+  serialOut += "}";
50
+  Serial.println(serialOut);
51
+  EEPROM.write(0, beacon_type);
52
+  EEPROM.commit();
53
+}
54
+
55
+void pollSerial() {
56
+  if (Serial.available() > 0) {
57
+    char command[SERIAL_MAX_LINES];
58
+    int sizeCommand = Serial.readBytesUntil('\r', command, sizeof(command) / sizeof(char) );
59
+    command[sizeCommand]='\0';
60
+    if(command[sizeCommand-1]=='\n') command[sizeCommand-1]='\0';
61
+    Serial.println("[CMD] received: "+String(command));
62
+    if( 0 == strcmp(command,"help") ) {
63
+      serialHelp();
64
+    } else if( 0 == strcmp(command,"beacon") ) {
65
+      serialBeacon();
66
+    } else if( 0 == strcmp(command,"type 1") ) {
67
+      serialType(EN_BEACON_TYPE_entry);
68
+    } else if( 0 == strcmp(command,"type 2") ) {
69
+      serialType(EN_BEACON_TYPE_exit);
70
+    } else if( 0 == strcmp(command,"type 3") ) {
71
+      serialType(EN_BEACON_TYPE_hazard);
72
+    } else if( 0 == strcmp(command,"type 4") ) {
73
+      serialType(EN_BEACON_TYPE_safe);
74
+    } else if( 0 == strcmp(command,"type 5") ) {
75
+      serialType(EN_BEACON_TYPE_other);
76
+    } else {
77
+      Serial.println("{\"command\": \""+String(command)+"\", \"status\": \"error\" }");
78
+    }
79
+  }
80
+}
81
+#endif //_SERIAL_H