Skip to content

DRAFT: Publish returns bool + print info on publishes #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ lib_deps =

; Common build environment for ESP32 platform
[common:esp32]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.11/platform-espressif32.zip
; This is needed for occasional new features and bug fixes
; platform = https://github.com/pioarduino/platform-espressif32#develop
lib_ignore = WiFiNINA, WiFi101, OneWire
Expand Down
66 changes: 53 additions & 13 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void publishI2CResponse(wippersnapper_signal_v1_I2CResponse *msgi2cResponse) {
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields,
msgi2cResponse);
WS_DEBUG_PRINT("Publishing Message: I2CResponse...");
if (!WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
if (!WS.publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Failed to publish I2C Response!");
} else {
Expand Down Expand Up @@ -995,9 +995,16 @@ bool cbDecodeServoMsg(pb_istream_t *stream, const pb_field_t *field,
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_ServoResponse_fields,
&msgServoResp);
WS_DEBUG_PRINT("-> Servo Attach Response...");
WS._mqtt->publish(WS._topic_signal_servo_device, WS._buffer_outgoing, msgSz,
1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_signal_servo_device, WS._buffer_outgoing, msgSz,
1))
{
WS_DEBUG_PRINTLN("ERROR: Failed to publish Servo Attach Response!");
return false;
}
else
{
WS_DEBUG_PRINTLN("Published!");
}
} else if (field->tag ==
wippersnapper_signal_v1_ServoRequest_servo_write_tag) {
WS_DEBUG_PRINTLN("GOT: Servo Write");
Expand Down Expand Up @@ -1161,12 +1168,12 @@ bool cbPWMDecodeMsg(pb_istream_t *stream, const pb_field_t *field, void **arg) {
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_PWMResponse_fields,
&msgPWMResponse);
WS_DEBUG_PRINT("PUBLISHING: PWM Attach Response...");
if (!WS._mqtt->publish(WS._topic_signal_pwm_device, WS._buffer_outgoing,
if (!WS.publish(WS._topic_signal_pwm_device, WS._buffer_outgoing,
msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Failed to publish PWM Attach Response!");
return false;
}
WS_DEBUG_PRINTLN("Published!");
WS_DEBUG_PRINTLN("Published! (PWM Attach Response)");

#ifdef USE_DISPLAY
char buffer[100];
Expand Down Expand Up @@ -1571,12 +1578,12 @@ bool cbDecodeUARTMessage(pb_istream_t *stream, const pb_field_t *field,
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_UARTResponse_fields,
&msgUARTResponse);
WS_DEBUG_PRINT("PUBLISHING: UART Attach Response...");
if (!WS._mqtt->publish(WS._topic_signal_uart_device, WS._buffer_outgoing,
if (!WS.publish(WS._topic_signal_uart_device, WS._buffer_outgoing,
msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Failed to publish UART Attach Response!");
return false;
}
WS_DEBUG_PRINTLN("Published!");
WS_DEBUG_PRINTLN("Published! (UART Device Attached)");

} else if (field->tag ==
wippersnapper_signal_v1_UARTRequest_req_uart_device_detach_tag) {
Expand Down Expand Up @@ -2653,14 +2660,40 @@ void Wippersnapper::processPackets() {
The Quality of Service to publish with.
*/
/*******************************************************/
void Wippersnapper::publish(const char *topic, uint8_t *payload, uint16_t bLen,
bool Wippersnapper::publish(const char *topic, uint8_t *payload, uint16_t bLen,
uint8_t qos) {
// runNetFSM(); // NOTE: Removed for now, causes error with virtual _connect
// method when caused with WS object in another file.
#ifdef ARDUINO_ARCH_ESP32
// print stack and heap usage
WS_DEBUG_PRINT("\nFree Heap: ");
WS_DEBUG_PRINTLN(ESP.getFreeHeap());
WS_DEBUG_PRINT("Min Free Heap: ");
WS_DEBUG_PRINTLN(ESP.getMinFreeHeap());
WS_DEBUG_PRINT("Max Alloc Heap: ");
WS_DEBUG_PRINTLN(ESP.getMaxAllocHeap());
WS_DEBUG_PRINT("Heap Size: ");
WS_DEBUG_PRINTLN(ESP.getHeapSize());
WS_DEBUG_PRINT("Free Stack: ");
WS_DEBUG_PRINTLN(uxTaskGetStackHighWaterMark(NULL));
#endif
WS.feedWDT();
if (!WS._mqtt->publish(topic, payload, bLen, qos)) {
WS_DEBUG_PRINTLN("Failed to publish MQTT message!");
bool response = WS._mqtt->publish(topic, payload, bLen, qos);
if (!response) {
WS_DEBUG_PRINTLN("Failed to publish MQTT message (t: ");
WS_DEBUG_PRINT(topic);
WS_DEBUG_PRINT(" q: ");
WS_DEBUG_PRINT(qos);
WS_DEBUG_PRINT(" l: ");
WS_DEBUG_PRINT(bLen);
WS_DEBUG_PRINT(" hex: ");
for (uint16_t i = 0; i < bLen; i++) {
WS_DEBUG_PRINTHEX(payload[i]);
WS_DEBUG_PRINT(" ");
}
WS_DEBUG_PRINTLN(")\n");
}
return response;
}

/**************************************************************/
Expand Down Expand Up @@ -2861,8 +2894,15 @@ void Wippersnapper::publishPinConfigComplete() {

// Publish message
WS_DEBUG_PRINTLN("Publishing to pin config complete...");
WS.publish(WS._topic_device_pin_config_complete, _message_buffer,
_message_len, 1);
if (WS.publish(WS._topic_device_pin_config_complete, _message_buffer,
_message_len, 1))
{
WS_DEBUG_PRINTLN("Published! (pin config complete)");
}
else
{
WS_DEBUG_PRINTLN("Failed to publish! (pin config complete)");
}
}

/**************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class Wippersnapper {
// run() loop
ws_status_t run();
void processPackets();
void publish(const char *topic, uint8_t *payload, uint16_t bLen,
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
uint8_t qos = 0);

// Networking helpers
Expand Down
5 changes: 4 additions & 1 deletion src/components/analogIO/Wippersnapper_AnalogIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ bool Wippersnapper_AnalogIO::encodePinEvent(
wippersnapper_signal_v1_CreateSignalRequest_fields,
&outgoingSignalMsg);
WS_DEBUG_PRINT("Publishing pinEvent...");
WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1);
if(!WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Unable to publish analogIO pin event message");
return false;
}
WS_DEBUG_PRINTLN("Published!");

return true;
Expand Down
17 changes: 13 additions & 4 deletions src/components/digitalIO/Wippersnapper_DigitalGPIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,11 @@ void Wippersnapper_DigitalGPIO::processDigitalInputs() {
&_outgoingSignalMsg);

WS_DEBUG_PRINT("Publishing pinEvent...");
WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1)){
WS_DEBUG_PRINTLN("ERROR: Unable to publish DigitalIO pinEvent");
} else {
WS_DEBUG_PRINTLN("Published! (DigitalIO PinEvent)");
}

// reset the digital pin
_digital_input_pins[i].prvPeriod = curTime;
Expand Down Expand Up @@ -315,8 +318,14 @@ void Wippersnapper_DigitalGPIO::processDigitalInputs() {
&msgSz, wippersnapper_signal_v1_CreateSignalRequest_fields,
&_outgoingSignalMsg);
WS_DEBUG_PRINT("Publishing pinEvent...");
WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_signal_device, WS._buffer_outgoing, msgSz, 1))
{
WS_DEBUG_PRINTLN("ERROR: Unable to publish DigitalIO pinEvent");
}
else
{
WS_DEBUG_PRINTLN("Published! (DigitalIO PinEvent)");
}

// set the pin value in the digital pin object for comparison on next
// run
Expand Down
14 changes: 10 additions & 4 deletions src/components/ds18x20/ws_ds18x20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,15 @@ bool ws_ds18x20::addDS18x20(
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_Ds18x20Response_fields,
&msgInitResp);
WS_DEBUG_PRINT("-> DS18x Init Response...");
WS._mqtt->publish(WS._topic_signal_ds18_device, WS._buffer_outgoing, msgSz,
1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_signal_ds18_device, WS._buffer_outgoing, msgSz,
1))
{
WS_DEBUG_PRINTLN("ERROR: Unable to publish DS18x20 init response message - check MQTT connection!");
}
else
{
WS_DEBUG_PRINTLN("Published! (Ds18x20 Init Response)");
}

return is_success;
}
Expand Down Expand Up @@ -294,7 +300,7 @@ void ws_ds18x20::update() {
wippersnapper_signal_v1_Ds18x20Response_fields,
&msgDS18x20Response);
WS_DEBUG_PRINT("PUBLISHING -> msgDS18x20Response Event Message...");
if (!WS._mqtt->publish(WS._topic_signal_ds18_device,
if (!WS.publish(WS._topic_signal_ds18_device,
WS._buffer_outgoing, msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: Unable to publish DS18x20 event message - "
"MQTT Publish failed!");
Expand Down
2 changes: 1 addition & 1 deletion src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ bool WipperSnapper_Component_I2C::encodePublishI2CDeviceEventMsg(
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_I2CResponse_fields,
msgi2cResponse);
WS_DEBUG_PRINT("PUBLISHING -> I2C Device Sensor Event Message...");
if (!WS._mqtt->publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
if (!WS.publish(WS._topic_signal_i2c_device, WS._buffer_outgoing,
msgSz, 1)) {
WS_DEBUG_PRINTLN("ERROR: MQTT Publish failed!");
return false;
Expand Down
13 changes: 10 additions & 3 deletions src/components/pixels/ws_pixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,16 @@ void ws_pixels::publishAddStrandResponse(bool is_success,
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_PixelsResponse_fields,
&msgInitResp);
WS_DEBUG_PRINT("-> wippersnapper_signal_v1_PixelsResponse...");
WS._mqtt->publish(WS._topic_signal_pixels_device, WS._buffer_outgoing, msgSz,
1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_signal_pixels_device, WS._buffer_outgoing, msgSz,
1))
{
WS_DEBUG_PRINTLN("ERROR: Unable to publish PixelsResponse message - "
"check MQTT connection!");
}
else
{
WS_DEBUG_PRINTLN("Published! (PixelsResponse)");
}
}

/**************************************************************************/
Expand Down
20 changes: 14 additions & 6 deletions src/components/register/Wippersnapper_Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ bool Wippersnapper::encodePubRegistrationReq() {
return _status;

// pubish message
WS.publish(WS._topic_description, _message_buffer, _message_len, 1);
WS_DEBUG_PRINTLN("Published!");
if (!WS.publish(WS._topic_description, _message_buffer, _message_len, 1)){
WS_DEBUG_PRINTLN("ERROR: Unable to publish registration message");
} else {
WS_DEBUG_PRINTLN("Published! (Registration)");
}
WS._boardStatus = WS_BOARD_DEF_SENT;

return _status;
Expand Down Expand Up @@ -153,10 +156,15 @@ void Wippersnapper::decodeRegistrationResp(char *data, uint16_t len) {
}

// Publish message
WS.publish(_topic_description_status_complete, _message_buffer,
_message_len, 1);
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");

if (WS.publish(_topic_description_status_complete, _message_buffer,
_message_len, 1))
{
WS_DEBUG_PRINTLN("Completed registration process, configuration next!");
}
else
{
WS_DEBUG_PRINTLN("ERROR: Unable to publish registration complete message");
}
} else {
WS._boardStatus = WS_BOARD_DEF_INVALID;
}
Expand Down
Loading