added command processor linked to BLE and mqtt
This commit is contained in:
parent
ccd3e4ebbe
commit
88979da2d6
@ -1,3 +1,6 @@
|
|||||||
|
# merge with accepting all incoming changes
|
||||||
|
git merge [branch] --strategy-option theirs
|
||||||
|
|
||||||
# MQTT Server
|
# MQTT Server
|
||||||
|
|
||||||
MQTT server:
|
MQTT server:
|
||||||
|
|||||||
@ -18,3 +18,4 @@ VR0gBBYwFDAIBgZngQwBAgEwCAYGZ4EMAQICMAoGCCqGSM49BAMDA2cAMGQCMD+q
|
|||||||
5Uq1fSGZSKRhrnWKKXlp4DvfZCEU/MF3rbdwAaXI/KVM65YRO9HvRbfDpV3x1wIw
|
5Uq1fSGZSKRhrnWKKXlp4DvfZCEU/MF3rbdwAaXI/KVM65YRO9HvRbfDpV3x1wIw
|
||||||
CHvqqpg/8YJPDn8NJIS/Rg+lYraOseXeuNYzkjeY6RLxIDB+nLVDs9QJ3/co89Cd
|
CHvqqpg/8YJPDn8NJIS/Rg+lYraOseXeuNYzkjeY6RLxIDB+nLVDs9QJ3/co89Cd
|
||||||
-----END CERTIFICATE-----
|
-----END CERTIFICATE-----
|
||||||
|
|
||||||
|
|||||||
14
main/App.cpp
14
main/App.cpp
@ -29,6 +29,11 @@ CircBuffer * App::getBuffer()
|
|||||||
return & m_circ_buffer;
|
return & m_circ_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CommandProcessor * App::getCommandProcessor()
|
||||||
|
{
|
||||||
|
return m_commandProcessor;
|
||||||
|
}
|
||||||
|
|
||||||
void App::init()
|
void App::init()
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Starting the app...");
|
ESP_LOGW(TAG, "Starting the app...");
|
||||||
@ -36,8 +41,6 @@ void App::init()
|
|||||||
m_led = new Led(LED_PIN);
|
m_led = new Led(LED_PIN);
|
||||||
m_wifi = new Wifi();
|
m_wifi = new Wifi();
|
||||||
|
|
||||||
#if 1
|
|
||||||
|
|
||||||
bool needs_provision = true;
|
bool needs_provision = true;
|
||||||
|
|
||||||
if(SETTINGS.wifi.num > 0)
|
if(SETTINGS.wifi.num > 0)
|
||||||
@ -80,7 +83,10 @@ void App::init()
|
|||||||
|
|
||||||
otaCheck();
|
otaCheck();
|
||||||
|
|
||||||
m_led->setPulse(255, 0, 255);
|
// m_led->setPulse(255, 0, 255);
|
||||||
|
m_led->setColor(0, 0, 0);
|
||||||
|
|
||||||
|
m_commandProcessor = new CommandProcessor(*this);
|
||||||
|
|
||||||
m_mqtt_service = new MqttService(*this);
|
m_mqtt_service = new MqttService(*this);
|
||||||
m_mqtt_service->start();
|
m_mqtt_service->start();
|
||||||
@ -88,8 +94,6 @@ void App::init()
|
|||||||
m_ble_service = new BleService(*this);
|
m_ble_service = new BleService(*this);
|
||||||
m_ble_service->start();
|
m_ble_service->start();
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
m_sensor_service = new SensorService(*this);
|
m_sensor_service = new SensorService(*this);
|
||||||
m_sensor_service->start();
|
m_sensor_service->start();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
#include "BleService.h"
|
#include "BleService.h"
|
||||||
#include "Buffers.h"
|
#include "Buffers.h"
|
||||||
#include "sensors/SensorService.h"
|
#include "sensors/SensorService.h"
|
||||||
|
#include "CommandProcessor.h"
|
||||||
#include "AppIF.h"
|
#include "AppIF.h"
|
||||||
|
|
||||||
class App : AppIF
|
class App : AppIF
|
||||||
@ -23,10 +24,12 @@ protected:
|
|||||||
SensorService * m_sensor_service = nullptr;
|
SensorService * m_sensor_service = nullptr;
|
||||||
CircBuffer m_circ_buffer;
|
CircBuffer m_circ_buffer;
|
||||||
Led * m_led = nullptr;
|
Led * m_led = nullptr;
|
||||||
|
CommandProcessor * m_commandProcessor = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Led * getLed() override;
|
Led * getLed() override;
|
||||||
CircBuffer * getBuffer() override;
|
CircBuffer * getBuffer() override;
|
||||||
|
CommandProcessor * getCommandProcessor() override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
App();
|
App();
|
||||||
|
|||||||
@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
class Led;
|
class Led;
|
||||||
class CircBuffer;
|
class CircBuffer;
|
||||||
|
class CommandProcessor;
|
||||||
|
|
||||||
class AppIF
|
class AppIF
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual Led * getLed() = 0;
|
virtual Led * getLed() = 0;
|
||||||
virtual CircBuffer * getBuffer() = 0;
|
virtual CircBuffer * getBuffer() = 0;
|
||||||
|
virtual CommandProcessor * getCommandProcessor() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -14,7 +14,7 @@ static const char * TAG = "ble";
|
|||||||
|
|
||||||
BleService::BleService(App & app) : m_app(app)
|
BleService::BleService(App & app) : m_app(app)
|
||||||
{
|
{
|
||||||
m_rw = new ReaderWriter(512);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleService::start()
|
void BleService::start()
|
||||||
@ -24,7 +24,7 @@ void BleService::start()
|
|||||||
uint8_t wifi_mac[8];
|
uint8_t wifi_mac[8];
|
||||||
esp_read_mac(wifi_mac, ESP_MAC_WIFI_STA);
|
esp_read_mac(wifi_mac, ESP_MAC_WIFI_STA);
|
||||||
|
|
||||||
sprintf(m_name, "WP_%u_%02x%02x%02x", SETTINGS.device.id, wifi_mac[3], wifi_mac[4], wifi_mac[5]);
|
sprintf(m_name, "WP_%02x%02x%02x", wifi_mac[3], wifi_mac[4], wifi_mac[5]);
|
||||||
|
|
||||||
NimBLEDevice::init(m_name);
|
NimBLEDevice::init(m_name);
|
||||||
NimBLEDevice::setMTU(512);
|
NimBLEDevice::setMTU(512);
|
||||||
@ -64,49 +64,16 @@ void BleService::onDisconnect(NimBLEServer* pServer)
|
|||||||
|
|
||||||
void BleService::onRead(NimBLECharacteristic* pCharacteristic)
|
void BleService::onRead(NimBLECharacteristic* pCharacteristic)
|
||||||
{
|
{
|
||||||
int a = 1234;
|
// int a = 1234;
|
||||||
pCharacteristic->setValue(a);
|
// pCharacteristic->setValue(a);
|
||||||
ESP_LOGI(TAG, "onRead hello");
|
// ESP_LOGI(TAG, "onRead hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleService::onWrite(NimBLECharacteristic* pCharacteristic)
|
void BleService::onWrite(NimBLECharacteristic* pCharacteristic)
|
||||||
{
|
{
|
||||||
uint32_t len = pCharacteristic->getDataLength();
|
m_app.getCommandProcessor()->take();
|
||||||
memcpy(m_buffer, pCharacteristic->getValue().c_str(), len);
|
char * buffer = m_app.getCommandProcessor()->process(pCharacteristic->getValue().c_str(), pCharacteristic->getDataLength());
|
||||||
m_buffer[len] = 0;
|
pCharacteristic->setValue((uint8_t*)buffer, strlen(buffer));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "'%s'", m_buffer);
|
|
||||||
|
|
||||||
Parser p((char *)m_buffer, "|");
|
|
||||||
char buffer[80];
|
|
||||||
p.getElementAt(0, buffer, sizeof(buffer));
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "command '%s'", buffer);
|
|
||||||
|
|
||||||
if(buffer[0] == 'w')
|
|
||||||
{
|
|
||||||
ESP_LOGI(TAG, "Scanning wifi networks...");
|
|
||||||
|
|
||||||
int num_networks = WiFi.scanNetworks(false, false, false, 0);
|
|
||||||
|
|
||||||
m_rw->reset();
|
|
||||||
m_rw->appendf("w|%d|", num_networks);
|
|
||||||
for(int n = 0; n < num_networks; n++)
|
|
||||||
{
|
|
||||||
m_rw->append(WiFi.SSID(n).c_str());
|
|
||||||
m_rw->append(",");
|
|
||||||
m_rw->appendf("%d", WiFi.RSSI(n));
|
|
||||||
if(n < num_networks-1)
|
|
||||||
m_rw->append("|");
|
|
||||||
}
|
|
||||||
ESP_LOGI(TAG, "'%s'", m_rw->getBuffer());
|
|
||||||
pCharacteristic->setValue((uint8_t*)m_rw->getBuffer(), m_rw->getLen());
|
|
||||||
pCharacteristic->notify();
|
pCharacteristic->notify();
|
||||||
}
|
m_app.getCommandProcessor()->give();
|
||||||
else if(buffer[0] == 'd')
|
|
||||||
{
|
|
||||||
SETTINGS.wifi.num = 0;
|
|
||||||
SETTINGS_SAVE;
|
|
||||||
esp_restart();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <NimBLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
#include "ReaderWriter.h"
|
|
||||||
|
|
||||||
class App;
|
class App;
|
||||||
|
|
||||||
@ -15,13 +14,11 @@ protected:
|
|||||||
App & m_app;
|
App & m_app;
|
||||||
|
|
||||||
char m_name[32];
|
char m_name[32];
|
||||||
char m_buffer[512];
|
|
||||||
|
|
||||||
NimBLEServer * m_server = nullptr;
|
NimBLEServer * m_server = nullptr;
|
||||||
NimBLEService * m_service = nullptr;
|
NimBLEService * m_service = nullptr;
|
||||||
NimBLECharacteristic * m_characteristic = nullptr;
|
NimBLECharacteristic * m_characteristic = nullptr;
|
||||||
NimBLEAdvertising * m_advertising = nullptr;
|
NimBLEAdvertising * m_advertising = nullptr;
|
||||||
ReaderWriter *m_rw = nullptr;
|
|
||||||
|
|
||||||
void onConnect(NimBLEServer* pServer) override;
|
void onConnect(NimBLEServer* pServer) override;
|
||||||
void onDisconnect(NimBLEServer* pServer) override;
|
void onDisconnect(NimBLEServer* pServer) override;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
idf_component_register(SRCS main.cpp App.cpp Settings.cpp Led.cpp TaskMgr.cpp Wifi.cpp utilities.cpp
|
idf_component_register(SRCS main.cpp App.cpp Settings.cpp Led.cpp TaskMgr.cpp Wifi.cpp utilities.cpp
|
||||||
ProvisionSoftAP.cpp ReaderWriter.cpp Ota.cpp MqttService.cpp BleService.cpp sensors/Bmp280.cpp
|
ProvisionSoftAP.cpp ReaderWriter.cpp Ota.cpp MqttService.cpp BleService.cpp sensors/Bmp280.cpp
|
||||||
sensors/Bme68x.cpp sensors/SensorService.cpp sensors/LD2410.cpp Buffers.cpp SensorData.cpp
|
sensors/Bme68x.cpp sensors/SensorService.cpp sensors/LD2410.cpp Buffers.cpp SensorData.cpp
|
||||||
|
CommandProcessor.cpp
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
EMBED_TXTFILES ../html/logo.png ../html/provision.html
|
EMBED_TXTFILES ../html/logo.png ../html/provision.html
|
||||||
EMBED_TXTFILES ../certs/eventgrid.azure.pem ../certs/client1-authn-ID.key ../certs/client1-authn-ID.pem ../certs/bigfoot-inc.pem
|
EMBED_TXTFILES ../certs/eventgrid.azure.pem ../certs/client1-authn-ID.key ../certs/client1-authn-ID.pem ../certs/bigfoot-inc.pem
|
||||||
|
|||||||
226
main/CommandProcessor.cpp
Normal file
226
main/CommandProcessor.cpp
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#include "CommandProcessor.h"
|
||||||
|
#include "utilities.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
const char * TAG = "CommandProcessor";
|
||||||
|
|
||||||
|
CommandProcessor::CommandProcessor(AppIF & app) : m_app(app), m_rw(512)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BUF_LEN 128
|
||||||
|
|
||||||
|
char * getElementAt(int el, Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
buffer[0] = 0;
|
||||||
|
p.getElementAt(el, buffer, BUF_LEN);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdSetGroupId(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
if(strToInt(getElementAt(1, p, buffer), value))
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "new group value: %u", value);
|
||||||
|
SETTINGS.device.group_id = value;
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
strcpy(m_buffer, "Z|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
strcpy(m_buffer, "Z|fail");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdSetWorkingId(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
if(strToInt(getElementAt(1, p, buffer), value))
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "new device working id: %u", value);
|
||||||
|
SETTINGS.device.tata_id = value;
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
strcpy(m_buffer, "-|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
strcpy(m_buffer, "-|fail");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdRestart(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
esp_restart();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdDeleteStoredSsids(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "delete stored ssid's");
|
||||||
|
SETTINGS.wifi.num = 0;
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
strcpy(m_buffer, "d|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdReturnCurrentSsid(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "return current ssid %s and rssi %d", WiFi.SSID().c_str(), WiFi.RSSI());
|
||||||
|
|
||||||
|
m_rw.reset();
|
||||||
|
m_rw.appendf("a|%s,%d", WiFi.SSID().c_str(), WiFi.RSSI());
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "'%s'", m_rw.getBuffer());
|
||||||
|
// "a|gumball,-42"
|
||||||
|
strcpy(m_buffer, m_rw.getBuffer());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdReturnVisibleNetworks(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Scanning wifi networks...");
|
||||||
|
|
||||||
|
int num_networks = WiFi.scanNetworks(false, false, false, 0);
|
||||||
|
|
||||||
|
m_rw.reset();
|
||||||
|
m_rw.appendf("w|%d|", num_networks);
|
||||||
|
for(int n = 0; n < num_networks; n++)
|
||||||
|
{
|
||||||
|
m_rw.append(WiFi.SSID(n).c_str());
|
||||||
|
m_rw.append(",");
|
||||||
|
m_rw.appendf("%d", WiFi.RSSI(n));
|
||||||
|
if(n < num_networks - 1)
|
||||||
|
m_rw.append("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "'%s'", m_rw.getBuffer());
|
||||||
|
// "w|4|gumball,-40|miro,-79|903noe,-84|TheGoodPlace-2.4,-93"
|
||||||
|
strcpy(m_buffer, m_rw.getBuffer());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdAddNetwork(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
if(p.getElementAt(1, buffer, BUF_LEN))
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "ssid/pwd: '%s'", buffer);
|
||||||
|
char ssid[36], pwd[36];
|
||||||
|
Parser pp(buffer, ",");
|
||||||
|
if(pp.getElementAt(0, ssid, sizeof(ssid)))
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "ssid: %s", ssid);
|
||||||
|
if(pp.getElementAt(1, pwd, sizeof(pwd)))
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "pwd: %s", pwd);
|
||||||
|
|
||||||
|
if(SETTINGS.wifi.num < SETTINGS_NUM_WIFI_ENTRIES - 1)
|
||||||
|
{
|
||||||
|
int ix = SETTINGS.wifi.num;
|
||||||
|
|
||||||
|
strcpy(SETTINGS.wifi.entry[ix].ssid, ssid);
|
||||||
|
strcpy(SETTINGS.wifi.entry[ix].pwd, pwd);
|
||||||
|
SETTINGS.wifi.num = ix + 1;
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
|
||||||
|
strcpy(m_buffer, "W|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strcpy(m_buffer, "W|fail");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdReportAllSettings(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
int64_t up_time = esp_timer_get_time();
|
||||||
|
|
||||||
|
m_rw.reset();
|
||||||
|
m_rw.appendf("-,%u", SETTINGS.device.tata_id); //wid
|
||||||
|
m_rw.append("|");
|
||||||
|
m_rw.appendf("Z,%u", SETTINGS.device.group_id); //group id
|
||||||
|
m_rw.append("|");
|
||||||
|
m_rw.appendf("0,%s", SETTINGS.mqtt.device_id); //client id mwtt client id(text)
|
||||||
|
m_rw.append("|");
|
||||||
|
m_rw.appendf("2,%u", up_time/1000000); //time since reboot (seconds)
|
||||||
|
m_rw.append("|");
|
||||||
|
|
||||||
|
m_rw.append("5,");
|
||||||
|
for(int n = 0; n < SETTINGS.wifi.num; n++)
|
||||||
|
{
|
||||||
|
m_rw.append(SETTINGS.wifi.entry[n].ssid);
|
||||||
|
m_rw.append("\t");
|
||||||
|
m_rw.append(SETTINGS.wifi.entry[n].pwd);
|
||||||
|
if(n < SETTINGS.wifi.num - 1)
|
||||||
|
m_rw.append("\n");
|
||||||
|
}
|
||||||
|
m_rw.append("|");
|
||||||
|
|
||||||
|
m_rw.appendf("9,%.3f", SETTINGS.sensors.temperature.temp_offset); //temp offset
|
||||||
|
|
||||||
|
strcpy(m_buffer, m_rw.getBuffer());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdSetMqttDevId(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
if(p.getElementAt(1, buffer, BUF_LEN))
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "new mqtt dev id: '%s'", buffer);
|
||||||
|
strcpy(SETTINGS.mqtt.device_id, buffer);
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
strcpy(m_buffer, "0|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
strcpy(m_buffer, "0|fail");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CommandProcessor::cmdSetTemperatureOffset(Parser & p, char * buffer)
|
||||||
|
{
|
||||||
|
float value = 0;
|
||||||
|
|
||||||
|
if(strToFloat(getElementAt(1, p, buffer), value))
|
||||||
|
{
|
||||||
|
ESP_LOGW(TAG, "new temp offset: %f", value);
|
||||||
|
SETTINGS.sensors.temperature.temp_offset = value;
|
||||||
|
SETTINGS_SAVE;
|
||||||
|
strcpy(m_buffer, "9|ok");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
strcpy(m_buffer, "9|fail");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define COMMAND_START if(false){}
|
||||||
|
#define COMMAND(command, function) else if(strcmp(command, getElementAt(0, p, temp)) == 0) function(p, temp)
|
||||||
|
#define COMMAND_END else strcpy(m_buffer, "error");
|
||||||
|
|
||||||
|
char * CommandProcessor::process(const char * data, uint32_t len)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "%s", data);
|
||||||
|
|
||||||
|
Parser p((char *)data, "|");
|
||||||
|
char temp[BUF_LEN];
|
||||||
|
|
||||||
|
COMMAND_START
|
||||||
|
COMMAND("Z", cmdSetGroupId);
|
||||||
|
COMMAND("0", cmdSetMqttDevId);
|
||||||
|
COMMAND("-", cmdSetWorkingId);
|
||||||
|
COMMAND("s", cmdRestart);
|
||||||
|
COMMAND("d", cmdDeleteStoredSsids);
|
||||||
|
COMMAND("a", cmdReturnCurrentSsid);
|
||||||
|
COMMAND("w", cmdReturnVisibleNetworks);
|
||||||
|
COMMAND("W", cmdAddNetwork);
|
||||||
|
COMMAND("r", cmdReportAllSettings);
|
||||||
|
COMMAND("9", cmdSetTemperatureOffset);
|
||||||
|
COMMAND_END;
|
||||||
|
|
||||||
|
return m_buffer;
|
||||||
|
}
|
||||||
31
main/CommandProcessor.h
Normal file
31
main/CommandProcessor.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <System.h>
|
||||||
|
#include "ReaderWriter.h"
|
||||||
|
#include "AppIF.h"
|
||||||
|
|
||||||
|
class CommandProcessor : public Mutex
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
AppIF & m_app;
|
||||||
|
Mutex m_mutex;
|
||||||
|
ReaderWriter m_rw;
|
||||||
|
char m_buffer[512];
|
||||||
|
|
||||||
|
public:
|
||||||
|
CommandProcessor(AppIF & app);
|
||||||
|
|
||||||
|
public:
|
||||||
|
char * process(const char * data, uint32_t len = 0);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool cmdSetGroupId(Parser & p, char * buffer);
|
||||||
|
bool cmdSetWorkingId(Parser & p, char * buffer);
|
||||||
|
bool cmdRestart(Parser & p, char * buffer);
|
||||||
|
bool cmdDeleteStoredSsids(Parser & p, char * buffer);
|
||||||
|
bool cmdReturnCurrentSsid(Parser & p, char * buffer);
|
||||||
|
bool cmdReturnVisibleNetworks(Parser & p, char * buffer);
|
||||||
|
bool cmdAddNetwork(Parser & p, char * buffer);
|
||||||
|
bool cmdReportAllSettings(Parser & p, char * buffer);
|
||||||
|
bool cmdSetTemperatureOffset(Parser & p, char * buffer);
|
||||||
|
bool cmdSetMqttDevId(Parser & p, char * buffer);
|
||||||
|
};
|
||||||
@ -6,6 +6,8 @@
|
|||||||
#include "TaskMgr.h"
|
#include "TaskMgr.h"
|
||||||
#include "app_config.h"
|
#include "app_config.h"
|
||||||
#include "Buffers.h"
|
#include "Buffers.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
#include "CommandProcessor.h"
|
||||||
|
|
||||||
static const char * mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net";
|
static const char * mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net";
|
||||||
static const char * topic = "wellnuotopics/topic1";
|
static const char * topic = "wellnuotopics/topic1";
|
||||||
@ -16,8 +18,16 @@ static const char * TAG = "mqtts";
|
|||||||
void MqttService::callback(char* topic, uint8_t * payload, uint32_t length)
|
void MqttService::callback(char* topic, uint8_t * payload, uint32_t length)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Message arrived in topic: %s\n", topic);
|
ESP_LOGI(TAG, "Message arrived in topic: %s\n", topic);
|
||||||
|
m_app_if.getCommandProcessor()->take();
|
||||||
|
|
||||||
payload[length] = 0;
|
payload[length] = 0;
|
||||||
ESP_LOGW(TAG, "%s", (char*)payload);
|
ESP_LOGW(TAG, "%s", (char*)payload);
|
||||||
|
char * buffer = m_app_if.getCommandProcessor()->process((char *)payload, strlen((char *)payload));
|
||||||
|
// reply to
|
||||||
|
ESP_LOGI(TAG, "%s", buffer);
|
||||||
|
m_mqtt_client->publish("/well_hub", buffer, strlen(buffer));
|
||||||
|
|
||||||
|
m_app_if.getCommandProcessor()->give();
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttService::MqttService(AppIF & app_if) : m_app_if(app_if)
|
MqttService::MqttService(AppIF & app_if) : m_app_if(app_if)
|
||||||
@ -31,28 +41,35 @@ void MqttService::task()
|
|||||||
{
|
{
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
// if(m_app_if.getBuffer()->getSemaphore().take(5000)) // wait for the data to become available
|
if(m_app_if.getBuffer()->waitForDataAvailable(1000))
|
||||||
if(m_app_if.getBuffer()->waitForDataAvailable(5000))
|
|
||||||
{
|
{
|
||||||
uint8_t len = 0;
|
uint8_t len = 0;
|
||||||
if(m_app_if.getBuffer()->getBlock(buffer, len))
|
if(m_app_if.getBuffer()->getBlock(buffer, len))
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "got data, len %d", len);
|
|
||||||
|
|
||||||
while (!m_mqtt_client->connected())
|
while (!m_mqtt_client->connected())
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "connecting to mqtt broker...");
|
ESP_LOGI(TAG, "connecting to mqtt broker, dev id '%s'...", SETTINGS.mqtt.device_id);
|
||||||
if (m_mqtt_client->connect("Esp32 client", "client1-authn-ID", NULL))
|
if (m_mqtt_client->connect("Esp32 client", SETTINGS.mqtt.device_id, NULL))
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "connected");
|
ESP_LOGI(TAG, "connected");
|
||||||
if(m_mqtt_client->subscribe("my_topic"))
|
uint8_t mac[6];
|
||||||
{
|
WiFi.macAddress(mac);
|
||||||
|
|
||||||
|
char top[64];
|
||||||
|
|
||||||
|
sprintf(top, "/%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
ESP_LOGI(TAG, "Subscribing to %s", top);
|
||||||
|
if(m_mqtt_client->subscribe(top))
|
||||||
|
ESP_LOGI(TAG, "subscribed");
|
||||||
|
else
|
||||||
|
ESP_LOGE(TAG, "subscribe failed");
|
||||||
|
|
||||||
|
sprintf(top, "/%08x", SETTINGS.device.group_id);
|
||||||
|
ESP_LOGI(TAG, "Subscribing to %s", top);
|
||||||
|
if(m_mqtt_client->subscribe(top))
|
||||||
ESP_LOGI(TAG, "subscribed");
|
ESP_LOGI(TAG, "subscribed");
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
ESP_LOGE(TAG, "subscribe failed");
|
ESP_LOGE(TAG, "subscribe failed");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -69,7 +86,6 @@ void MqttService::task()
|
|||||||
m_mqtt_client->loop();
|
m_mqtt_client->loop();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "publishing...");
|
ESP_LOGI(TAG, "publishing...");
|
||||||
// bool val = m_mqtt_client->publish(topic, "Hi I'm ESP32 ^^");
|
|
||||||
bool val = m_mqtt_client->publish(topic, buffer, len);
|
bool val = m_mqtt_client->publish(topic, buffer, len);
|
||||||
|
|
||||||
if(val)
|
if(val)
|
||||||
@ -82,10 +98,12 @@ void MqttService::task()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
if(m_mqtt_client->connected())
|
||||||
|
{
|
||||||
m_mqtt_client->loop();
|
m_mqtt_client->loop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MqttService::start()
|
void MqttService::start()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -90,6 +90,10 @@ void Settings::setDefaults()
|
|||||||
m_data->wifi.dns_primary = PP_HTONL(LWIP_MAKEU32(8, 8, 8, 8));
|
m_data->wifi.dns_primary = PP_HTONL(LWIP_MAKEU32(8, 8, 8, 8));
|
||||||
m_data->wifi.dns_secondary = PP_HTONL(LWIP_MAKEU32(8, 8, 4, 4));
|
m_data->wifi.dns_secondary = PP_HTONL(LWIP_MAKEU32(8, 8, 4, 4));
|
||||||
|
|
||||||
m_data->device.id = 101;
|
m_data->device.tata_id = 100;
|
||||||
m_data->device.group_id = 5101;
|
m_data->device.group_id = 5101;
|
||||||
|
|
||||||
|
m_data->sensors.temperature.temp_offset = 0;
|
||||||
|
|
||||||
|
strcpy(m_data->mqtt.device_id, "client1-authn-ID");
|
||||||
}
|
}
|
||||||
@ -106,10 +106,25 @@ struct LED
|
|||||||
// general device level settings
|
// general device level settings
|
||||||
struct DEVICE
|
struct DEVICE
|
||||||
{
|
{
|
||||||
uint32_t id;
|
uint32_t tata_id;
|
||||||
uint32_t group_id;
|
uint32_t group_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct MQTT
|
||||||
|
{
|
||||||
|
char device_id[64];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TEMPERATURE
|
||||||
|
{
|
||||||
|
float temp_offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SENSORS
|
||||||
|
{
|
||||||
|
struct TEMPERATURE temperature;
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
struct NV_DATA
|
struct NV_DATA
|
||||||
@ -118,6 +133,8 @@ struct NV_DATA
|
|||||||
struct LED led;
|
struct LED led;
|
||||||
struct WIFI wifi;
|
struct WIFI wifi;
|
||||||
struct DEVICE device;
|
struct DEVICE device;
|
||||||
|
struct MQTT mqtt;
|
||||||
|
struct SENSORS sensors;
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|||||||
@ -8,6 +8,8 @@
|
|||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
#include "app_config.h"
|
#include "app_config.h"
|
||||||
|
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
|
||||||
static const char *TAG = "Wifi";
|
static const char *TAG = "Wifi";
|
||||||
#define IGNORE_SSID_MINS 20
|
#define IGNORE_SSID_MINS 20
|
||||||
|
|
||||||
@ -80,9 +82,15 @@ Wifi::WIFI_STATUS Wifi::connectTo(int index)
|
|||||||
|
|
||||||
ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[index].ssid);
|
ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[index].ssid);
|
||||||
|
|
||||||
|
// set hostname
|
||||||
|
uint8_t mac[6];
|
||||||
|
WiFi.macAddress(mac);
|
||||||
|
char buffer[32];
|
||||||
|
sprintf(buffer, "wellhub-tag%02x%02x%02x", mac[3], mac[4], mac[5]);
|
||||||
|
WiFi.hostname(buffer);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
|
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
|
||||||
WiFi.hostname("wellhub");
|
|
||||||
|
|
||||||
WiFi.begin(SETTINGS.wifi.entry[index].ssid, SETTINGS.wifi.entry[index].pwd);
|
WiFi.begin(SETTINGS.wifi.entry[index].ssid, SETTINGS.wifi.entry[index].pwd);
|
||||||
int status = WiFi.waitForConnectResult(10000);
|
int status = WiFi.waitForConnectResult(10000);
|
||||||
|
|||||||
@ -5,6 +5,8 @@
|
|||||||
#include "SensorService.h"
|
#include "SensorService.h"
|
||||||
#include "Buffers.h"
|
#include "Buffers.h"
|
||||||
#include "SensorData.h"
|
#include "SensorData.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
#include "Led.h"
|
||||||
|
|
||||||
static const char *TAG = "sensors";
|
static const char *TAG = "sensors";
|
||||||
|
|
||||||
@ -33,14 +35,28 @@ void SensorService::start()
|
|||||||
m_bme68x = new Bme68x(Wire);
|
m_bme68x = new Bme68x(Wire);
|
||||||
m_ld2410 = new LD2410();
|
m_ld2410 = new LD2410();
|
||||||
|
|
||||||
|
bool hw_fault = false;
|
||||||
|
|
||||||
if(!m_bmp280->init())
|
if(!m_bmp280->init())
|
||||||
|
{
|
||||||
|
hw_fault = true;
|
||||||
ESP_LOGE(TAG, "bmp280 sensor error");
|
ESP_LOGE(TAG, "bmp280 sensor error");
|
||||||
|
}
|
||||||
|
|
||||||
if(!m_bme68x->init())
|
if(!m_bme68x->init())
|
||||||
|
{
|
||||||
|
hw_fault = true;
|
||||||
ESP_LOGE(TAG, "bme68x sensor error");
|
ESP_LOGE(TAG, "bme68x sensor error");
|
||||||
|
}
|
||||||
|
|
||||||
if(!m_ld2410->init())
|
if(!m_ld2410->init())
|
||||||
|
{
|
||||||
|
hw_fault = true;
|
||||||
ESP_LOGE(TAG, "ld2410 sensor error");
|
ESP_LOGE(TAG, "ld2410 sensor error");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(hw_fault)
|
||||||
|
m_app_if.getLed()->setColor(255, 0, 0);
|
||||||
|
|
||||||
assert(m_i2c1_task = TaskMgr::getInstance().createTask(std::bind(&SensorService::run_i2c_1, this),
|
assert(m_i2c1_task = TaskMgr::getInstance().createTask(std::bind(&SensorService::run_i2c_1, this),
|
||||||
I2C1_TASK_NAME, I2C1_TASK_STACK_SIZE, I2C1_TASK_PRIORITY, I2C1_TASK_CORE));
|
I2C1_TASK_NAME, I2C1_TASK_STACK_SIZE, I2C1_TASK_PRIORITY, I2C1_TASK_CORE));
|
||||||
@ -61,11 +77,11 @@ void::SensorService::processPressure(float pressure)
|
|||||||
|
|
||||||
uint64_t now = esp_timer_get_time();
|
uint64_t now = esp_timer_get_time();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "delta T: %d", (uint32_t)((now-previous)/1000));
|
// ESP_LOGI(TAG, "delta T: %d", (uint32_t)((now-previous)/1000));
|
||||||
|
|
||||||
filtered = 0.95 * filtered + 0.05 * pressure;
|
filtered = 0.95 * filtered + 0.05 * pressure;
|
||||||
previous = now;
|
previous = now;
|
||||||
ESP_LOGI(TAG, "%0.3f \t%0.3f", pressure, filtered);
|
// ESP_LOGI(TAG, "%0.3f \t%0.3f", pressure, filtered);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SensorService::postBme68xData(float pressure, float temp)
|
void SensorService::postBme68xData(float pressure, float temp)
|
||||||
@ -76,7 +92,7 @@ void SensorService::postBme68xData(float pressure, float temp)
|
|||||||
msg->humidity = m_bme_data.humidity;
|
msg->humidity = m_bme_data.humidity;
|
||||||
msg->light = m_light_value;
|
msg->light = m_light_value;
|
||||||
msg->pressure = pressure;
|
msg->pressure = pressure;
|
||||||
msg->temperature = temp;
|
msg->temperature = temp + SETTINGS.sensors.temperature.temp_offset;
|
||||||
|
|
||||||
int num_total = 0;
|
int num_total = 0;
|
||||||
|
|
||||||
@ -164,7 +180,7 @@ void SensorService::run_uart()
|
|||||||
if(has_read && esp_timer_get_time() - last_read >= ms_to_us(10000-50))
|
if(has_read && esp_timer_get_time() - last_read >= ms_to_us(10000-50))
|
||||||
{
|
{
|
||||||
int64_t now = esp_timer_get_time();
|
int64_t now = esp_timer_get_time();
|
||||||
ESP_LOGI(TAG, "count %d", (int)m_ld2410->stationary_energy[0]);
|
// ESP_LOGI(TAG, "count %d", (int)m_ld2410->stationary_energy[0]);
|
||||||
|
|
||||||
if(m_ld2410->stationary_energy[0] != 0)
|
if(m_ld2410->stationary_energy[0] != 0)
|
||||||
{
|
{
|
||||||
@ -181,7 +197,7 @@ void SensorService::run_uart()
|
|||||||
|
|
||||||
m_app_if.getBuffer()->putBlock((uint8_t*)&msg, sizeof(msg));
|
m_app_if.getBuffer()->putBlock((uint8_t*)&msg, sizeof(msg));
|
||||||
|
|
||||||
ESP_LOGI(TAG, "delta t: %lld", (now - last_read)/1000);
|
// ESP_LOGI(TAG, "delta t: %lld", (now - last_read)/1000);
|
||||||
last_read = now;
|
last_read = now;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|||||||
@ -27,3 +27,45 @@ void dump_bytes(const void *data, uint32_t len)
|
|||||||
if (hasMore)
|
if (hasMore)
|
||||||
ESP_LOGI(TAG, "%s", c);
|
ESP_LOGI(TAG, "%s", c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool strToInt(const char *str, int & val, int base)
|
||||||
|
{
|
||||||
|
if(str == nullptr || str[0] == 0)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "input str invalid");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *endptr;
|
||||||
|
|
||||||
|
val = strtol(str, &endptr, base);
|
||||||
|
|
||||||
|
if(endptr == str || *endptr != 0)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "input str invalid (2)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool strToFloat(const char *str, float & val)
|
||||||
|
{
|
||||||
|
if(str == nullptr || str[0] == 0)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "input str invalid");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *endptr;
|
||||||
|
|
||||||
|
val = strtof(str, &endptr);
|
||||||
|
|
||||||
|
if(endptr == str || *endptr != 0)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "input str invalid (2)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@ -6,5 +6,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
void dump_bytes(const void *data, uint32_t len);
|
void dump_bytes(const void *data, uint32_t len);
|
||||||
|
bool strToInt(const char *str, int & val, int base = 10);
|
||||||
|
bool strToFloat(const char *str, float & val);
|
||||||
|
|
||||||
#endif /* __UTILITIES_H__ */
|
#endif /* __UTILITIES_H__ */
|
||||||
@ -7,6 +7,7 @@ mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net"
|
|||||||
mqtt_port = 8883
|
mqtt_port = 8883
|
||||||
mqtt_topic = "wellnuotopics/topic1"
|
mqtt_topic = "wellnuotopics/topic1"
|
||||||
mqtt_client_id = "client1-authn-ID"
|
mqtt_client_id = "client1-authn-ID"
|
||||||
|
mqtt_client_id = "292"
|
||||||
|
|
||||||
connected = False
|
connected = False
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ def connect_mqtt(client_id):
|
|||||||
else:
|
else:
|
||||||
print("Failed to connect, return code %d\n", rc)
|
print("Failed to connect, return code %d\n", rc)
|
||||||
|
|
||||||
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, "client_id")
|
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, "asdasdadaswd")
|
||||||
client.tls_set(
|
client.tls_set(
|
||||||
ca_certs='eventgrid.azure_full.pem',
|
ca_certs='eventgrid.azure_full.pem',
|
||||||
certfile='../certs/client1-authn-ID.pem',
|
certfile='../certs/client1-authn-ID.pem',
|
||||||
@ -47,7 +48,7 @@ def main() -> None:
|
|||||||
while(connected == False):
|
while(connected == False):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
publish(client, "wellnuotopics/topic1", "hello")
|
publish(client, "/000013ed", "s")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user