diff --git a/libraries/Terkin/TerkinTelemetry.cpp b/libraries/Terkin/TerkinTelemetry.cpp index fc95f82..ef06aea 100644 --- a/libraries/Terkin/TerkinTelemetry.cpp +++ b/libraries/Terkin/TerkinTelemetry.cpp @@ -139,6 +139,8 @@ bool TelemetryManager::transmit(const char *address_path, JsonObject& data) { * **/ +#if USE_GPRSBEE + // Initialize without APN authentication GPRSbeeTransmitter::GPRSbeeTransmitter(GPRSbeeClass& driver, const char *apn) : @@ -195,3 +197,101 @@ bool GPRSbeeTransmitter::transmit(const char *uri, JsonObject& data) { return retval; } +#endif + + +#if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32 + +// https://github.com/esp8266/Arduino/blob/2.7.4/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino#L43-L72 + +#if defined ARDUINO_ARCH_ESP8266 +#include +#include +#include +#include +#elif defined ARDUINO_ARCH_ESP32 +#include +#include +#include +#endif + +void sendRequest(const char *uri) { + WiFiClient client; + + HTTPClient http; + + Serial.print("[HTTP] begin...\n"); + if (http.begin(client, uri)) { + + + Serial.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); + + // httpCode will be negative on error + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + Serial.printf("[HTTP] GET... code: %d\n", httpCode); + + // file found at server + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + String payload = http.getString(); + Serial.println(payload); + } + } else { + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); + } else { + Serial.printf("[HTTP} Unable to connect\n"); + } +} + +ESPHTTPTransmitter::ESPHTTPTransmitter() +{} + +// The transmit method obtains a reference to a JsonObject +// which will get serialized before handing the payload +// over to the appropriate driver component. +bool ESPHTTPTransmitter::transmit(const char *uri, JsonObject& data) { + + // Serialize data + char payload[256]; + data.printTo(payload, sizeof(payload)); + + // Transmit data + // Derived from https://github.com/SodaqMoja/GPRSbee/wiki#do-a-http-post + //const char testData[] = "testdata3 = hello world with newline\n"; + char response[50]; + memset(response, 0, sizeof(response)); + bool retval; + + sendRequest(uri); + + /* + if (!_authenticated) { + // Without APN authentication + //retval = _driver.doHTTPPOSTWithReply(_apn, uri, payload, strlen(payload), response, sizeof(response)); + } else { + // With APN authentication + //retval = _driver.doHTTPPOSTWithReply(_apn, _apnuser, _apnpwd, uri, payload, strlen(payload), response, sizeof(response)); + } + */ + + // FIXME: Enable debugging + /* + if (retval) { + print(F("Post result: ")); + print('"'); + print(buffer); + println('"'); + } else { + println(F("Post failed!")); + } + */ + + return retval; + +} +#endif diff --git a/libraries/Terkin/TerkinTelemetry.h b/libraries/Terkin/TerkinTelemetry.h index 515110b..3e201f5 100644 --- a/libraries/Terkin/TerkinTelemetry.h +++ b/libraries/Terkin/TerkinTelemetry.h @@ -28,7 +28,9 @@ #define TerkinTelemetry_h #include // https://github.com/bblanchon/ArduinoJson +#if USE_GPRSBEE #include // https://github.com/SodaqMoja/GPRSbee +#endif namespace Terkin { @@ -137,6 +139,7 @@ namespace Terkin { * Transmitter component wrapping the GPRSbeeClass of the Sodaq GSM modem driver. * **/ +#if USE_GPRSBEE class GPRSbeeTransmitter : public GenericJsonTransmitter { public: GPRSbeeTransmitter(GPRSbeeClass& driver, const char *apn); @@ -149,8 +152,14 @@ namespace Terkin { const char *_apnpwd; bool _authenticated = false; }; +#endif - + class ESPHTTPTransmitter : public GenericJsonTransmitter { + public: + ESPHTTPTransmitter(); + bool transmit(const char *uri, JsonObject& data); + protected: + }; } diff --git a/node-pipa/Makefile b/node-pipa/Makefile index ba259bb..ddbfaeb 100644 --- a/node-pipa/Makefile +++ b/node-pipa/Makefile @@ -1,11 +1,20 @@ -$(eval venvpath := .venv) +# Build all Pipa examples + +# Get the full path and directory of Makefile +# https://www.systutorials.com/how-to-get-the-full-path-and-directory-of-a-makefile-itself/ +mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) +mkfile_dir := $(dir $(mkfile_path)) + +# Configure tool shortcuts +$(eval venvpath := $(mkfile_dir)/.venv) $(eval python := $(venvpath)/bin/python) $(eval pip := $(venvpath)/bin/pip) $(eval pio := $(venvpath)/bin/pio) build: setup-virtualenv - $(pio) run # --verbose + cd examples/legacy-gprsbee; $(pio) run # --verbose + cd examples/pipa-espressif; $(pio) run # --verbose upload: setup-virtualenv $(pio) run --target upload --upload-port=${MCU_PORT} diff --git a/node-pipa/README.rst b/node-pipa/README.rst index e5ace4d..e81addd 100644 --- a/node-pipa/README.rst +++ b/node-pipa/README.rst @@ -21,7 +21,8 @@ The program currently compiles for - AVR ATmega328 - Espressif ESP8266 -- Espressif ESP32, ESP32S2 +- Espressif ESP32 +- Espressif ESP32S2 - ARM SAMD Cortex-M0 diff --git a/node-pipa/examples/legacy-gprsbee/pipa-legacy-gprsbee.ino b/node-pipa/examples/legacy-gprsbee/pipa-legacy-gprsbee.ino index 879d836..be2f37e 100644 --- a/node-pipa/examples/legacy-gprsbee/pipa-legacy-gprsbee.ino +++ b/node-pipa/examples/legacy-gprsbee/pipa-legacy-gprsbee.ino @@ -69,7 +69,7 @@ #define HE_API_URL "http://swarm.hiveeyes.org/api-notls/" #define HE_USER "testdrive" #define HE_SITE "area-42" -#define HE_HIVE "gprs-workbench" +#define HE_HIVE "pipa-gprsbee-01" // GPRSbee diff --git a/node-pipa/examples/pipa-espressif/pipa-espressif.ino b/node-pipa/examples/pipa-espressif/pipa-espressif.ino new file mode 100644 index 0000000..e92b4b0 --- /dev/null +++ b/node-pipa/examples/pipa-espressif/pipa-espressif.ino @@ -0,0 +1,207 @@ +/* + + Hiveeyes Pipa Datalogger + + A software spike to submit data using native WiFi on Espressif chips. + + Copyright (C) 2015-2016 Clemens Gruber + Copyright (C) 2015-2016 Richard Pobering + Copyright (C) 2015-2021 Andreas Motl + + + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see: + , + or write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +------------------------------------------------------------------------- + + This is an Arduino sketch for the Hiveeyes bee monitoring system. + The purpose is to collect vital data of a bee hive and transmit it + to the data collection server. + + The sensor data could be temperature (DS18B20), humidity (BME280) + or a load cell (H30A with HX711). Other sensors can easily be added. + + After the sensor data is collected, it gets serialized to JSON, + which will be the transmitted in the body of a HTTP POST request. + + The creation of this code is strongly influenced by other projects, + so credits goes to them. + + Feel free to adapt this code to your own needs. + +------------------------------------------------------------------------- + + Futher informations can be obtained at: + + hiveeyes https://hiveeyes.org/ + documentation https://hiveeyes.org/docs/system/ + repository https://github.com/hiveeyes/ + open hive http://open-hive.org/ + + +------------------------------------------------------------------------- +*/ + + +// ****************************************** +// User configuration +// ****************************************** + +// Communication +// ============= + +// Telemetry +// --------- +#define HE_API_URL "http://swarm.hiveeyes.org/api-notls/" +#define HE_USER "testdrive" +#define HE_SITE "area-42" +#define HE_HIVE "pipa-espwifi-01" + + + +// Sensors +// ======= + +// TODO: No sensors yet. This is just a telemetry demo program. + + +// ****************************************** +// Developer configuration +// ****************************************** + +// Debugging +// --------- +/* +#define DEBUG +#define DEBUG_RADIO +#define DEBUG_FLASH +#define DEBUG_SENSORS +#define DEBUG_SERIAL +#define DEBUG_ENCODE +*/ + + +// ****************************************** +// Modules +// ****************************************** + + +// Libraries +// --------- +#include // https://github.com/bblanchon/ArduinoJson +#include // https://github.com/hiveeyes/arduino/tree/master/libraries/Terkin +#include // https://github.com/hiveeyes/arduino/tree/master/libraries/Hiveeyes +#include // https://github.com/hiveeyes/arduino/tree/master/libraries/OpenHive +#include // Arduino + + + +// Imports +// ------- +using namespace Terkin; +using namespace Hiveeyes; +using namespace OpenHive; + + + +// ****************************************** +// Sensors +// ****************************************** + + + + +// ****************************************** +// Main program +// ****************************************** + + +// Main telemetry API +TelemetryNode *telemetry; + +// Forward declarations +TelemetryNode& setup_telemetry_esphttp_json(); + + +// Setup function +void setup() { + + // Telemetry setup +#if USE_ESPHTTP_JSON + telemetry = &setup_telemetry_esphttp_json(); +#endif + + // Sensor setup + // FIXME: No sensors yet. This is just a telemetry demo program. + +} + +// Main loop +void loop() { + + + // 1. Read sensors + + // Collect basic sensor data + // TODO: No sensors yet. This is just a telemetry demo program. + JsonObject& data_basic = openhive_sensors.read_demo(); + + // Collect sensor data from temperature array + // TODO: No sensors yet. This is just a telemetry demo program. + JsonObject& data_temparray = openhive_temparray.read_demo(); + + + // 2. Transmit data + + // Transmit telemetry data to data collection server + telemetry->transmit(data_basic); + telemetry->transmit(data_temparray); + + // TODO: Start sleeping cycle, see "node-rfm69-beradio.ino" + delay(5000); + +} + + +/** + * + * Setup function for initializing a TelemetryNode instance + * with appropriate components in individual setups. + * + * Here, the machinery is instructed to + * communicate using WiFi on an Espressif device. + * +**/ +#if USE_ESPHTTP_JSON +TelemetryNode& setup_telemetry_esphttp_json() { + + /* Setup telemetry client with pluggable components */ + + // Transmitter + ESPHTTPTransmitter transmitter; + + // Telemetry manager + TelemetryManager manager(transmitter, HE_API_URL); + + // Telemetry node + HiveeyesNodeAddress address(HE_USER, HE_SITE, HE_HIVE); + TelemetryNode node(manager, address); + + return node; + +} +#endif diff --git a/node-pipa/examples/pipa-espressif/platformio.ini b/node-pipa/examples/pipa-espressif/platformio.ini new file mode 100644 index 0000000..61e7c9b --- /dev/null +++ b/node-pipa/examples/pipa-espressif/platformio.ini @@ -0,0 +1,89 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +src_dir = . + +[common] +lib_deps = + ArduinoJson@^5 + +lib_extra_dirs = + ../../../libraries + +# Because "libraries" is included above, we need to exclude specific code there. +lib_ignore = + StandardCplusplus + RTCZero + GPRSbee + +build_flags = + -D USE_ESPHTTP_JSON=true + + +[env:esp8266] +platform = espressif8266 +board = nodemcuv2 +framework = arduino + +lib_ldf_mode = deep+ +lib_deps = + ${common.lib_deps} + #../libraries/MemoryFree +lib_extra_dirs = + ${common.lib_extra_dirs} + +lib_ignore = + ${common.lib_ignore} + +build_flags = + ${common.build_flags} + + +[env:esp32] +platform = espressif32 +board = ttgo-t1 +framework = arduino + +lib_ldf_mode = deep+ +lib_deps = + ${common.lib_deps} + fbiego/ESP32Time @ ^1.0.3 +lib_extra_dirs = + ${common.lib_extra_dirs} + +lib_ignore = + ${common.lib_ignore} + +build_flags = + ${common.build_flags} + + +[env:esp32s2] +#platform = espressif32 +platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-v4.2 +board = featheresp32-s2 +#board = metroesp32-s2 +board_build.mcu = esp32s2 +framework = arduino +platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git + +lib_ldf_mode = deep+ +lib_deps = + ${common.lib_deps} + fbiego/ESP32Time @ ^1.0.3 +lib_extra_dirs = + ${common.lib_extra_dirs} + +lib_ignore = + ${common.lib_ignore} + +build_flags = + ${common.build_flags}