Skip to content

Commit

Permalink
TerkinTelemetry: Add rough sketch-like support for Espressif (HTTP)
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed May 5, 2021
1 parent 4e8e8fb commit 0eae253
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 5 deletions.
100 changes: 100 additions & 0 deletions libraries/Terkin/TerkinTelemetry.cpp
Expand Up @@ -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)
:
Expand Down Expand Up @@ -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 <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#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
11 changes: 10 additions & 1 deletion libraries/Terkin/TerkinTelemetry.h
Expand Up @@ -28,7 +28,9 @@
#define TerkinTelemetry_h

#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#if USE_GPRSBEE
#include <GPRSbee.h> // https://github.com/SodaqMoja/GPRSbee
#endif

namespace Terkin {

Expand Down Expand Up @@ -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);
Expand All @@ -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:
};

}

Expand Down
13 changes: 11 additions & 2 deletions 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}
Expand Down
3 changes: 2 additions & 1 deletion node-pipa/README.rst
Expand Up @@ -21,7 +21,8 @@ The program currently compiles for

- AVR ATmega328
- Espressif ESP8266
- Espressif ESP32, ESP32S2
- Espressif ESP32
- Espressif ESP32S2
- ARM SAMD Cortex-M0


Expand Down
2 changes: 1 addition & 1 deletion node-pipa/examples/legacy-gprsbee/pipa-legacy-gprsbee.ino
Expand Up @@ -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
Expand Down
207 changes: 207 additions & 0 deletions 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 <clemens@hiveeyes.org>
Copyright (C) 2015-2016 Richard Pobering <richard@hiveeyes.org>
Copyright (C) 2015-2021 Andreas Motl <andreas@hiveeyes.org>
<https://hiveeyes.org>
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:
<http://www.gnu.org/licenses/gpl-3.0.txt>,
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 <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <Terkin.h> // https://github.com/hiveeyes/arduino/tree/master/libraries/Terkin
#include <Hiveeyes.h> // https://github.com/hiveeyes/arduino/tree/master/libraries/Hiveeyes
#include <OpenHive.h> // https://github.com/hiveeyes/arduino/tree/master/libraries/OpenHive
#include <Wire.h> // 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

0 comments on commit 0eae253

Please sign in to comment.