Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ platform_packages =
lib_deps =
https://github.com/tzapu/WiFiManager.git#0d84861270c3cd64f72a4eaf34443ee580d2547e ; Anchor to latest commit as of 8/7/24
pstolarz/OneWireNg@^0.13.1
https://github.com/pstolarz/Arduino-Temperature-Control-Library.git#ba26a1f1ee74dc875d3cc8fdb2ea1f55ee5ee333 ; Anchor to latest commit as of 1/26/24
bblanchon/ArduinoJson @ ^6.21.2
bblanchon/StreamUtils @ ^1.7.3
https://github.com/pstolarz/Arduino-Temperature-Control-Library.git#475f390038fe6828f0403960b8634a5227e54b34 ; Anchor to latest commit as of 12/15/24
bblanchon/ArduinoJson @ ^7.2.1
bblanchon/StreamUtils @ ^1.9.0
; https://github.com/thorrak/AsyncTCP
; https://github.com/thorrak/ESPAsyncWebServer.git
https://github.com/thorrak/Arduino-Log.git ; // Need this until ArduinoLog merges https://github.com/thijse/Arduino-Log/pull/21
lib_deps_bluetooth =
h2zero/NimBLE-Arduino @ ^1.4.1 ; https://github.com/h2zero/NimBLE-Arduino.git
https://github.com/h2zero/NimBLE-Arduino.git#master ; Need this until this gets merged: https://github.com/h2zero/NimBLE-Arduino/commit/c7ce2892280f5eb9bcfe541ff9765d527472a6da
; h2zero/NimBLE-Arduino @ ^2.1.0 ; https://github.com/h2zero/NimBLE-Arduino.git

; Let's also include some other common build flags for ease-of-use
build_flags =
!python3 scripts/gen_version.py
-DCONFIG_USE_NATIVE_CPP_NEW
-D CORE_DEBUG_LEVEL=0 ; Set core Arduino log level (5 = high)
-D ARDUINO_LOG_LEVEL=0 ; Set Serial log level (6 = high)
-D CONFIG_NIMBLE_CPP_LOG_LEVEL=0 ; Set NimBLE log level (4 = high)
-D DISABLE_LOGGING ; This will remove log lib from sketch

