From 21f28485311aed82798d20b425e8839db4122cba Mon Sep 17 00:00:00 2001 From: Thomas Schwery Date: Wed, 9 May 2018 21:51:23 +0200 Subject: [PATCH] The client pushes the measures to a Nuclio function on K8S --- arduino-sensor/arduino-sensor.ino | 198 +++++++++++++----------------- sensors-input/function.yaml | 22 ++++ sensors-input/handler.js | 7 ++ 3 files changed, 113 insertions(+), 114 deletions(-) create mode 100644 sensors-input/function.yaml create mode 100644 sensors-input/handler.js diff --git a/arduino-sensor/arduino-sensor.ino b/arduino-sensor/arduino-sensor.ino index f324c50..af28baf 100644 --- a/arduino-sensor/arduino-sensor.ino +++ b/arduino-sensor/arduino-sensor.ino @@ -1,150 +1,120 @@ #include #include -#include - + +// From https://github.com/DaKaZ/esp8266-restclient +#include "RestClient.h" + +#include +#include +#include +#include + const char* ssid = "FLRLAN"; const char* password = "XXXXXX"; -#define DHT_PIN 4 +const char * host = "apis.lab.inf3.xyz"; +const int httpsPort = 443; +const char * path = "/esp8266/sensors"; -DHT dht(DHT_PIN, DHT22); +#define BMP_SCK 13 +#define BMP_MISO 12 +#define BMP_MOSI 11 +#define BMP_CS 10 + +Adafruit_BMP280 bmp; -WiFiServer server(80); - void setup() { Serial.begin(115200); - dht.begin(); + delay(500); + + while (!bmp.begin()) { + Serial.println("Could not find a valid BMP280 sensor, check wiring!"); + delay(100); + } + delay(1000); + EEPROM.begin(512); - - delay(10); - + // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); - + WiFi.begin(ssid, password); - + + int c = 0; while (WiFi.status() != WL_CONNECTED) { - delay(500); + delay(250); + c++; Serial.print("."); + + if (c > 100) { + Serial.println(); + Serial.println("Unable to connect. Sleeping for 30 seconds before trying again."); + int sleepTime = 30 * 1e6; + ESP.deepSleep(sleepTime); + } } Serial.println(""); Serial.println("WiFi connected"); - - // Start the server - server.begin(); - Serial.println("Server started"); - - // Print the IP address - Serial.print("Use this URL to connect: "); - Serial.print("http://"); - Serial.print(WiFi.localIP()); - Serial.println("/"); } -#define REQ_SLP 0 -#define REQ_SET 1 -#define REQ_ERR 99 - void loop() { - // Check if a client has connected - WiFiClient client = server.available(); - if (!client) { - return; - } - - // Wait until the client sends some data - Serial.println("new client"); - while(!client.available()){ - delay(1); - } - - // Read the first line of the request - String requestStr = client.readStringUntil('\r'); - Serial.println(requestStr); - client.flush(); - int idxSpace = requestStr.indexOf(" "); - requestStr = requestStr.substring(idxSpace + 1); - - int idxSndSpace = requestStr.indexOf(" "); - requestStr = requestStr.substring(0, idxSndSpace); - - // Match the request - - int value = REQ_ERR; - String parameter = ""; - - if (requestStr.indexOf("/SLEEP=") != -1) { - value = REQ_SLP; - int idx = requestStr.indexOf("/SLEEP="); - parameter = requestStr.substring(idx + 7); - } else if (requestStr.indexOf("/SET=") != -1) { - value = REQ_SET; - int idx = requestStr.indexOf("/SET="); - parameter = requestStr.substring(idx + 5); - } else { - value = REQ_ERR; + float h = bmp.readPressure(); + float t = bmp.readTemperature(); + + while (isnan(h) || isnan(t) || t > 100 || t < -100) { + h = bmp.readPressure(); + t = bmp.readTemperature(); } - - // Headers of the response - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println(""); + Serial.println("Got t=" + String(t) + " and h=" + String(h) + "..."); - // HTML part - client.println(""); - client.println(""); - client.println("

"); - client.println(getNodeId()); - client.println("

"); - - float h = dht.readHumidity(); - float t = dht.readTemperature(); + String content = "{\n"; + //content += "\"id\": \"" + String(getNodeId()) + "\",\n"; + content += "\"temp\": " + String(t) + ",\n"; + content += "\"pressure\": " + String(h) + "\n"; + content += "}\n"; - while (isnan(h) || isnan(t)) { - h = dht.readHumidity(); - t = dht.readTemperature(); + Serial.println("Sending: " + content); + + String response = ""; + int statusCode = 0; + + while (statusCode < 100) { + RestClient client = RestClient(host, httpsPort, 1); + const char * contentArr = content.c_str(); + statusCode = client.post(path, contentArr, &response); + + Serial.println("Got response code: " + String(statusCode)); + Serial.println("Got response: " + response); + + if (statusCode < 100) { + delay(2500); + } } - client.print ("Temperature: "); - client.print (t); - client.print (" *C"); - client.println("
"); + String sleepTimeStr; + String nodeId; - client.print ("Humidity: "); - client.print (h); - client.print ("%"); - client.println("
"); - - client.print("Request is "); - client.println(value); - - client.print("Parameter is "); - client.println(parameter); + int sleepTime = sleepTimeStr.toInt() * 1e6; + if (sleepTime < 60 * 1e6) { + sleepTime = 15 * 60 * 1e6; + } - client.println("

"); - client.println(""); + Serial.print("Going to sleep for "); + Serial.print(sleepTime); + Serial.print(" nanoseconds"); - client.stop(); - - delay(1); - Serial.println("Client disonnected"); - - if (value == REQ_SLP) { - int sleepTime = parameter.toInt() * 1e6; - Serial.print("Going to sleep for "); - Serial.print(sleepTime); - Serial.print(" nanoseconds"); - - ESP.deepSleep(sleepTime); - } else if (value == REQ_SET) { + ESP.deepSleep(sleepTime); + /* + } else if (value == REQ_SET) { setNodeId(parameter); - } + } + */ } #define LENGTH_ID 10 @@ -168,7 +138,7 @@ void setNodeId(String value) { EEPROM.commit(); } -String getNodeId() { +char* getNodeId() { size_t addr = 0x0; char* eepromId = new char[LENGTH_ID]; @@ -177,8 +147,8 @@ String getNodeId() { eepromId[i] = temp; } - return String(eepromId); - + return eepromId; + } diff --git a/sensors-input/function.yaml b/sensors-input/function.yaml new file mode 100644 index 0000000..6b0233f --- /dev/null +++ b/sensors-input/function.yaml @@ -0,0 +1,22 @@ +apiVersion: "nuclio.io/v1beta1" +kind: "Function" +metadata: + name: esp8266-temperature-input + namespace: nuclio +spec: + runtime: "nodejs" + handler: "handler" + build: + registry: registry.lab.inf3.xyz + commands: + - "npm install --global moment" + triggers: + http: + maxWorkers: 2 + kind: http + attributes: + ingresses: + second: + host: apis.lab.inf3.xyz + paths: + - /esp8266/sensors diff --git a/sensors-input/handler.js b/sensors-input/handler.js new file mode 100644 index 0000000..4245a64 --- /dev/null +++ b/sensors-input/handler.js @@ -0,0 +1,7 @@ +exports.handler = function(context, event) { + context.logger.info('Connected: ' + event.body); + + var request = JSON.parse(event.body); + + context.callback('Thanks !'); +};