-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smart-BMS-Bluetooth-ESP32.ino
72 lines (60 loc) · 1.71 KB
/
Smart-BMS-Bluetooth-ESP32.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
Program to read out and display
data from xiaoxiang Smart BMS
over Bluetooth Low Energy
https://www.lithiumbatterypcb.com/
Tested with original BLE module provided.
*/
#define TRACE
#include <Arduino.h>
#include <ArduinoJson.h>
#include "config.h"
#include "BLEDevice.h"
#include "mydatatypes.h"
#include <SPI.h>
//#include <Wire.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
WiFiClient wifiClient;
PubSubClient client(wifiClient);
HardwareSerial commSerial(0);
HardwareSerial bmsSerial(1);
//---- global variables ----
static boolean doConnect = false;
static boolean BLE_client_connected = false;
static boolean doScan = false;
packBasicInfoStruct packBasicInfo; // here shall be the latest data got from BMS
packEepromStruct packEeprom; // here shall be the latest data got from BMS
packCellInfoStruct packCellInfo; // here shall be the latest data got from BMS
const byte cBasicInfo3 = 3; // type of packet 3 = basic info
const byte cCellInfo4 = 4; // type of packet 4 = individual cell info
unsigned long previousMillis = 0;
const long interval = 2000;
bool toggle = false;
bool newPacketReceived = false;
char buffer[20];
char result[20];
void setup()
{
commSerial.println("Starting network...");
networkStartup();
client.setServer(MQTT_SERVER, MQTT_PORT);
commSerial.begin(115200, SERIAL_8N1, 3, 1);
bmsSerial.begin(9600, SERIAL_8N1, 21, 22);
commSerial.println("Starting bluetooth scan ...");
bleStartup();
}
//---------------------main loop------------------
void loop()
{
bleRequestData();
if (newPacketReceived == true)
{
printBasicInfo();
printCellInfo();
MQTTsend();
delay(10000);
}
}
//---------------------/ main loop------------------