Skip to content

Commit

Permalink
add message encoding, publishing, new data_available getter
Browse files Browse the repository at this point in the history
  • Loading branch information
brentru committed Aug 11, 2023
1 parent 45dc6f8 commit 2e54ee8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 25 deletions.
26 changes: 26 additions & 0 deletions src/Wippersnapper_demo.ino.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmpvfzcm2ti"
#include <Arduino.h>
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
# 16 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
#include "Wippersnapper_Networking.h"
Wippersnapper_WiFi wipper;


#define WS_DEBUG
void setup();
void loop();
#line 22 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
void setup() {

wipper.provision();

Serial.begin(115200);


wipper.connect();

}

void loop() {
wipper.run();
}
5 changes: 4 additions & 1 deletion src/components/uart/drivers/ws_uart_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class ws_uart_drv {
/*******************************************************************************/
~ws_uart_drv(void){};

void set_mqtt_client(Adafruit_MQTT *_mqtt) {_mqttClient = _mqtt;}
void set_mqtt_client(Adafruit_MQTT *_mqtt) { _mqttClient = _mqtt; }

bool data_available() { return false; }

// TODO:
// can we just call an update() here or something and then in uart's update()
Expand All @@ -57,6 +59,7 @@ class ws_uart_drv {
// types wed be polling, the protos would need an update
int32_t pollingInterval; ///< UART device's polling interval, in milliseconds
Adafruit_MQTT *_mqttClient;

private:
};

Expand Down
80 changes: 56 additions & 24 deletions src/components/uart/drivers/ws_uart_drv_pm25aqi.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,80 @@ class ws_uart_drv_pm25aqi : public ws_uart_drv {
return false;
}
#endif
// Serial.println(WS.bufSize);
return true;
}

void update() {
// Attempt to read and pack PM25AQI data
bool data_available() {
if (!_aqi->read(&_data)) {
Serial.println("Could not read AQI...");
return;
Serial.println("[UART, PM25] Data not available.");
return false;
}
// TODO: Make this printout more verbose, showing the units
Serial.println("[UART, PM25] Got Data");
Serial.println("[UART, PM25] Read data OK");
return true;
}

void update() {
// TODO: Print out the results from last read

// Create a new UART response message
wippersnapper_signal_v1_UARTResponse msgUARTResponse = wippersnapper_signal_v1_UARTResponse_init_zero;
msgUARTResponse.which_payload = wippersnapper_signal_v1_UARTResponse_resp_uart_device_event_tag;
// We'll be sending back six sensor_events: pm10_standard, pm25_standard, pm100_standard, pm10_env, pm25_env, and pm100_env
wippersnapper_signal_v1_UARTResponse msgUARTResponse =
wippersnapper_signal_v1_UARTResponse_init_zero;
msgUARTResponse.which_payload =
wippersnapper_signal_v1_UARTResponse_resp_uart_device_event_tag;
// We'll be sending back six sensor_events: pm10_standard, pm25_standard,
// pm100_standard, pm10_env, pm25_env, and pm100_env
msgUARTResponse.payload.resp_uart_device_event.sensor_event_count = 6;


// Pack all _data into `device_event` fields
// pm10_std
msgUARTResponse.payload.resp_uart_device_event.sensor_event[0].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[0].value = (float) _data.pm10_standard;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[0].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[0].value =
(float)_data.pm10_standard;
// pm25_std
msgUARTResponse.payload.resp_uart_device_event.sensor_event[1].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[1].value = (float) _data.pm25_standard;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[1].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[1].value =
(float)_data.pm25_standard;
// pm100_std
msgUARTResponse.payload.resp_uart_device_event.sensor_event[2].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[2].value = (float) _data.pm100_standard;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[2].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_STD;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[2].value =
(float)_data.pm100_standard;
// pm10_env
msgUARTResponse.payload.resp_uart_device_event.sensor_event[3].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[3].value = (float) _data.pm10_env;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[3].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM10_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[3].value =
(float)_data.pm10_env;
// pm25_env
msgUARTResponse.payload.resp_uart_device_event.sensor_event[4].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[4].value = (float) _data.pm25_env;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[4].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM25_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[4].value =
(float)_data.pm25_env;
// pm100_env
msgUARTResponse.payload.resp_uart_device_event.sensor_event[5].type = wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[5].value = (float) _data.pm100_env;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[5].type =
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_PM100_ENV;
msgUARTResponse.payload.resp_uart_device_event.sensor_event[5].value =
(float)_data.pm100_env;

// Encode data
// Encode message data
uint8_t mqttBuffer[512] = {0};
pb_ostream_t ostream =
pb_ostream_from_buffer(mqttBuffer, sizeof(mqttBuffer));
if (!pb_encode(&ostream, wippersnapper_signal_v1_UARTResponse_fields,
&msgUARTResponse)) {
Serial.println("[ERROR, UART]: Unable to encode device response!");
return;
}

// Publish message to IO
size_t msgSz;
pb_get_encoded_size(&msgSz, wippersnapper_signal_v1_UARTResponse_fields,
&msgUARTResponse);
Serial.print("[UART] Publishing event to IO..");
// TODO: Re-enable
//_mqttClient->publish(WS._topic_signal_i2c_device, mqttBuffer, msgSz, 1);
Serial.println("Published!");
}

protected:
Expand Down

0 comments on commit 2e54ee8

Please sign in to comment.