Skip to content

Commit

Permalink
Merge pull request #82 from medlor/3.2.5_beta
Browse files Browse the repository at this point in the history
3.2.5
  • Loading branch information
medlor authored Jan 23, 2024
2 parents 570b14e + fca3c1b commit 79fe1ff
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Do it yourself, open-source PID for your espresso machine

Version 3.2.4
Version 3.2.5

## Support / Contact
You can chat with us directly using our [discord server](https://discord.gg/VA5ZeacFdw).
Expand Down Expand Up @@ -125,6 +125,10 @@ Installation is as explained on http://rancilio-pid.de/ but with following adapa
- Instructions can be found at https://github.com/medlor/bleeding-edge-ranciliopid/wiki/Instructions-on-how-to-create-new-icon-collections
## Changelog
- 3.2.5:
- Feature: Add local(!) MQTT Broker support on ESP32. Finally you can have MQTT without an external running broker. Many thanks to bviecelli!
- Fix: Upgrade to Expressif v6.5.0
- Some bugfixes on the new broker integration.
- 3.2.4:
- Many more refactorings by aschoelzhorn. Thanks again!
- Seperated wifi, temperature and other code to dedicated files.
Expand Down
Binary file added pictures/hardware-led/perfectcoffeepid_leds.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ lib_deps =
HX711_ADC_fix=https://github.com/medlor/HX711_ADC#1.2.12_pcp_v1

[env:esp32_usb]
platform = espressif32@5.3
platform = espressif32@6.5.0
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
upload_protocol = esptool
build_flags =
-include "rancilio-pid/RemoteDebugCfg.h"
-include "rancilio-pid/scaleConfigOverwrite.h"
Expand All @@ -85,9 +86,10 @@ lib_deps =
adafruit/MAX6675 library @ ^1.1.0
#olkal/HX711_ADC@^1.2.11
HX711_ADC_fix=https://github.com/medlor/HX711_ADC#1.2.12_pcp_v1
mlesniew/PicoMQTT@^0.3.8

[env:esp32_ota_DEV]
platform = espressif32@5.3
platform = espressif32@6.5.0
board = esp32dev
framework = arduino
monitor_speed = 115200
Expand All @@ -110,9 +112,10 @@ lib_deps =
adafruit/MAX6675 library @ ^1.1.0
#olkal/HX711_ADC@^1.2.11
HX711_ADC_fix=https://github.com/medlor/HX711_ADC#1.2.12_pcp_v1
mlesniew/PicoMQTT@^0.3.8

[env:esp32_ota_LIVE]
platform = espressif32@5.3
platform = espressif32@6.5.0
board = esp32dev
framework = arduino
monitor_speed = 115200
Expand All @@ -135,3 +138,4 @@ lib_deps =
adafruit/MAX6675 library @ ^1.1.0
#olkal/HX711_ADC@^1.2.11
HX711_ADC_fix=https://github.com/medlor/HX711_ADC#1.2.12_pcp_v1
mlesniew/PicoMQTT@^0.3.8
12 changes: 11 additions & 1 deletion rancilio-pid/MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
#if (MQTT_ENABLE == 1)
#include <PubSubClient.h>
extern PubSubClient mqttClient;
#elif (MQTT_ENABLE == 2 && defined(ESP8266))
#elif MQTT_ENABLE == 2
#if defined(ESP8266)
#include <uMQTTBroker.h>
//bool MQTT_local_publish(char* topic, char* data, size_t data_length, uint8_t qos, uint8_t retain);
#elif defined(ESP32)
#include <PicoMQTT.h>
class MyPicoMQTTBroker: public PicoMQTT::Server {
protected:
virtual void on_connected(const char * client_id) override;
};
extern MyPicoMQTTBroker picoMQTTBroker;
#endif
#endif

bool isWifiWorking();
Expand All @@ -21,6 +30,7 @@ bool mqttPublish(char*, char*);
bool isMqttWorking();
bool isMqttWorking(bool refresh);
void mqttPublishSettings();
void mqttPublishPersistedSettings();
bool persistSetting(char*, float*, char*);
bool persistSetting(char*, int*, char*);
bool persistSetting(char*, unsigned int*, char*);
Expand Down
64 changes: 58 additions & 6 deletions rancilio-pid/MQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ char topicWill[256];
char topicSet[256];
char topicActions[256];
const bool mqttFlagRetained = true;
unsigned long publishSettingsAfterClientConnect = 0;
unsigned long mqttDontPublishUntilTime = 0;
unsigned long mqttDontPublishBackoffTime = 15000; // Failsafe: dont publish if there are errors for 15 seconds
unsigned long mqttLastReconnectAttemptTime = 0;
Expand Down Expand Up @@ -58,8 +59,18 @@ extern void blynkSave(char*);
WiFiClient espClient;
#include <PubSubClient.h>
PubSubClient mqttClient(espClient);
#elif (MQTT_ENABLE == 2 && defined(ESP8266))
#elif MQTT_ENABLE == 2
#if defined(ESP8266)
#include <uMQTTBroker.h>
#elif defined(ESP32)
#include <PicoMQTT.h>
void MyPicoMQTTBroker::on_connected(const char * client_id) {
//Serial.printf("Client %s connected.\n", client_id);
publishSettingsAfterClientConnect = millis();
}
MyPicoMQTTBroker picoMQTTBroker{};
#endif

#endif

bool almostEqual_exact(float a, float b) { return fabs(a - b) <= FLT_EPSILON; }
Expand Down Expand Up @@ -203,7 +214,12 @@ bool mqttPublish(char* reading, char* payload) {
} else {
unsigned long currentMillis = millis();
if (currentMillis > mqttDontPublishUntilTime) {
#if defined(ESP32)
bool ret = picoMQTTBroker.publish(topic, payload);
mqtt_callback_2(nullptr, topic, strlen(topic), payload, strlen(payload));
#else
bool ret = MQTT_local_publish((unsigned char*)&topic, (unsigned char*)payload, strlen(payload), 1, 1);
#endif
if (ret == false) {
mqttDontPublishUntilTime = millis() + mqttDontPublishBackoffTime;
ERROR_print("Error on publish. Wont publish the next %lu ms\n", mqttDontPublishBackoffTime);
Expand All @@ -227,7 +243,7 @@ void mqtt_callback_2(uint32_t* client, const char* topic, uint32_t topic_len, co
char data_str[length + 1];
os_memcpy(data_str, data, length);
data_str[length] = '\0';
// DEBUG_print("MQTT: %s = %s\n", topic_str, data_str);
//DEBUG_print("MQTT: %s = %s\n", topic_str, data_str);
if (strstr(topic_str, "/actions/") != NULL) {
mqttParseActions(topic_str, data_str);
} else {
Expand All @@ -241,9 +257,9 @@ void mqttParseActions(char* topic_str, char* data_str) {
char topic_pattern[255];
char actionParsed[120];
char* endptr;
// DEBUG_print("mqttParseActions(%s, %s)\n", topic_str, data_str);
//DEBUG_print("mqttParseActions(%s, %s)\n", topic_str, data_str);
snprintf(topic_pattern, sizeof(topic_pattern), "%s%s/actions/%%[^\\/]", mqttTopicPrefix, hostname);
// DEBUG_print("topic_pattern=%s\n",topic_pattern);
//DEBUG_print("topic_pattern=%s\n",topic_pattern);
if (sscanf(topic_str, topic_pattern, &actionParsed) != 1) {
DEBUG_print("Ignoring un-parsable topic (%s)\n", topic_str);
return;
Expand All @@ -269,7 +285,7 @@ void mqttParse(char* topic_str, char* data_str) {
//DEBUG_print("mqttParse(%s, %s)\n", topic_str, data_str);
snprintf(topic_pattern, sizeof(topic_pattern), "%s%s/%%[^\\/]/%%[^\\/]", mqttTopicPrefix, hostname);
if ((sscanf(topic_str, topic_pattern, &configVar, &cmd) != 2) || (strcmp(cmd, "set") != 0)) {
// DEBUG_print("Ignoring topic (%s)\n", topic_str);
//DEBUG_print("Ignoring topic (%s)\n", topic_str);
return;
}
if (strcmp(configVar, "profile") == 0) {
Expand Down Expand Up @@ -425,7 +441,7 @@ bool persistSetting(char* setting, int* value, char* data_str) {
int data_int;
sscanf(data_str, "%d", &data_int);
if (data_int != *value) {
// DEBUG_print("setting %s=%s (=%d) (prev=%d)\n", setting, data_str, data_int, *value);
//DEBUG_print("setting %s=%s (=%d) (prev=%d)\n", setting, data_str, data_int, *value);
*value = data_int;
mqttPublish(setting, data_str);
eepromForceSync = millis();
Expand Down Expand Up @@ -470,6 +486,32 @@ void mqttPublishSettings() {
mqttPublish((char*)"steadyPower/set", number2string(steadyPower)); // this should be last in list
}

void mqttPublishPersistedSettings() {
mqttPublish((char*)"profile", number2string(profile));
mqttPublish((char*)"activeBrewTime", number2string(*activeBrewTime));
mqttPublish((char*)"activeStartTemp", number2string(*activeStartTemp));
mqttPublish((char*)"activeSetPoint", number2string(*activeSetPoint));
mqttPublish((char*)"activePreinfusion", number2string(*activePreinfusion));
mqttPublish((char*)"activePreinfusionPause", number2string(*activePreinfusionPause));
mqttPublish((char*)"pidON", number2string(pidON));
mqttPublish((char*)"brewDetectionSensitivity", number2string(brewDetectionSensitivity));
mqttPublish((char*)"brewDetectionPower", number2string(brewDetectionPower));
mqttPublish((char*)"aggKp", number2string(aggKp));
mqttPublish((char*)"aggTn", number2string(aggTn));
mqttPublish((char*)"aggTv", number2string(aggTv));
mqttPublish((char*)"aggoKp", number2string(aggoKp));
mqttPublish((char*)"aggoTn", number2string(aggoTn));
mqttPublish((char*)"aggoTv", number2string(aggoTv));
mqttPublish((char*)"setPointSteam", number2string(setPointSteam));
mqttPublish((char*)"steadyPowerOffset", number2string(steadyPowerOffset));
mqttPublish((char*)"steadyPowerOffsetTime", number2string(steadyPowerOffsetTime));
mqttPublish((char*)"activeBrewTimeEndDetection", number2string(*activeBrewTimeEndDetection));
mqttPublish((char*)"activeScaleSensorWeightSetPoint", number2string(*activeScaleSensorWeightSetPoint));
mqttPublish((char*)"steadyPower", number2string(steadyPower)); // this should be last in list
mqttPublish((char*)"steadyPower", number2string(steadyPower));
publishActions();
}

// Setup Mqtt (and also call initBlynk()) returns eeprom_force_read
bool InitMqtt() {
bool eeprom_force_read = true;
Expand Down Expand Up @@ -507,6 +549,15 @@ bool InitMqtt() {
const unsigned int mqtt_service_port = 1883;
snprintf(topicSet, sizeof(topicSet), "%s%s/+/%s", mqttTopicPrefix, hostname, "set");
snprintf(topicActions, sizeof(topicActions), "%s%s/actions/+", mqttTopicPrefix, hostname);
#if defined(ESP32)
auto callback = [](const char * topic, const char * payload) {
mqtt_callback_2(nullptr, topic, strlen(topic), payload, strlen(payload));
};
picoMQTTBroker.subscribe(topicSet, callback);
picoMQTTBroker.subscribe(topicActions, callback);
picoMQTTBroker.begin();
picoMQTTBroker.loop();
#else
MQTT_server_onData(mqtt_callback_2);
if (MQTT_server_start(mqtt_service_port, max_subscriptions, max_retained_topics)) {
if (!MQTT_local_subscribe((unsigned char*)topicSet, 0) || !MQTT_local_subscribe((unsigned char*)topicActions, 0)) {
Expand All @@ -518,6 +569,7 @@ bool InitMqtt() {
// displaymessage(State::Undefined, "Cannot create MQTT service", "");
// delay(1000);
}
#endif
#endif

return InitBlynk() && eeprom_force_read;
Expand Down
1 change: 1 addition & 0 deletions rancilio-pid/controls.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void brewingAction(int state);
void menuAction(int state);
void menuIncAction(int state);
void menuDecAction(int state);
void publishActions();

extern int simulatedBrewSwitch;

Expand Down
6 changes: 6 additions & 0 deletions rancilio-pid/rancilio-network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ network-issues with your other WiFi-devices on your WiFi-network. */
if (isWifiWorking()) {
DEBUG_print("Wifi connection attempt (#%u) successfull (%lu secs)\n", wifiReconnects, (millis() - lastWifiConnectionAttempt) / 1000);
wifiReconnects = 0;
#if (MQTT_ENABLE == 2)
#ifdef ESP32
picoMQTTBroker.stop();
#endif
#endif
InitMqtt();
} else {
ERROR_print("Wifi connection attempt (#%u) not successfull (%lu secs)\n", wifiReconnects, (millis() - lastWifiConnectionAttempt) / 1000);
wifiReconnects++;
Expand Down
5 changes: 2 additions & 3 deletions rancilio-pid/rancilio-pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
// ESP32 sometimes (after crash) is not able to connect to wifi. Workaround: set DISABLE_SERVICES_ON_STARTUP_ERRORS to 0
#undef DISABLE_SERVICES_ON_STARTUP_ERRORS
#define DISABLE_SERVICES_ON_STARTUP_ERRORS 0
#if (MQTT_ENABLE == 2)
#error ERROR Not supported to set MQTT_ENABLE=2 on ESP32
#endif

#endif

Expand Down Expand Up @@ -161,4 +158,6 @@ extern float scaleSensorWeightOffset;

extern TemperatureSensor tempSensor;

extern unsigned long publishSettingsAfterClientConnect;

#endif
17 changes: 15 additions & 2 deletions rancilio-pid/rancilio-pid.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "controls.h"
#include "PIDBias.h"

const char* sysVersion PROGMEM = "3.2.4";
const char* sysVersion PROGMEM = "3.2.5";

/********************************************************
* definitions below must be changed in the userConfig.h file
Expand Down Expand Up @@ -1010,12 +1010,23 @@ void CheckMqttConnection() {
mqttPublish((char*)"powerOffTimer", int2string(powerOffTimer >= 0 ? ((powerOffTimer + 59) / 60) : 0)); // in minutes always rounded up
}
// mqttPublishSettings(); //not needed because we update live on occurence
if (publishSettingsAfterClientConnect != 0 && millis() - publishSettingsAfterClientConnect >= 2000) { //delay is needed else messages are dropped
publishSettingsAfterClientConnect = 0;
mqttPublishPersistedSettings();
}
}
#if (MQTT_ENABLE == 1)
if (millis() >= previousTimerMqttHandle + 100) {
previousTimerMqttHandle = millis();
mqttClient.loop(); // mqtt client connected, do mqtt housekeeping
}
#elif (MQTT_ENABLE == 2)
#if defined(ESP32)
if (millis() >= previousTimerMqttHandle + 1000) {
previousTimerMqttHandle = millis();
picoMQTTBroker.loop(); // mqtt client connected, do mqtt housekeeping
}
#endif
#endif
}
}
Expand Down Expand Up @@ -1057,7 +1068,9 @@ void CheckMqttConnection() {
if (!forceOffline) {
if (!isWifiWorking()) {
#if (MQTT_ENABLE == 2)
MQTT_server_cleanupClientCons();
#if defined(ESP8266)
MQTT_server_cleanupClientCons();
#endif
#endif
checkWifi(inSensitivePhase());
} else {
Expand Down
2 changes: 1 addition & 1 deletion rancilio-pid/userConfig.h.SAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
#define BLYNKPORT 8080

// MQTT
#define MQTT_ENABLE 1 // 0 = off (no MQTT at all) | 1=MQTT client | 2=MQTT Server if you dont have a mqtt server running on another system)
#define MQTT_ENABLE 2 // 0 = off (no MQTT at all) | 1=MQTT client | 2=MQTT Server if you dont have a mqtt server running on another system)
#define MQTT_USERNAME "myuser"
#define MQTT_PASSWORD "mypass"
#define MQTT_TOPIC_PREFIX "custom/Küche." // topic will be "<MQTT_TOPIC_PREFIX><HOSTNAME>/<READING>" <HOSTNAME> is defined in //Wifi section
Expand Down

0 comments on commit 79fe1ff

Please sign in to comment.