142 lines
3.5 KiB
C++
Executable File
142 lines
3.5 KiB
C++
Executable File
extern "C" {
|
|
#include "user_interface.h"
|
|
}
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <Wire.h>
|
|
#include "DHT.h"
|
|
|
|
const char* ssid = "Livebox-37cc";
|
|
const char* password = "8A6060920A8A86896F770F2C47";
|
|
const int sleepTime = 60; //Power down WiFi for 6 seconds
|
|
|
|
// DHT
|
|
|
|
#define DHTPIN 2 // what pin we're connected to
|
|
#define DHTTYPE DHT11 // DHT 11
|
|
// Initialize DHT sensor for normal 16mhz Arduino
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
|
|
// int status = WL_IDLE_STATUS; // the Wifi radio's status
|
|
WiFiClient client;
|
|
|
|
// domoticz
|
|
const char * domoticz_server = "192.168.0.10"; //Domoticz port
|
|
int port = 8080; //Domoticz port
|
|
int idx = 660; //IDX for this virtual sensor, found in Setup -> Devices
|
|
|
|
float temp = 0.0;
|
|
float humidite = 0.0;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
Wire.pins(0, 2);
|
|
Wire.begin(0, 2);
|
|
|
|
// DHT
|
|
dht.begin();
|
|
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
initWifi();
|
|
doDHT();
|
|
printInfo();
|
|
sleepWifi();
|
|
}
|
|
void doDHT() {
|
|
// Reading temperature or humidity takes about 250 milliseconds!
|
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
|
float h = dht.readHumidity();
|
|
humidite = h;
|
|
|
|
// Read temperature as Celsius
|
|
float t = dht.readTemperature();
|
|
// Read temperature as Fahrenheit
|
|
float f = dht.readTemperature(true);
|
|
|
|
// Check if any reads failed and exit early (to try again).
|
|
if (isnan(h) || isnan(t) || isnan(f)) {
|
|
// Serial.println("Failed to read from DHT sensor!");
|
|
return;
|
|
} else {
|
|
// Compute heat index
|
|
// Must send in temp in Fahrenheit!
|
|
float hi = dht.computeHeatIndex(f, h);
|
|
|
|
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(hi);
|
|
Serial.println(" *F");
|
|
|
|
temp = t;
|
|
}
|
|
}
|
|
|
|
void sleepWifi()
|
|
{
|
|
//Time to let the WiFi go to sleep!
|
|
Serial.println("Sleeping...");
|
|
WiFi.disconnect();
|
|
WiFi.mode(WIFI_OFF);
|
|
WiFi.forceSleepBegin(sleepTime * 1000000L); //In uS. Must be same length as your delay
|
|
delay(sleepTime * 1000); //Hang out at 15mA for 6 seconds
|
|
WiFi.mode(WIFI_STA);
|
|
Serial.println("Awake!");
|
|
}
|
|
void initWifi()
|
|
{
|
|
WiFi.mode(WIFI_AP);
|
|
WiFi.begin(ssid, password); //Connect to local Wifi
|
|
|
|
Serial.println();
|
|
Serial.print("Connecting to WiFi");
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.print(".");
|
|
delay(500);
|
|
}
|
|
Serial.println("WiFi Connected!");
|
|
}
|
|
|
|
void printInfo()
|
|
{
|
|
// Domoticz format /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
|
|
|
|
if (client.connect(domoticz_server, port) && temp > 0) {
|
|
|
|
// /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
|
|
//domoticz:8080/json.htm?type=command¶m=udevice&idx=659&nvalue=0&svalue=16.00;50;50;1007;760
|
|
client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
|
client.print(idx);
|
|
client.print("&nvalue=0&svalue=");
|
|
client.print(temp);
|
|
client.print(";");
|
|
client.print(humidite);
|
|
client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
|
|
client.print(";");
|
|
client.print(humidite);
|
|
client.println(" HTTP/1.1");
|
|
client.print("Host: ");
|
|
client.print(domoticz_server);
|
|
client.print(":");
|
|
client.println(port);
|
|
client.println("User-Agent: Arduino-ethernet");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
client.stop();
|
|
}
|
|
}
|
|
|