Sensornode with dht sensor and rest service support
This commit is contained in:
1
ESP8266/README.md
Normal file
1
ESP8266/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# Description Sensornode
|
1953
ESP8266/sensornode/aREST.h
Normal file
1953
ESP8266/sensornode/aREST.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,8 @@
|
||||
// Example testing sketch for various DHT humidity/temperature sensors
|
||||
// Written by ladyada, public domain
|
||||
|
||||
#include "DHT.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "aREST.h"
|
||||
|
||||
//DHT settings:
|
||||
#define DHTPIN 14 // what digital pin we're connected to
|
||||
|
||||
// Uncomment whatever type you're using!
|
||||
@@ -23,11 +23,53 @@
|
||||
// as the current DHT reading algorithm adjusts itself to work on faster procs.
|
||||
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() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("DHTxx test!");
|
||||
Serial.println("Sensornode start");
|
||||
|
||||
//dht driver initialization
|
||||
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() {
|
||||
@@ -39,31 +81,25 @@ void loop() {
|
||||
float h = dht.readHumidity();
|
||||
// Read temperature as Celsius (the default)
|
||||
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).
|
||||
if (isnan(h) || isnan(t) || isnan(f)) {
|
||||
if (isnan(h) || isnan(t)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return;
|
||||
} else {
|
||||
//set the new values
|
||||
humidity = h;
|
||||
temperature = t;
|
||||
|
||||
}
|
||||
|
||||
// Compute heat index in Fahrenheit (the default)
|
||||
float hif = dht.computeHeatIndex(f, h);
|
||||
// Compute heat index in Celsius (isFahreheit = false)
|
||||
float hic = dht.computeHeatIndex(t, h, false);
|
||||
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(h);
|
||||
Serial.print(" %\t");
|
||||
Serial.print("Temperature: ");
|
||||
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");
|
||||
// Handle REST calls
|
||||
WiFiClient client = server.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
while(!client.available()){
|
||||
delay(1);
|
||||
}
|
||||
rest.handle(client);
|
||||
}
|
||||
|
Reference in New Issue
Block a user