first commit
This commit is contained in:
96
ESP8266_DOMOTICZ_LUM/ESP8266_DOMOTICZ_LUM.ino
Executable file
96
ESP8266_DOMOTICZ_LUM/ESP8266_DOMOTICZ_LUM.ino
Executable file
@@ -0,0 +1,96 @@
|
||||
extern "C" {
|
||||
#include "user_interface.h"
|
||||
}
|
||||
|
||||
#include <Wire.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
|
||||
const char* ssid = "Livebox-37cc";
|
||||
const char* password = "8A6060920A8A86896F770F2C47";
|
||||
const int sleepTime = 120; //Power down WiFi for n seconds
|
||||
|
||||
|
||||
// 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 = 158; //IDX for this virtual sensor, found in Setup -> Devices
|
||||
float lum = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
initWifi();
|
||||
|
||||
readLum();
|
||||
|
||||
printInfo();
|
||||
//delay(60000); // Wait 60 seconds
|
||||
//wifi_set_sleep_type(LIGHT_SLEEP_T);
|
||||
sleepWifi();
|
||||
//WiFi.disconnect();
|
||||
//delay(30000);
|
||||
}
|
||||
|
||||
void readLum()
|
||||
{
|
||||
lum = analogRead(2);
|
||||
Serial.println(lum);
|
||||
}
|
||||
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)) {
|
||||
|
||||
client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
||||
client.print(idx);
|
||||
client.print("&nvalue=0&svalue=");
|
||||
client.print((1000.0 * lum / 1024.0));
|
||||
client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user