Sensornode with dht sensor and rest service support

This commit is contained in:
2017-08-24 22:07:20 +02:00
parent 6b02e29383
commit e12b9331ec
5 changed files with 2017 additions and 27 deletions

1
ESP8266/README.md Normal file
View File

@@ -0,0 +1 @@
# Description Sensornode

1953
ESP8266/sensornode/aREST.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,8 @@
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h" #include "DHT.h"
#include <ESP8266WiFi.h>
#include "aREST.h"
//DHT settings:
#define DHTPIN 14 // what digital pin we're connected to #define DHTPIN 14 // what digital pin we're connected to
// Uncomment whatever type you're using! // Uncomment whatever type you're using!
@@ -23,11 +23,53 @@
// as the current DHT reading algorithm adjusts itself to work on faster procs. // as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE); DHT dht(DHTPIN, DHTTYPE);
// Create aREST instance
aREST rest = aREST();
// WiFi settings:
const char* ssid = "Signal im Regal";
const char* password = "42003489";
#define LISTEN_PORT 80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
// Variables to be exposed to the API
float temperature;
float humidity;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("DHTxx test!"); Serial.println("Sensornode start");
//dht driver initialization
dht.begin(); dht.begin();
//Expose variables to the rest api
rest.variable("temperature", &temperature);
rest.variable("humidity", &humidity);
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("1");
rest.set_name("sensornode");
// Connect to WiFi
Serial.println("Connecting to wlan");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
} }
void loop() { void loop() {
@@ -39,31 +81,25 @@ void loop() {
float h = dht.readHumidity(); float h = dht.readHumidity();
// Read temperature as Celsius (the default) // Read temperature as Celsius (the default)
float t = dht.readTemperature(); float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again). // Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) { if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!"); Serial.println("Failed to read from DHT sensor!");
return; return;
} else {
//set the new values
humidity = h;
temperature = t;
} }
// Compute heat index in Fahrenheit (the default) // Handle REST calls
float hif = dht.computeHeatIndex(f, h); WiFiClient client = server.available();
// Compute heat index in Celsius (isFahreheit = false) if (!client) {
float hic = dht.computeHeatIndex(t, h, false); return;
}
Serial.print("Humidity: "); while(!client.available()){
Serial.print(h); delay(1);
Serial.print(" %\t"); }
Serial.print("Temperature: "); rest.handle(client);
Serial.print(t); }
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}