upload_speed = 460800
monitor_speed = 115200
; These are true for most devices on MacOS - Will likely need to be changed if compiling on other platforms
Expand Down
39 changes: 17 additions & 22 deletions src/CommandProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void CommandProcessor::commandNotImplemented(const char command, const String me
* \ingroup commands
*/
void CommandProcessor::versionInfo() {
DynamicJsonDocument doc(256);
JsonDocument doc;
// v version
// s shield type
// y: simulator
Expand Down Expand Up @@ -257,7 +257,7 @@ void CommandProcessor::setAlarmState(bool enabled) { alarm_actuator.setActive(en
* \ingroup commands
*/
void CommandProcessor::listDevices() {
DynamicJsonDocument doc(2048);
JsonDocument doc;
deviceManager.listDevices(doc);
piLink.sendJsonMessage('d', doc);
}
Expand All @@ -268,11 +268,7 @@ void CommandProcessor::listDevices() {
* \ingroup commands
*/
void CommandProcessor::listHardware() {
#if !defined(HAS_BLUETOOTH) && !defined(EXTERN_SENSOR_ACTUATOR_SUPPORT)
DynamicJsonDocument doc(2048);
#else
DynamicJsonDocument doc(8192);
#endif
JsonDocument doc;

EnumerateHardware spec;
deviceManager.readJsonIntoHardwareSpec(spec); // This reads JSON off the wire into an EnumerateHardware object
Expand All @@ -288,7 +284,7 @@ void CommandProcessor::listHardware() {
*/
void CommandProcessor::parseDeviceDefinition() {
DeviceDefinition dev;
DynamicJsonDocument doc(512);
JsonDocument doc;

piLink.receiveJsonMessage(doc); // Read the JSON off the line from the Pi
dev = DeviceManager::readJsonIntoDeviceDef(doc); // Parse the JSON into a DeviceDefinition object
Expand Down Expand Up @@ -322,7 +318,7 @@ void CommandProcessor::wifiInfo() {
return;
}

DynamicJsonDocument doc(1024);
JsonDocument doc;
wifi_connection_info(doc);
piLink.sendJsonMessage('W', doc);
}
Expand All @@ -340,7 +336,7 @@ void CommandProcessor::toggleBacklight() { ::toggleBacklight = !::toggleBackligh
* \ingroup commands
*/
void CommandProcessor::getLcdContent() {
DynamicJsonDocument doc(256);
JsonDocument doc;
getLcdContentJson(doc);
piLink.sendJsonMessage('L', doc);
}
Expand All @@ -361,7 +357,7 @@ void CommandProcessor::printTemperatures() {
* \ingroup commands
*/
void CommandProcessor::printRawTemperatures() {
DynamicJsonDocument doc(1024);
JsonDocument doc;
deviceManager.rawDeviceValues(doc);
piLink.sendJsonMessage('R', doc);
}
Expand All @@ -372,13 +368,12 @@ void CommandProcessor::printRawTemperatures() {
* \see EepromManager::initializeEeprom()
*/
void CommandProcessor::initEeprom() {
StaticJsonDocument<128> doc;
JsonDocument doc;
piLink.receiveJsonMessage(doc);

// Due to the "scanning" issue, we now need to test that there is an
// additional key being appended to the initializeEeprom command
if(!doc.containsKey(ExtendedSettingsKeys::eepromReset) ||
!doc[ExtendedSettingsKeys::eepromReset].is<bool>() || !doc[ExtendedSettingsKeys::eepromReset].as<bool>()) {
if(!doc[ExtendedSettingsKeys::eepromReset].is<bool>() || !doc[ExtendedSettingsKeys::eepromReset].as<bool>()) {
logError(INFO_UNCONFIRMED_EEPROM_RESET);
return;
}
Expand All @@ -393,7 +388,7 @@ void CommandProcessor::initEeprom() {
* \brief Print out the configured device names
*/
void CommandProcessor::printDeviceNames() {
DynamicJsonDocument doc(256);
JsonDocument doc;
DeviceNameManager::enumerateDeviceNames(doc);
piLink.sendJsonMessage('N', doc);
}
Expand All @@ -402,7 +397,7 @@ void CommandProcessor::printDeviceNames() {
* \brief Process incoming settings
*/
void CommandProcessor::processSettingsJson() {
DynamicJsonDocument doc(256);
JsonDocument doc;
piLink.receiveJsonMessage(doc);

// Process
Expand All @@ -424,7 +419,7 @@ void CommandProcessor::processSettingsJson() {
* \brief Process incoming extended settings
*/
void CommandProcessor::processExtendedSettingsJson() {
DynamicJsonDocument doc(256);
JsonDocument doc;
piLink.receiveJsonMessage(doc);

// Process
Expand All @@ -451,7 +446,7 @@ void CommandProcessor::processExtendedSettingsJson() {
* @see DeviceNameManager::setDeviceName
*/
void CommandProcessor::setDeviceNames() {
DynamicJsonDocument doc(256);
JsonDocument doc;
piLink.receiveJsonMessage(doc);

JsonObject root = doc.as<JsonObject>();
Expand All @@ -471,7 +466,7 @@ void CommandProcessor::setDeviceNames() {
* \brief Send control settings as JSON string
*/
void CommandProcessor::sendControlSettings() {
DynamicJsonDocument doc(256);
JsonDocument doc;
tempControl.getControlSettingsDoc(doc);
piLink.sendJsonMessage('S', doc);
}
Expand All @@ -480,7 +475,7 @@ void CommandProcessor::sendControlSettings() {
* \brief Send extended settings as JSON string
*/
void CommandProcessor::sendExtendedSettings() {
DynamicJsonDocument doc(256);
JsonDocument doc;
extendedSettings.toJson(doc);
piLink.sendJsonMessage('X', doc);
}
Expand All @@ -492,7 +487,7 @@ void CommandProcessor::sendExtendedSettings() {
* these
*/
void CommandProcessor::sendControlConstants() {
DynamicJsonDocument doc(1024);
JsonDocument doc;
tempControl.getControlConstantsDoc(doc);
piLink.sendJsonMessage('C', doc);
}
Expand All @@ -503,7 +498,7 @@ void CommandProcessor::sendControlConstants() {
* Useful for debugging and choosing parameters
*/
void CommandProcessor::sendControlVariables() {
DynamicJsonDocument doc(1024);
JsonDocument doc;
tempControl.getControlVariablesDoc(doc);
piLink.sendJsonMessage('V', doc);
}
94 changes: 49 additions & 45 deletions src/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void DeviceManager::installDevice(DeviceConfig& config)
* \param doc - JSON document containing DeviceDefinition parameters
* @return populated DeviceDefinition
*/
DeviceDefinition DeviceManager::readJsonIntoDeviceDef(const DynamicJsonDocument& doc) {
DeviceDefinition DeviceManager::readJsonIntoDeviceDef(const JsonDocument& doc) {
DeviceDefinition dev;

if(doc[DeviceDefinitionKeys::hardware].is<uint8_t>()) {
Expand All @@ -388,8 +388,11 @@ DeviceDefinition DeviceManager::readJsonIntoDeviceDef(const DynamicJsonDocument&
break;
#ifdef HAS_BLUETOOTH
case DEVICE_HARDWARE_BLUETOOTH_INKBIRD:
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>(), 0);
break;
case DEVICE_HARDWARE_BLUETOOTH_TILT:
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>());
// Tilts use address type 1 ("random", which (correctly!) indicates they didn't buy a MAC block)
dev.btAddress = NimBLEAddress(doc[DeviceDefinitionKeys::address].as<std::string>(), 1);
break;
#endif
#ifdef EXTERN_SENSOR_ACTUATOR_SUPPORT
Expand All @@ -403,43 +406,37 @@ DeviceDefinition DeviceManager::readJsonIntoDeviceDef(const DynamicJsonDocument&
}
}

if(doc.containsKey(DeviceDefinitionKeys::calibrateadjust)) {
if(doc[DeviceDefinitionKeys::calibrateadjust].is<double>()) {
temperature tempDiff = 0;
if(doc[DeviceDefinitionKeys::calibrateadjust].is<double>()) {
char buff[10];
dtostrf(doc[DeviceDefinitionKeys::calibrateadjust].as<double>(), 4, 6, buff);
tempDiff = stringToTempDiff(buff);
} else if(doc[DeviceDefinitionKeys::calibrateadjust].is<const char *>())
tempDiff = stringToTempDiff(doc[DeviceDefinitionKeys::calibrateadjust].as<const char *>());
char buff[10];
dtostrf(doc[DeviceDefinitionKeys::calibrateadjust].as<double>(), 4, 6, buff);
tempDiff = stringToTempDiff(buff);
dev.calibrationAdjust = fixed4_4(tempDiff >> (TEMP_FIXED_POINT_BITS - TEMP_CALIBRATION_OFFSET_PRECISION));
} else if(doc[DeviceDefinitionKeys::calibrateadjust].is<const char *>()) {
temperature tempDiff = stringToTempDiff(doc[DeviceDefinitionKeys::calibrateadjust].as<const char *>());
dev.calibrationAdjust = fixed4_4(tempDiff >> (TEMP_FIXED_POINT_BITS - TEMP_CALIBRATION_OFFSET_PRECISION));
}

// dev.id defaults to -1, so if this fails, the device won't get processed by deviceManager.updateDeviceDefinition
if(doc.containsKey(DeviceDefinitionKeys::index) && doc[DeviceDefinitionKeys::index].is<uint8_t>())
dev.id = doc[DeviceDefinitionKeys::index].as<uint8_t>();
if(doc[DeviceDefinitionKeys::index].is<uint8_t>()) dev.id = doc[DeviceDefinitionKeys::index].as<uint8_t>();

if(doc.containsKey(DeviceDefinitionKeys::chamber) && doc[DeviceDefinitionKeys::chamber].is<uint8_t>())
dev.chamber = doc[DeviceDefinitionKeys::chamber].as<uint8_t>();
if(doc[DeviceDefinitionKeys::chamber].is<uint8_t>()) dev.chamber = doc[DeviceDefinitionKeys::chamber].as<uint8_t>();

if(doc.containsKey(DeviceDefinitionKeys::beer) && doc[DeviceDefinitionKeys::beer].is<uint8_t>())
dev.beer = doc[DeviceDefinitionKeys::beer].as<uint8_t>();
if(doc[DeviceDefinitionKeys::beer].is<uint8_t>()) dev.beer = doc[DeviceDefinitionKeys::beer].as<uint8_t>();

if(doc.containsKey(DeviceDefinitionKeys::function) && doc[DeviceDefinitionKeys::function].is<uint8_t>())
dev.deviceFunction = doc[DeviceDefinitionKeys::function].as<uint8_t>();
if(doc[DeviceDefinitionKeys::function].is<uint8_t>()) dev.deviceFunction = doc[DeviceDefinitionKeys::function].as<uint8_t>();

dev.deactivate = false;

if(doc.containsKey(DeviceDefinitionKeys::pin) && doc[DeviceDefinitionKeys::pin].is<uint8_t>())
if(doc[DeviceDefinitionKeys::pin].is<uint8_t>())
dev.pinNr = doc[DeviceDefinitionKeys::pin].as<uint8_t>();

if(doc.containsKey(DeviceDefinitionKeys::invert)) {
if(doc[DeviceDefinitionKeys::invert].is<bool>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<bool>() ? 1 : 0;
else if(doc[DeviceDefinitionKeys::invert].is<const char *>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<const char*>()[0] == '1' ? 1 : 0;
else if (doc[DeviceDefinitionKeys::invert].is<uint8_t>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<uint8_t>();
}
if(doc[DeviceDefinitionKeys::invert].is<bool>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<bool>() ? 1 : 0;
else if(doc[DeviceDefinitionKeys::invert].is<const char *>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<const char*>()[0] == '1' ? 1 : 0;
else if (doc[DeviceDefinitionKeys::invert].is<uint8_t>())
dev.invert = doc[DeviceDefinitionKeys::invert].as<uint8_t>();

return dev;
}
Expand Down Expand Up @@ -704,7 +701,7 @@ inline bool hasOnewire(DeviceHardware hw)
* Used for outputting device information
*/
void DeviceManager::serializeJsonDevice(JsonDocument& doc, device_slot_t slot, DeviceConfig& config, const char* value) {
DynamicJsonDocument deviceObj(1024);
JsonDocument deviceObj;
config.toJson(deviceObj);

if(strlen(value) > 0)
Expand Down Expand Up @@ -810,14 +807,25 @@ device_slot_t findHardwareDevice(DeviceConfig& find)
*
* **Warning:** the read value does not include any calibration offset.
*/
inline void DeviceManager::readTempSensorValue(DeviceConfig::Hardware hw, char* out)
inline void DeviceManager::readTempSensorValue(DeviceHardware hw_type, DeviceConfig::Hardware hw, char* out)
{
#if !BREWPI_SIMULATE
OneWire* bus = oneWireBus(hw.pinNr);
OneWireTempSensor sensor(bus, hw.address, 0); // NB: this value is uncalibrated, since we don't have the calibration offset until the device is configured
temperature temp = INVALID_TEMP;
if (sensor.init())
temp = sensor.read();

if(hw_type == DEVICE_HARDWARE_ONEWIRE_TEMP) {
OneWire* bus = oneWireBus(hw.pinNr);
OneWireTempSensor sensor(bus, hw.address, 0); // NB: this value is uncalibrated, since we don't have the calibration offset until the device is configured
if (sensor.init())
temp = sensor.read();
}
#ifdef HAS_BLUETOOTH
else if(hw_type == DEVICE_HARDWARE_BLUETOOTH_INKBIRD) {
temp = bt_scanner.get_inkbird(hw.btAddress)->getTempFixedPoint();
} else if(hw_type == DEVICE_HARDWARE_BLUETOOTH_TILT) {
temp = bt_scanner.get_tilt(hw.btAddress)->getTempFixedPoint();
}
#endif

tempToString(out, temp, 3, 9);
#else
strcpy_P(out, PSTR("0.00"));
Expand Down Expand Up @@ -852,16 +860,12 @@ void DeviceManager::handleEnumeratedDevice(DeviceConfig config_in, EnumerateHard
if (h.values) {
switch (config.deviceHardware) {
case DEVICE_HARDWARE_ONEWIRE_TEMP:
readTempSensorValue(config.hw, out.value);
break;
#if HAS_BLUETOOTH
case DEVICE_HARDWARE_BLUETOOTH_INKBIRD:
tempToString(out.value, bt_scanner.get_inkbird(config.hw.btAddress)->getTempFixedPoint(), 3, 9);
break;
case DEVICE_HARDWARE_BLUETOOTH_TILT:
tempToString(out.value, bt_scanner.get_tilt(config.hw.btAddress)->getTempFixedPoint(), 3, 9);
break;
#endif
readTempSensorValue(config.deviceHardware, config.hw, out.value);
break;

// unassigned pins could be input or output so we can't determine any
// other details from here. values can be read once the pin has been
Expand Down Expand Up @@ -1044,7 +1048,7 @@ void DeviceManager::enumerateTplinkDevices(EnumerateHardware& h, EnumDevicesCall
/**
* \brief Output devices matching hardware spec passed in
*/
void DeviceManager::enumerateHardware(DynamicJsonDocument& doc, EnumerateHardware spec)
void DeviceManager::enumerateHardware(JsonDocument& doc, EnumerateHardware spec)
{

// Initialize the document as an array
Expand Down Expand Up @@ -1076,7 +1080,7 @@ void DeviceManager::enumerateHardware(DynamicJsonDocument& doc, EnumerateHardwar
/**
* \brief Output devices matching default hardware spec (all devices, no values)
*/
void DeviceManager::enumerateHardware(DynamicJsonDocument& doc)
void DeviceManager::enumerateHardware(JsonDocument& doc)
{
EnumerateHardware spec;
enumerateHardware(doc, spec);
Expand All @@ -1086,7 +1090,7 @@ void DeviceManager::enumerateHardware(DynamicJsonDocument& doc)
* \brief Parse JSON into a DeviceDisplay struct
*/
void DeviceManager::readJsonIntoDeviceDisplay(DeviceDisplay& dev) {
StaticJsonDocument<128> doc;
JsonDocument doc;
piLink.receiveJsonMessage(doc);

JsonVariant id = doc[DeviceDisplayKeys::index];
Expand All @@ -1111,7 +1115,7 @@ void DeviceManager::readJsonIntoDeviceDisplay(DeviceDisplay& dev) {
* \brief Parse JSON into an EnumerateHardware struct
*/
void DeviceManager::readJsonIntoHardwareSpec(EnumerateHardware& hw) {
StaticJsonDocument<128> doc;
JsonDocument doc;
piLink.receiveJsonMessage(doc);

JsonVariant hardware = doc[EnumerateHardwareKeys::hardware];
Expand Down Expand Up @@ -1226,15 +1230,15 @@ void DeviceManager::outputRawDeviceValue(DeviceConfig* config, void* pv, JsonDoc
if(config->deviceHardware == DeviceHardware::DEVICE_HARDWARE_ONEWIRE_TEMP) {
// Read the temp
char str_temp[10];
DeviceManager::readTempSensorValue(config->hw, str_temp);
DeviceManager::readTempSensorValue(config->deviceHardware, config->hw, str_temp);

// Pretty-print the address
char devName[17];
printBytes(config->hw.address, 8, devName);

String humanName = DeviceNameManager::getDeviceName(devName);

JsonObject deviceObj = doc->createNestedObject();
JsonObject deviceObj = doc->add<JsonObject>();
deviceObj["device"] = devName;
deviceObj["value"] = str_temp;
deviceObj["name"] = humanName;
Expand All @@ -1254,7 +1258,7 @@ void DeviceManager::outputRawDeviceValue(DeviceConfig* config, void* pv, JsonDoc
// Pretty-print the address
String humanName = DeviceNameManager::getDeviceName(config->hw.btAddress.toString().c_str());

JsonObject deviceObj = doc->createNestedObject();
JsonObject deviceObj = doc->add<JsonObject>();
deviceObj["device"] = config->hw.btAddress.toString();
deviceObj["value"] = str_temp;
deviceObj["name"] = humanName;
Expand Down
Loading