Skip to content
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

Publish retry #696

Draft
wants to merge 6 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 library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ lib_deps =
adafruit/Adafruit VEML7700 Library
adafruit/Adafruit LC709203F
adafruit/Adafruit LPS2X
adafruit/Adafruit LPS28
adafruit/Adafruit LPS35HW
adafruit/Adafruit seesaw Library
adafruit/Adafruit BME680 Library
Expand All @@ -94,7 +95,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.13/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
34 changes: 27 additions & 7 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,7 +995,7 @@ 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,
WS.publish(WS._topic_signal_servo_device, WS._buffer_outgoing, msgSz,
1);
WS_DEBUG_PRINTLN("Published!");
} else if (field->tag ==
Expand Down Expand Up @@ -1161,7 +1161,7 @@ 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;
Expand Down Expand Up @@ -1571,7 +1571,7 @@ 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;
Expand Down Expand Up @@ -2364,7 +2364,7 @@ void Wippersnapper::runNetFSM() {
while (fsmNetwork != FSM_NET_CONNECTED) {
switch (fsmNetwork) {
case FSM_NET_CHECK_MQTT:
if (WS._mqtt->connected()) {
if (WS._mqtt->connected() && networkStatus() == WS_NET_CONNECTED) {
// WS_DEBUG_PRINTLN("Connected to Adafruit IO!");
fsmNetwork = FSM_NET_CONNECTED;
return;
Expand Down Expand Up @@ -2653,14 +2653,34 @@ 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.
WS.feedWDT();
if (!WS._mqtt->publish(topic, payload, bLen, qos)) {
WS_DEBUG_PRINTLN("Failed to publish MQTT message!");
WS_DEBUG_PRINTLN("FAILED!");
WS_DEBUG_PRINT("Mqtt connected: ");
WS_DEBUG_PRINTLN(WS._mqtt->connected());
WS_DEBUG_PRINT("Network status: ");
WS_DEBUG_PRINTLN(networkStatus());
if (WS._mqtt->connected() && networkStatus() == WS_NET_CONNECTED) {
WS_DEBUG_PRINTLN("Failed to publish MQTT message, retrying!");
} else {
WS_DEBUG_PRINTLN("MQTT connection broken! Running network FSM then publish...");
WS._mqtt->disconnect();
WS_DEBUG_PRINTLN("MQTT forcibly disconnected. Running Network FSM...");
runNetFSM();
}
WS.feedWDT();
WS_DEBUG_PRINTLN("Retrying publish...");
if (!WS._mqtt->publish(topic, payload, bLen, qos)) {
WS_DEBUG_PRINTLN("Failed to publish MQTT message!");
return false;
}
WS_DEBUG_PRINTLN("MQTT message published successfully!");
}
return true;
}

/**************************************************************/
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: 2 additions & 3 deletions src/components/ds18x20/ws_ds18x20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ 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.publish(WS._topic_signal_ds18_device, WS._buffer_outgoing, msgSz, 1);
WS_DEBUG_PRINTLN("Published!");

return is_success;
Expand Down Expand Up @@ -294,7 +293,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
13 changes: 12 additions & 1 deletion src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_lps25hb->configureDriver(msgDeviceInitReq);
drivers.push_back(_lps25hb);
WS_DEBUG_PRINTLN("LPS25HB Sensor Initialized Successfully!");
} else if (strcmp("lps28dfw", msgDeviceInitReq->i2c_device_name) == 0) {
_lps28hb = new WipperSnapper_I2C_Driver_LPS28DFW(this->_i2c, i2cAddress);
if (!_lps28hb->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize LPS28DFW Sensor!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_lps28hb->configureDriver(msgDeviceInitReq);
drivers.push_back(_lps28hb);
WS_DEBUG_PRINTLN("LPS28HB Sensor Initialized Successfully!");
} else if ((strcmp("lps33hw", msgDeviceInitReq->i2c_device_name) == 0) ||
(strcmp("lps35hw", msgDeviceInitReq->i2c_device_name)) == 0) {
_lps3xhw = new WipperSnapper_I2C_Driver_LPS3XHW(this->_i2c, i2cAddress);
Expand Down Expand Up @@ -910,7 +921,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
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS25HB.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS28DFW.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS3XHW.h"
#include "drivers/WipperSnapper_I2C_Driver_LTR329_LTR303.h"
#include "drivers/WipperSnapper_I2C_Driver_LTR390.h"
Expand Down Expand Up @@ -177,6 +178,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_LC709203F *_lc = nullptr;
WipperSnapper_I2C_Driver_LPS22HB *_lps22hb = nullptr;
WipperSnapper_I2C_Driver_LPS25HB *_lps25hb = nullptr;
WipperSnapper_I2C_Driver_LPS28DFW *_lps28hb = nullptr;
WipperSnapper_I2C_Driver_LPS3XHW *_lps3xhw = nullptr;
WipperSnapper_I2C_Driver_STEMMA_Soil_Sensor *_ss = nullptr;
WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr;
Expand Down
147 changes: 147 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_LPS28DFW.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*!
* @file WipperSnapper_I2C_Driver_LPS28DFW.h
*
* Device driver for a LPS28DFW precision pressure sensor breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_LPS28DFW_H
#define WipperSnapper_I2C_Driver_LPS28DFW_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_LPS28.h>

/**************************************************************************/
/*!
@brief Class that provides a sensor driver for the LPS28DFW temperature
and pressure sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_LPS28DFW : public WipperSnapper_I2C_Driver {

public:
/*******************************************************************************/
/*!
@brief Constructor for an LPS28DFW sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_LPS28DFW(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an LPS28DFW sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_LPS28DFW() { delete _lps28; }

/*******************************************************************************/
/*!
@brief Initializes the LPS28DFW sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_lps28 = new Adafruit_LPS28();
// attempt to initialize LPS28DFW
if (!_lps28->begin(_i2c, _sensorAddress))
return false;

// Set up sample rate and filter initialization
if (!_lps28->setDataRate(LPS28_ODR_ONESHOT)) {
WS_DEBUG_PRINTLN("Failed to set data rate");
return false;
}
if (!_lps28->setAveraging(LPS28_AVG_512)) {
WS_DEBUG_PRINTLN("Failed to set averaging");
return false;
}

return true;
}

/*******************************************************************************/
/*!
@brief Reads the sensor and stores the data in the object.
@returns True if the sensor was read successfully, False otherwise.
*/
/*******************************************************************************/
bool readSensor() {
// grab one reading to seed the sensor
if (!_lps28->triggerOneShot()) {
return false;
}

// Wait (block up to 100ms) until data is ready
for (uint8_t i = 0; i < 100; i++) {
if (_lps28->getStatus() & LPS28_STATUS_PRESS_READY) {
if (_temp == NULL) {
_temp = _lps28->getTemperatureSensor();
}
if (_pressure == NULL) {
_pressure = _lps28->getPressureSensor();
}
return true;
}
delay(1);
}
return false;
}

/*******************************************************************************/
/*!
@brief Gets the LPS28DFW's current temperature.
@param tempEvent
Pointer to an Adafruit_Sensor event.
@returns True if the temperature was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
if (!readSensor())
return false;
_temp->getEvent(tempEvent);
return true;
}

/*******************************************************************************/
/*!
@brief Reads a pressure sensor and converts
the reading into the expected SI unit.
@param pressureEvent
Pointer to an Adafruit_Sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventPressure(sensors_event_t *pressureEvent) {
if (!readSensor())
return false;
_pressure->getEvent(pressureEvent);
return true;
}

protected:
Adafruit_LPS28 *_lps28; ///< LPS28DFW object
Adafruit_Sensor *_temp =
NULL; ///< Ptr to an adafruit_sensor representing the temperature
Adafruit_Sensor *_pressure =
NULL; ///< Ptr to an adafruit_sensor representing the pressure
};

#endif // WipperSnapper_I2C_Driver_LPS28DFW
2 changes: 1 addition & 1 deletion src/components/pixels/ws_pixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ 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,
WS.publish(WS._topic_signal_pixels_device, WS._buffer_outgoing, msgSz,
1);
WS_DEBUG_PRINTLN("Published!");
}
Expand Down
42 changes: 42 additions & 0 deletions src/nanopb/ws_pb_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@
// *****************************************************************************
bool ws_pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields,
void *dest_struct) {
if(!stream || !fields || !dest_struct) {
WS_DEBUG_PRINTLN("Protobuf decode error: Invalid arguments to function");
if (!stream) {
WS_DEBUG_PRINTLN("stream is NULL");
} else if (stream == nullptr) {
WS_DEBUG_PRINTLN("stream is nullptr");
}

if (!fields) {
WS_DEBUG_PRINTLN("fields is NULL");
} else if (fields == nullptr) {
WS_DEBUG_PRINTLN("fields is nullptr");
}

if (!dest_struct) {
WS_DEBUG_PRINTLN("dest_struct is NULL");
} else if (dest_struct == nullptr) {
WS_DEBUG_PRINTLN("dest_struct is nullptr");
}
return false;
}
bool status = pb_decode(stream, fields, dest_struct);
if (!status) {
WS_DEBUG_PRINT("Protobuf decode error: ");
Expand All @@ -52,6 +73,27 @@ bool ws_pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields,
// *****************************************************************************
bool ws_pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields,
const void *src_struct) {
if(!stream || !fields || !src_struct) {
WS_DEBUG_PRINTLN("Protobuf encode error: Invalid arguments to function");
if (!stream) {
WS_DEBUG_PRINTLN("stream is NULL");
} else if (stream == nullptr) {
WS_DEBUG_PRINTLN("stream is nullptr");
}

if (!fields) {
WS_DEBUG_PRINTLN("fields is NULL");
} else if (fields == nullptr) {
WS_DEBUG_PRINTLN("fields is nullptr");
}

if (!src_struct) {
WS_DEBUG_PRINTLN("src_struct is NULL");
} else if (src_struct == nullptr) {
WS_DEBUG_PRINTLN("src_struct is nullptr");
}
return false;
}
bool status = pb_encode(stream, fields, src_struct);
if (!status) {
WS_DEBUG_PRINT("Protobuf encode error: ");
Expand Down
Loading