Files
Arduino/ESP8266_DOMOTICZ_DIMMER_THERMISTANCE/ESP8266_DOMOTICZ_DIMMER_THERMISTANCE.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

392 lines
9.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/////////////////////
// Domoticz Classe
/////////////////////
#include "Domoticz.h"
#include <ESP8266WebServer.h>
#include <Ticker.h>
#define NO_THERMISTANCE true
void handleRoot(void);
int getValueFromParam();
void setPwm(int pwm);
Ticker flipper;
Domoticz domo("192.168.1.3", "81", "Livebox-37cc", "8A6060920A8A86896F770F2C47");
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress DNS(192, 168, 1, 1);
// https://learn.adafruit.com/thermistor/using-a-thermistor
#ifndef NO_THERMISTANCE
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 11200
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 20
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 6700
#define DOMOTICZ_DEVICE_ID 1154
uint16_t samples[NUMSAMPLES];
#endif
// DIMMER
ESP8266WebServer server(80);
void ICACHE_RAM_ATTR handleInterrupt();
// Dimmer
#include <RBDdimmer.h>//
#define LEDn 15
#define outputPin 13
#define zerocross 12 // for boards with CHANGEBLE input pins
#define pas 5
dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
//dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
int outVal = 0;
String webpage = R"=(
<html>
<head>
<style>
.btn {
border: 1px black;
}
</style>
</head>
<body>
<div id="LEDn" style="background:#C0C0C0; width:128px; height:64px">
<div class="btn" style="background:#C0C0C0; width:100%; height:10%">
<h2 align="center">ON</h2>
</div>
</div>
<div id="pwmplus" style="background:#C0C0C0; width:128px; height:64px">
<div class="btn" style="background:#C0C0C0; width:100%; height:10%">
<h2 align="center">PWM plus</h2>
</div>
</div>
<div id="pwmminus" style="background:#C0C0C0; width:128px; height:64px">
<div class="btn" style="background:#C0C0C0; width:100%; height:10%">
<h2 align="center">PWM minus</h2>
</div>
</div>
<div id="pwmstop" style="background:#C0C0C0; width:128px; height:64px">
<div class="btn" style="background:#C0C0C0; width:100%; height:10%">
<h2 align="center">PWM stop</h2>
</div>
</div>
<div id="pwmexact" style="background:#C0C0C0; width:128px; height:64px">
<div class="btn" style="background:#C0C0C0; width:100%; height:10%">
<h2 align="center">PWM exact</h2>
</div>
</div>
<p>@@pwmCount@@</p>
</body>
<script>
window.addEventListener('load', function(){
var n = document.getElementById('LEDn')
var exact = document.getElementById('pwmexact')
var minus = document.getElementById('pwmminus')
var plus = document.getElementById('pwmplus')
var stop = document.getElementById('pwmstop')
var xhr = new XMLHttpRequest();
n.onmousedown = function(){
xhr.open("GET", "LEDon", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}
n.addEventListener('touchstart', function(e){
xhr.open("GET", "LEDon", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}, false)
plus.onmousedown = function(){
xhr.open("GET", "plus", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}
stop.onmousedown = function(){
xhr.open("GET", "stop", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}
plus.addEventListener('touchstart', function(e){
xhr.open("GET", "plus", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}, false)
minus.onmousedown = function(){
xhr.open("GET", "minus", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}
minus.addEventListener('touchstart', function(e){
xhr.open("GET", "minus", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}, false)
exact.onmousedown = function(){
xhr.open("GET", "exact", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}
exact.addEventListener('touchstart', function(e){
xhr.open("GET", "exact", true);
xhr.send();
setTimeout('location.reload(true)', 1000);
}, false)
},false)
</script>
</html>
)=";
bool led;
int pwm;
int pwmCount;
void ledON(){
led=1;
digitalWrite(LEDn, led);
handleRoot();
}
void ledOFF(){
led=0;
digitalWrite(LEDn, led);
handleRoot();
}
//pwm functions - if pwm is set to 1/-1 it will in-/decrease pwmCounter in loop()
void exact(){
int value_to_set = getValueFromParam();
pwm=value_to_set;
pwmCount = pwm;
if (pwmCount>100) pwmCount = 100;
if (pwmCount < 0) {
pwmCount = 0;
ledOFF();
}
else {
ledON();
}
Serial.println(pwmCount);
dimmer.setPower(pwmCount);
handleRoot();
}
void plus(){
int value_to_set = getValueFromParam();
pwm=value_to_set;
setPwm(pwm);
handleRoot();
}
void minus(){
int value_to_set = getValueFromParam();
pwm=-value_to_set;
setPwm(pwm);
handleRoot();
}
void stopPWM(){
pwm=-pwmCount;
setPwm(pwm);
ledOFF();
handleRoot();
}
void setPwm(int pwm)
{
pwmCount += pwm;
if (pwmCount>100) pwmCount = 100;
if (pwmCount < 0) {
pwmCount = 0;
ledOFF();
}
else {
ledON();
}
Serial.println(pwmCount);
dimmer.setPower(pwmCount);
}
//----------------------------------------------------------------------------------
void handleRoot(void){
String page = webpage;
page.replace("@@pwmCount@@", String(pwmCount));
//Serial.println(page);
server.send(200, "text/html", page);
}
int getValueFromParam()
{
String message = "Number of args received:";
message += server.args();
for (int i = 0; i < server.args(); i++) {
message = message + ("Arg nº" + String(i) + " > ");
message = message + (server.argName(i) + ": ");
message = message + (server.arg(i) + "\n");
if (server.argName(i) == "value") {
return String(server.arg(i)).toInt();
}
}
Serial.println(message);
return pas;
}
void setup()
{
Serial.begin(115200);
//delay(200);
String macId = domo.generateKey();
IPAddress ip = domo.getIP(macId);
// Conversion d'objet en pointeur
domo.initWifiStatic(ip, gateway, subnet, DNS);
//Server________________________________
server.begin();
server.on("/", handleRoot);
server.on("/LEDon", ledON);
server.on("/LEDoff", ledOFF);
server.on("/minus", minus);
server.on("/plus", plus);
server.on("/exact", exact);
server.on("/stop", stopPWM);
//initialize variables__________________
pwm = 0;
pwmCount= 0;
led = 0;
Serial.print("Module IP: ");
Serial.println(WiFi.softAPIP());
// Dimmer
Serial.println("Dimmer Program is starting...");
delay(1000);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
Serial.println("Set value");
dimmer.setPower(pwmCount); // setPower(0-100%);
// send temperature every 30s
// flipper.attach(30, flip);
}
#ifndef NO_THERMISTANCE
void flip() {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog reading ");
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart * 1.25);
Serial.println(" *C");
printInfo(steinhart * 1.25);
}
#endif
long temps = 0;
void loop()
{
server.handleClient();
#ifndef NO_THERMISTANCE
if (millis() > temps + 30000) {
flip();
temps = millis();
}
#endif
// sleep(300);
// delay(5000);
}
//void sleep(int sleepTime)
//{
// Serial.print("Go to sleep ");
// Serial.println(sleepTime);
// delay(20);
//
// ESP.deepSleep(sleepTime * 1000000L);
//
// //sleepWifi();
// delay(200);
//}
#ifndef NO_THERMISTANCE
void printInfo(double temperature)
{
// Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
boolean connected = domo.connect();
if (connected) {
Serial.println("Dans set temperature");
String svalue = String(temperature) ;
Serial.println(svalue);
domo.executeJson("/json.htm?type=command&param=udevice&idx=1154", svalue, "0");
domo._client.stop();
delay(200);
}
Serial.print("Time = ");
Serial.println(millis());
delay(200);
}
#endif