SpejsNode: JSON broker configuration & initial TLS setup
parent
517ceea15c
commit
19a58cf28f
|
@ -1,2 +1,3 @@
|
||||||
SPIFF_SIZE = 196600
|
SPIFF_SIZE = 196600
|
||||||
DISABLE_SPIFFS = 0
|
DISABLE_SPIFFS = 0
|
||||||
|
ENABLE_SSL = 1
|
||||||
|
|
|
@ -6,8 +6,15 @@
|
||||||
|
|
||||||
#define CONFIG_FILE "config.json"
|
#define CONFIG_FILE "config.json"
|
||||||
|
|
||||||
|
uint8_t hexToInt(char c) {
|
||||||
|
if (c >= '0' && c <= '9') return c - '0';
|
||||||
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||||||
|
return c - 'a' + 10;
|
||||||
|
}
|
||||||
|
|
||||||
void SpejsNode::init(bool debug) {
|
void SpejsNode::init(bool debug) {
|
||||||
deviceID = WifiStation.getMAC().substring(6, 12);
|
deviceID = WifiStation.getMAC().substring(6, 12);
|
||||||
|
brokerClient = "iot-" + deviceID;
|
||||||
|
|
||||||
currentSlot = 0;
|
currentSlot = 0;
|
||||||
if(!rboot_get_last_boot_rom(¤tSlot)) {
|
if(!rboot_get_last_boot_rom(¤tSlot)) {
|
||||||
|
@ -48,12 +55,18 @@ void SpejsNode::loadJSON(std::vector<EndpointInitializer> initializers) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
|
||||||
if (fileExist(CONFIG_FILE)) {
|
if (fileExist(CONFIG_FILE)) {
|
||||||
int size = fileGetSize(CONFIG_FILE);
|
int size = fileGetSize(CONFIG_FILE);
|
||||||
debugf("Found config file, %d bytes", size);
|
debugf("Found config file, %d bytes", size);
|
||||||
char* jsonString = new char[size + 1];
|
char* jsonString = new char[size + 1];
|
||||||
fileGetContent(CONFIG_FILE, jsonString, size + 1);
|
fileGetContent(CONFIG_FILE, jsonString, size + 1);
|
||||||
|
|
||||||
JsonObject& root = jsonBuffer.parseObject(jsonString);
|
JsonObject& root = jsonBuffer.parseObject(jsonString);
|
||||||
|
if (!root.success()) {
|
||||||
|
debugf("invalid config");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (root.containsKey("name"))
|
if (root.containsKey("name"))
|
||||||
deviceType = (root["name"]).asString();
|
deviceType = (root["name"]).asString();
|
||||||
|
@ -72,6 +85,25 @@ void SpejsNode::loadJSON(std::vector<EndpointInitializer> initializers) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Broker configuration
|
||||||
|
if (root.containsKey("broker")) {
|
||||||
|
auto brokerPort = root.containsKey("brokerPort") ? root["brokerPort"] : 1883;
|
||||||
|
brokerUseTLS = root["brokerUseTLS"];
|
||||||
|
brokerURL = root["broker"].as<String>();
|
||||||
|
|
||||||
|
String hash = root.get<String>("brokerSHA1");
|
||||||
|
if ((hash.length() + 1) % 3 == 0) {
|
||||||
|
int hashLength = (hash.length() + 1) / 3;
|
||||||
|
uint8_t* hashBlob = new uint8_t[hashLength];
|
||||||
|
|
||||||
|
for (int i = 0; i < hashLength; i++) {
|
||||||
|
hashBlob[i] = hexToInt(hash[3*i]) << 4 | hexToInt(hash[3*i+1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fingerprints.certSha1 = hashBlob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JsonObject& data = root.get<JsonObject&>("endpoints");
|
JsonObject& data = root.get<JsonObject&>("endpoints");
|
||||||
for (auto it: data) {
|
for (auto it: data) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
@ -164,12 +196,13 @@ void SpejsNode::onConnected() {
|
||||||
mqtt.setWill(DEV_TOPIC("$online"), "false", 1, true);
|
mqtt.setWill(DEV_TOPIC("$online"), "false", 1, true);
|
||||||
|
|
||||||
#ifdef ENABLE_SSL
|
#ifdef ENABLE_SSL
|
||||||
const uint8_t sha1Fingerprint[] = SSL_FINGERPRINT;
|
if (brokerUseTLS) {
|
||||||
mqtt.addSslOptions(SSL_SERVER_VERIFY_LATER);
|
mqtt.addSslOptions(SSL_SERVER_VERIFY_LATER);
|
||||||
mqtt.setSslFingerprint(sha1Fingerprint, 20);
|
mqtt.pinCertificate(fingerprints);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mqtt.connect(brokerURL, "iot-" + deviceID);
|
mqtt.connect(brokerURL, brokerClient);
|
||||||
|
|
||||||
for(unsigned int i = 0 ; i < endpoints.count() ; i++) {
|
for(unsigned int i = 0 ; i < endpoints.count() ; i++) {
|
||||||
endpoints.valueAt(i)->onConnected();
|
endpoints.valueAt(i)->onConnected();
|
||||||
|
|
|
@ -76,7 +76,14 @@ protected:
|
||||||
String wifiSSID = WIFI_SSID;
|
String wifiSSID = WIFI_SSID;
|
||||||
String wifiPassword = WIFI_PWD;
|
String wifiPassword = WIFI_PWD;
|
||||||
|
|
||||||
|
String brokerURL = "mqtt://" MQTT_BROKER;
|
||||||
|
String brokerClient;
|
||||||
|
|
||||||
bool brokerUseTLS = false;
|
bool brokerUseTLS = false;
|
||||||
|
|
||||||
|
#ifdef ENABLE_SSL
|
||||||
|
SslFingerprints fingerprints;
|
||||||
|
#endif
|
||||||
public:
|
public:
|
||||||
MqttClient mqtt;
|
MqttClient mqtt;
|
||||||
HttpServer http;
|
HttpServer http;
|
||||||
|
|
Loading…
Reference in New Issue