diff --git a/platformio.ini b/platformio.ini index 7491dbca..f8822295 100644 --- a/platformio.ini +++ b/platformio.ini @@ -78,7 +78,7 @@ lib_deps = https://github.com/Sensirion/arduino-i2c-sen5x.git https://github.com/adafruit/WiFiNINA.git https://github.com/Starmbi/hp_BH1750.git - + https://github.com/adafruit/RTClib.git ; Common build environment for ESP32 platform [common:esp32] diff --git a/src/Wippersnapper_V2.cpp b/src/Wippersnapper_V2.cpp index bae78eac..a159afd3 100644 --- a/src/Wippersnapper_V2.cpp +++ b/src/Wippersnapper_V2.cpp @@ -434,6 +434,28 @@ void cbBrokerToDevice(char *data, uint16_t len) { WS_DEBUG_PRINTLN("Decoded BrokerToDevice message!"); } +void callDecodeB2D() { + WS_DEBUG_PRINTLN("\n[App] Parsing new Signal->B2D Message"); + + wippersnapper_signal_BrokerToDevice msg_signal = + wippersnapper_signal_BrokerToDevice_init_default; + + // Configure the payload callback + msg_signal.cb_payload.funcs.decode = cbDecodeBrokerToDevice; + + // Decode msg_signal + WS_DEBUG_PRINTLN("Creating input stream..."); + pb_istream_t istream = + pb_istream_from_buffer(WsV2._msgBuf, WsV2._szMessageBuf); + WS_DEBUG_PRINTLN("Decoding BrokerToDevice message..."); + if (!pb_decode(&istream, wippersnapper_signal_BrokerToDevice_fields, + &msg_signal)) { + WS_DEBUG_PRINTLN("ERROR: Unable to decode BrokerToDevice message!"); + return; + } + WS_DEBUG_PRINTLN("Decoded BrokerToDevice message!"); +} + /**************************************************************************/ /*! @brief Called when client receives a message published across the @@ -1170,7 +1192,8 @@ void Wippersnapper_V2::connectV2() { // Parse the JSON file if (!WsV2._sdCardV2->parseConfigFile()) haltErrorV2("Failed to parse incoming JSON file"); - // TODO: Configure the device using the incoming JSON file + WS_DEBUG_PRINT("[Offline] Attempting to configure hardware..."); + callDecodeB2D(); // TODO: We need a way to repeat this call better WS_DEBUG_PRINTLN( "[Offline Mode] Hardware configured, skipping network setup..."); return; diff --git a/src/Wippersnapper_V2.h b/src/Wippersnapper_V2.h index ff099171..1550458d 100644 --- a/src/Wippersnapper_V2.h +++ b/src/Wippersnapper_V2.h @@ -254,6 +254,12 @@ public: #endif bool got_checkin_response; ///< True if a checkin response was received, False ///< otherwise. + + wippersnapper_signal_BrokerToDevice _signalB2dV2; + pb_ostream_t _signalStream; + size_t _szMessageBuf; + uint8_t _msgBuf[512]; + private: void _initV2(); diff --git a/src/Wippersnapper_demo_wokwi.ino.cpp b/src/Wippersnapper_demo_wokwi.ino.cpp new file mode 100644 index 00000000..bc939f64 --- /dev/null +++ b/src/Wippersnapper_demo_wokwi.ino.cpp @@ -0,0 +1,38 @@ +# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmpurmcpxg_" +#include +# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo_wokwi.ino" +# 14 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo_wokwi.ino" +#define IO_USERNAME "brubell" +#define IO_KEY "YOUR_AIO_KEY" + +#define WIFI_SSID "Wokwi-GUEST" +#define WIFI_PASS "" + +#define WS_DEBUG + +#define API_PIN 0 + +#include "ws_manager.h" + + +Wippersnapper_Manager manager; +Wippersnapper_WiFiV2 wipper(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, "io.adafruit.com", 8883); +void setup(); +void loop(); +#line 31 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo_wokwi.ino" +void setup() { + + manager.checkAPIVersion(API_PIN); + manager.provision(); + + Serial.begin(115200); + + Serial.println("Adafruit Wippersnapper API Manager Demo"); + Serial.print("Running Wippersnapper API Version: "); + Serial.println(manager.getAPIVersion()); + manager.connect(); +} + +void loop() { + manager.run(); +} \ No newline at end of file diff --git a/src/components/analogIO/controller.cpp b/src/components/analogIO/controller.cpp index 381b9275..109f34b3 100644 --- a/src/components/analogIO/controller.cpp +++ b/src/components/analogIO/controller.cpp @@ -90,6 +90,16 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) { // Initialize the pin _analogio_hardware->InitPin(pin_name); + // Print out the pin's details + WS_DEBUG_PRINTLN("Added new analog pin:"); + WS_DEBUG_PRINT("Pin Name: "); + WS_DEBUG_PRINTLN(new_pin.name); + WS_DEBUG_PRINT("Period: "); + WS_DEBUG_PRINTLN(new_pin.period); + WS_DEBUG_PRINT("Read Mode: "); + WS_DEBUG_PRINTLN(new_pin.read_mode); + + // Add the new pin to the vector _analogio_pins.push_back(new_pin); diff --git a/src/provisioning/sdcard/ws_sdcard.cpp b/src/provisioning/sdcard/ws_sdcard.cpp index 9c87f350..8676be68 100644 --- a/src/provisioning/sdcard/ws_sdcard.cpp +++ b/src/provisioning/sdcard/ws_sdcard.cpp @@ -100,10 +100,12 @@ bool ws_sdcard::parseConfigFile() { components["componentAPI"]; // ie: "analogio", "digitalio", etc. if (component_api_type == nullptr) { - WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No component API type found in JSON string!"); + WS_DEBUG_PRINTLN("[SD] FATAL Parsing error - No component API type found " + "in JSON string!"); return false; } else { - WS_DEBUG_PRINTLN("[SD] Component API type found: " + String(component_api_type)); + WS_DEBUG_PRINTLN("[SD] Component API type found: " + + String(component_api_type)); } // TODO- maybe a Switch case to handle the different component API types but @@ -186,6 +188,41 @@ bool ws_sdcard::parseConfigFile() { WS_DEBUG_PRINTLN("Pin Name: " + String(msg_AnalogIOAdd.pin_name)); WS_DEBUG_PRINTLN("Period: " + String(msg_AnalogIOAdd.period)); WS_DEBUG_PRINTLN("Read Mode: " + String(msg_AnalogIOAdd.read_mode)); + + // TODO: Note that the TOP-LEVEL decoder is actually looking + // for a SIGNAL message with one of these SUBMESSAGEd + // so we'll need to encode this into a signalproto form before sending + // it over + + // Zero-out the signal message + // TODO: This should be global + WsV2._signalB2dV2 = wippersnapper_signal_BrokerToDevice_init_zero; + //. Fill the signal message with msg_AnalogIOAdd data + WsV2._signalB2dV2.which_payload = + wippersnapper_signal_BrokerToDevice_analogio_add_tag; + WsV2._signalB2dV2.payload.analogio_add = msg_AnalogIOAdd; + + // Get the encoded size of the signal message + WS_DEBUG_PRINTLN("Encoding D2b signal message..."); + // size_t szMessageBuf; + if (!pb_get_encoded_size(&WsV2._szMessageBuf, + wippersnapper_signal_BrokerToDevice_fields, + &WsV2._signalB2dV2)) { + WS_DEBUG_PRINTLN("[SD] ERROR: Unable to get signal message size!"); + return false; + } + // Encode the signal message + // uint8_t msgBuf[szMessageBuf]; + WsV2._signalStream = + pb_ostream_from_buffer(WsV2._msgBuf, WsV2._szMessageBuf); + if (!ws_pb_encode(&WsV2._signalStream, + wippersnapper_signal_BrokerToDevice_fields, + &WsV2._signalB2dV2)) { + WS_DEBUG_PRINTLN("[SD] ERROR: Unable to encode D2B signal message!"); + return false; + } + WS_DEBUG_PRINTLN("Encoded the D2b signal message"); + } else { // Unknown component API type WS_DEBUG_PRINTLN("[SD] Unknown component API type found: " + @@ -193,11 +230,6 @@ bool ws_sdcard::parseConfigFile() { return false; } - // TODO: Note that the TOP-LEVEL decoder is actually looking - // for a SIGNAL message with one of these SUBMESSAGEd - // so we'll need to encode this into a signalproto form before sending - // it over - return true; } diff --git a/tests/diagram.json b/tests/diagram.json index ffc41e1b..2a9d45c6 100644 --- a/tests/diagram.json +++ b/tests/diagram.json @@ -11,6 +11,14 @@ "left": -290.93, "rotate": 90, "attrs": {} + }, + { + "type": "wokwi-ds1307", + "id": "rtc1", + "top": -129.5, + "left": -204.4, + "rotate": 270, + "attrs": {} } ], "connections": [ @@ -21,7 +29,11 @@ [ "sd1:CS", "esp:D15", "#8f4814", [ "v0" ] ], [ "sd1:DI", "esp:D23", "blue", [ "v0" ] ], [ "sd1:SCK", "esp:D18", "green", [ "v0" ] ], - [ "sd1:DO", "esp:D19", "orange", [ "v0" ] ] + [ "sd1:DO", "esp:D19", "orange", [ "v0" ] ], + [ "rtc1:5V", "esp:VIN", "red", [ "v163.2", "h-230.8" ] ], + [ "rtc1:GND", "esp:GND.1", "black", [ "v76.8", "h-124.5" ] ], + [ "rtc1:SDA", "esp:D21", "violet", [ "v57.6", "h-144" ] ], + [ "rtc1:SCL", "esp:D22", "green", [ "v28.8", "h-153.5" ] ] ], "serialMonitor": { "display": "terminal", "newline": "lf" }, "dependencies": {}