The client pushes the measures to a Nuclio function on K8S
This commit is contained in:
parent
874a5c5a78
commit
21f2848531
3 changed files with 113 additions and 114 deletions
|
@ -1,150 +1,120 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
#include <EEPROM.h>
|
||||
#include <DHT.h>
|
||||
|
||||
|
||||
// From https://github.com/DaKaZ/esp8266-restclient
|
||||
#include "RestClient.h"
|
||||
|
||||
#include <Wire.h>
|
||||
#include <SPI.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BMP280.h>
|
||||
|
||||
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("<!DOCTYPE HTML>");
|
||||
client.println("<html>");
|
||||
|
||||
client.println("<h1>");
|
||||
client.println(getNodeId());
|
||||
client.println("</h1>");
|
||||
|
||||
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("<br>");
|
||||
String sleepTimeStr;
|
||||
String nodeId;
|
||||
|
||||
client.print ("Humidity: ");
|
||||
client.print (h);
|
||||
client.print ("%");
|
||||
client.println("<br>");
|
||||
|
||||
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("<br><br>");
|
||||
client.println("</html>");
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
22
sensors-input/function.yaml
Normal file
22
sensors-input/function.yaml
Normal file
|
@ -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
|
7
sensors-input/handler.js
Normal file
7
sensors-input/handler.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
exports.handler = function(context, event) {
|
||||
context.logger.info('Connected: ' + event.body);
|
||||
|
||||
var request = JSON.parse(event.body);
|
||||
|
||||
context.callback('Thanks !');
|
||||
};
|
Reference in a new issue