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

374 lines
11 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.
#include <ESP8266WebServer.h>
/* ---------------------- OUTPUT & INPUT Pin table ---------------------
* +---------------+-------------------------+-------------------------+
* | Board | INPUT Pin | OUTPUT Pin |
* | | Zero-Cross | |
* +---------------+-------------------------+-------------------------+
* | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* +---------------+-------------------------+-------------------------+
* | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
* +---------------+-------------------------+-------------------------+
* | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
* +---------------+-------------------------+-------------------------+
* | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
* | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
* | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
* | | | D8(IO15) |
* +---------------+-------------------------+-------------------------+
* | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
* | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
* | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
* | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
* | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
* | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
* | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
* | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
* | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
* | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
* | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
* | | 33(GPIO21), 35(GPIO1), | |
* | | 36(GPIO22), 37(GPIO23), | |
* +---------------+-------------------------+-------------------------+
* | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
* | Arduino Zero | | |
* +---------------+-------------------------+-------------------------+
* | Arduino Due | D0-D53 | D0-D53 |
* +---------------+-------------------------+-------------------------+
* | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
* | Black Pill | PC13-PC15 | PC13-PC15 |
* | BluePill | | |
* | Etc... | | |
* +---------------+-------------------------+-------------------------+
*/
//Define name of the Wifi & password for creating an access point
const char* ssid = "Livebox-37cc";
const char* pass = "8A6060920A8A86896F770F2C47";
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(9600);
pinMode(LEDn,OUTPUT);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress DNS(192, 168, 1, 1);
Serial.begin(9600);
// Conversion d'objet en pointeur
String macId = generateKey();
IPAddress ip = getIP(macId);
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%);
}
void loop() {
server.handleClient();
}
String generateKey()
{
// WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
{
AP_NameChar[i] = AP_NameString.charAt(i);
}
// WiFi.softAP(AP_NameChar, WiFiAPPSK);
Serial.println("macID=" + macID);
return macID;
}
IPAddress getIP(String macId)
{
IPAddress ip; //(192, 168, 1, 222);
String fst = macId.substring(0, 2);
String sec = macId.substring(2);
char fstc[fst.length() + 1];
fst.toCharArray(fstc, fst.length() + 1);
char secc[sec.length() + 1];
sec.toCharArray(secc, fst.length() + 1);
return IPAddress(192, 168, strtol(fstc, 0, 16), strtol(secc, 0, 16));
}
void initWifiStatic(
IPAddress ip,
IPAddress gateway,
IPAddress subnet,
IPAddress DNS)
{
WiFi.config(ip, gateway, subnet, DNS);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
}
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println();
Serial.println("Fail connecting");
delay(5000);
ESP.restart();
}
Serial.print(" static OK ");
Serial.print("Module IP: ");
Serial.println(WiFi.localIP());
}