just comment
This commit is contained in:
5
Holodolinik_ESP/.gitignore
vendored
Normal file
5
Holodolinik_ESP/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
7
Holodolinik_ESP/.vscode/extensions.json
vendored
Normal file
7
Holodolinik_ESP/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
]
|
||||
}
|
||||
39
Holodolinik_ESP/include/README
Normal file
39
Holodolinik_ESP/include/README
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the usual convention is to give header files names that end with `.h'.
|
||||
It is most portable to use only letters, digits, dashes, and underscores in
|
||||
header file names, and at most one dot.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
||||
46
Holodolinik_ESP/lib/README
Normal file
46
Holodolinik_ESP/lib/README
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into executable file.
|
||||
|
||||
The source code of each library should be placed in a an own separate directory
|
||||
("lib/your_library_name/[here are source files]").
|
||||
|
||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
and a contents of `src/main.c`:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
||||
19
Holodolinik_ESP/platformio.ini
Normal file
19
Holodolinik_ESP/platformio.ini
Normal file
@@ -0,0 +1,19 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:esp07]
|
||||
platform = espressif8266
|
||||
board = esp07
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
milesburton/DallasTemperature @ ^3.9.1
|
||||
mathertel/OneButton @ 0.0.0-alpha+sha.eb583d713a
|
||||
knolleary/PubSubClient @ ^2.8
|
||||
jwrw/ESP_EEPROM @ ^2.1.1
|
||||
227
Holodolinik_ESP/src/main.cpp
Normal file
227
Holodolinik_ESP/src/main.cpp
Normal file
@@ -0,0 +1,227 @@
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
#include <OneButton.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
|
||||
#define LED_WF (16) //Red
|
||||
#define LED_WRK (5) //Green
|
||||
#define LED_MQ (4) //Blue
|
||||
#define ONE_WIRE_BUS (0)
|
||||
#define BUTTON (2)
|
||||
#define BUZZER (14)
|
||||
#define DOOR_HOL (13)
|
||||
#define DOOR_MOR (12)
|
||||
|
||||
#define HOST_NAME "Holodilnik"
|
||||
#define MAIN_TOPIC "/home/kuht/"
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
const char* mqtt_server = "192.168.1.250";
|
||||
|
||||
|
||||
unsigned long cRun = millis();
|
||||
bool buzzer_en;
|
||||
float max_hol, max_mor;
|
||||
unsigned long wrk_ms;
|
||||
unsigned long buz_wrk;
|
||||
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient mClient(espClient);
|
||||
void reconnect();
|
||||
|
||||
WiFiEventHandler wifiConnectHandler;
|
||||
WiFiEventHandler wifiDisconnectHandler;
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
||||
|
||||
OneWire oneWire(ONE_WIRE_BUS);
|
||||
DallasTemperature tsens(&oneWire);
|
||||
DeviceAddress hol = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
|
||||
DeviceAddress mor = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
|
||||
|
||||
OneButton button(BUTTON, true);
|
||||
void buttonClick();
|
||||
void buttonLongClick();
|
||||
void mqttCallback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.hostname(HOST_NAME);
|
||||
Serial.begin(115200);
|
||||
|
||||
ArduinoOTA.begin();
|
||||
|
||||
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
||||
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
|
||||
|
||||
mClient.setServer("192.168.1.250", 1883);
|
||||
|
||||
pinMode(LED_WF, OUTPUT);
|
||||
pinMode(LED_MQ, OUTPUT);
|
||||
pinMode(LED_WRK, OUTPUT);
|
||||
pinMode(BUTTON, INPUT_PULLUP);
|
||||
pinMode(DOOR_HOL, INPUT_PULLUP);
|
||||
pinMode(DOOR_MOR, INPUT_PULLUP);
|
||||
digitalWrite(LED_WF, LOW);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
digitalWrite(LED_WRK, HIGH);
|
||||
|
||||
tsens.begin();
|
||||
tsens.setResolution(12);
|
||||
tsens.setWaitForConversion(false);
|
||||
|
||||
button.attachClick(buttonClick);
|
||||
button.setPressTicks(3000);
|
||||
button.attachLongPressStart(buttonLongClick);
|
||||
|
||||
buzzer_en = false;
|
||||
buz_wrk = 0;
|
||||
|
||||
EEPROM.begin(sizeof(float) * 2);
|
||||
EEPROM.get(0, max_hol);
|
||||
EEPROM.get(sizeof(float), max_mor);
|
||||
|
||||
connectToWifi();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
static bool measd = false;
|
||||
static uint32_t runSecs = 0;
|
||||
char sVal[6];
|
||||
float t_hol, t_mor;
|
||||
//static bool led_wrk = false;
|
||||
if(wrk_ms < millis())
|
||||
digitalWrite(LED_WRK, HIGH);
|
||||
// if(buz_wrk < millis())
|
||||
// noTone(BUZZER);
|
||||
ArduinoOTA.handle();
|
||||
button.tick();
|
||||
if(!mClient.connected()){
|
||||
reconnect();
|
||||
}
|
||||
if(mClient.connected())
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
else
|
||||
if(!WiFi.isConnected())
|
||||
digitalWrite(LED_MQ, LOW);
|
||||
mClient.loop();
|
||||
if(cRun + 499 < millis()){
|
||||
cRun = millis();
|
||||
if(!measd){
|
||||
tsens.requestTemperatures();
|
||||
}
|
||||
else{
|
||||
if(mClient.connected()){
|
||||
t_hol = tsens.getTempC(hol);
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
wrk_ms = millis() + 300;
|
||||
if(t_hol != DEVICE_DISCONNECTED_C)
|
||||
sprintf(sVal, "%.1f", t_hol);
|
||||
else
|
||||
strcpy(sVal, "NaN");
|
||||
mClient.publish(MAIN_TOPIC "t_hol", sVal);
|
||||
t_mor = tsens.getTempC(mor);
|
||||
if(t_mor != DEVICE_DISCONNECTED_C)
|
||||
sprintf(sVal, "%.1f", t_mor);
|
||||
else
|
||||
strcpy(sVal, "NaN");
|
||||
mClient.publish(MAIN_TOPIC "t_mor", sVal);
|
||||
// if (t_mor > max_mor || t_hol > max_hol){
|
||||
// if(buzzer_en)
|
||||
// if(buz_wrk+1000 < millis())
|
||||
// tone(BUZZER, 1000);
|
||||
// }
|
||||
// else{
|
||||
// buzzer = true;
|
||||
// noTone(BUZZER);
|
||||
// }
|
||||
}
|
||||
runSecs++;
|
||||
if(runSecs % 60){
|
||||
if(mClient.connected()){
|
||||
sprintf(sVal, "%d", runSecs / 60);
|
||||
mClient.publish(MAIN_TOPIC "hol_mins", sVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
if(!WiFi.isConnected()) return;
|
||||
for(uint8_t i = 0; i < 3; i++){
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (mClient.connect(HOST_NAME)) {
|
||||
Serial.println("connected");
|
||||
mClient.subscribe(MAIN_TOPIC "max_hol");
|
||||
mClient.subscribe(MAIN_TOPIC "max_mor");
|
||||
break;
|
||||
// Once connected, publish an announcement...
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(mClient.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 0.5 seconds before retrying
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
||||
String msg = "";
|
||||
for (unsigned int i = 0; i < length; i++) {
|
||||
msg += (char)payload[i];
|
||||
}
|
||||
if(strcmp(topic, MAIN_TOPIC "max_hol") == 0){
|
||||
max_hol = msg.toFloat();
|
||||
EEPROM.put(0, max_hol);
|
||||
EEPROM.commit();
|
||||
}
|
||||
if(strcmp(topic, MAIN_TOPIC "max_mor") == 0){
|
||||
max_mor = msg.toFloat();
|
||||
EEPROM.put(sizeof(float), max_hol);
|
||||
EEPROM.commit();
|
||||
}
|
||||
}
|
||||
|
||||
void connectToWifi() {
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(ssid, password);
|
||||
while (!WiFi.isConnected()){
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
||||
Serial.println("Connected to Wi-Fi.");
|
||||
Serial.print("IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println("Connecting to MQTT...");
|
||||
//reconnect();
|
||||
digitalWrite(LED_WF, LOW);
|
||||
}
|
||||
|
||||
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
Serial.println("Disconnected from Wi-Fi.");
|
||||
digitalWrite(LED_WF, HIGH);
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
}
|
||||
|
||||
void buttonClick() {
|
||||
buzzer_en = false;
|
||||
noTone(BUZZER);
|
||||
}
|
||||
void buttonLongClick() {
|
||||
ESP.reset();
|
||||
}
|
||||
11
Holodolinik_ESP/test/README
Normal file
11
Holodolinik_ESP/test/README
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Unit Testing and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/page/plus/unit-testing.html
|
||||
@@ -9,11 +9,15 @@
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[platformio]
|
||||
default_envs = nano
|
||||
default_envs = uno_ob
|
||||
[env:uno]
|
||||
platform = atmelavr
|
||||
board = uno
|
||||
framework = arduino
|
||||
[env:uno_ob]
|
||||
platform = atmelavr
|
||||
board = uno_ob
|
||||
framework = arduino
|
||||
[env:nodemcuv2]
|
||||
platform = espressif8266
|
||||
board = nodemcuv2
|
||||
|
||||
@@ -129,7 +129,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
if (pub){
|
||||
digitalWrite(LED_WRK, LOW);
|
||||
led_ms = millis();
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "true" : "false");
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "1" : "0");
|
||||
}
|
||||
return state;
|
||||
}
|
||||
@@ -154,9 +154,9 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/kuh/lighttbl_set", 1, false, digitalRead(LAMP) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/kuh/lighttbl_set", 1, false, digitalRead(LAMP) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/kuh/lighttbl_set", 1);
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "1" : "0");
|
||||
digitalWrite(LED_MQ, HIGH);
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
|
||||
strncpy(pl, payload, len);
|
||||
pl[len] = '\0';
|
||||
if(strcmp(topic, "/home/kuh/lighttbl_set") == 0){
|
||||
if (strcmp("true", pl) == 0) switchLight(LAMP, 1, true);//lStat2 = true;
|
||||
if (strncmp("1", pl, 1) == 0) switchLight(LAMP, 1, true);//lStat2 = true;
|
||||
else switchLight(LAMP, 0, true);//lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,5 @@ framework = arduino
|
||||
board_build.ldscript = eagle.flash.2m.ld
|
||||
upload_protocol = espota
|
||||
upload_port = 192.168.1.129
|
||||
lib_deps =
|
||||
joaolopesf/RemoteDebug @ ^3.0.5
|
||||
;lib_deps =
|
||||
; joaolopesf/RemoteDebug @ ^3.0.5
|
||||
@@ -6,11 +6,11 @@
|
||||
#include <Bounce2.h>
|
||||
|
||||
//#define DEBUG_DISABLED true
|
||||
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
|
||||
#ifndef DEBUG_DISABLED // Only if debug is not disabled (for production/release)
|
||||
RemoteDebug Debug;
|
||||
//#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
|
||||
//#ifndef DEBUG_DISABLED // Only if debug is not disabled (for production/release)
|
||||
//RemoteDebug Debug;
|
||||
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
@@ -110,12 +110,12 @@ void setup() {
|
||||
connectToWifi();
|
||||
|
||||
cRun = millis();
|
||||
Debug.begin("SW-BigRoom"); // Initialize the WiFi server
|
||||
// Debug.begin("SW-BigRoom"); // Initialize the WiFi server
|
||||
|
||||
Debug.setResetCmdEnabled(true); // Enable the reset command
|
||||
// Debug.setResetCmdEnabled(true); // Enable the reset command
|
||||
|
||||
Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes)
|
||||
Debug.showColors(true); // Colors
|
||||
// Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes)
|
||||
// Debug.showColors(true); // Colors
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -137,7 +137,7 @@ void loop() {
|
||||
mqttClient.publish("/home/bigroom/millislamp", 0, false, v);
|
||||
//debugI("*Publish Millis: %u");
|
||||
}
|
||||
Debug.handle();
|
||||
// Debug.handle();
|
||||
yield();
|
||||
}
|
||||
|
||||
@@ -149,8 +149,8 @@ bool switchLight(uint8_t nLamp, bool state, bool pub)
|
||||
String topic = "/home/bigroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
//if (pub){
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2);
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
|
||||
// debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2);
|
||||
//}
|
||||
return state;
|
||||
}
|
||||
@@ -173,10 +173,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp1_set", 1, false, lStat1 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp2_set", 1, false, lStat2 ? "true" : "false");
|
||||
mqttClient.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "1" : "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "1" : "0");
|
||||
mqttClient.publish("/home/bigroom/lamp1_set", 1, false, lStat1 ? "1" : "0");
|
||||
mqttClient.publish("/home/bigroom/lamp2_set", 1, false, lStat2 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/bigroom/lamp1_set", 1);
|
||||
mqttClient.subscribe("/home/bigroom/lamp2_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
@@ -194,27 +194,27 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
|
||||
strncpy(s, payload, len);
|
||||
if(String(topic) == "/home/bigroom/lamp1_set"){
|
||||
// if (atoi(payload) == 1) switchLight(R_LED1, 1, false);
|
||||
if (strncmp("true", payload, 4) == 0){
|
||||
if (strncmp("1", payload, 1) == 0){
|
||||
switchLight(R_LED1, true, false);
|
||||
debugI("*Switch from MQTT T1: %s", s);
|
||||
// debugI("*Switch from MQTT T1: %s", s);
|
||||
}
|
||||
//if (atoi(payload) == 1) switchLight(R_LED2, 1, false);
|
||||
//lStat1 = true;
|
||||
else{
|
||||
switchLight(R_LED1, false, false);
|
||||
debugI("*Switch from MQTT F1: %s", s);
|
||||
// debugI("*Switch from MQTT F1: %s", s);
|
||||
}
|
||||
}
|
||||
if(String(topic) == "/home/bigroom/lamp2_set"){
|
||||
if (strncmp("true", payload, 4) == 0){
|
||||
if (strncmp("1", payload, 1) == 0){
|
||||
switchLight(R_LED2, true, false);
|
||||
debugI("*Switch from MQTT T2: %s", s);
|
||||
// debugI("*Switch from MQTT T2: %s", s);
|
||||
}
|
||||
//if (atoi(payload) == 1) switchLight(R_LED2, 1, false);
|
||||
//lStat1 = true;
|
||||
else{
|
||||
switchLight(R_LED2, false, false);
|
||||
debugI("*Switch from MQTT F2: %s", s);
|
||||
// debugI("*Switch from MQTT F2: %s", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ESP_EEPROM.h>
|
||||
#include <OneButton.h>
|
||||
#include <Bounce2.h>
|
||||
//#include <OneButton.h>
|
||||
|
||||
const char* ssid = "wf-home";
|
||||
const char* password = "0ndthnrf";
|
||||
@@ -31,7 +32,8 @@ unsigned long cRun;
|
||||
#define BUTT (5)
|
||||
//bool led = false;
|
||||
|
||||
OneButton button(BUTT);
|
||||
//OneButton button(BUTT);
|
||||
Bounce lSwitch = Bounce();
|
||||
bool switchLight(uint8_t nLamp, int state, bool pub);
|
||||
void connectToWifi();
|
||||
void connectToMqtt();
|
||||
@@ -54,18 +56,18 @@ void oneClick()
|
||||
//lStat1 = !lStat1;
|
||||
}
|
||||
|
||||
void longPress()
|
||||
{
|
||||
//lStat1 = false;
|
||||
switchLight(R_LED, 0, true);
|
||||
mqttClient.publish("/home/bigroom/lamp1_set", 0, false, "false");
|
||||
mqttClient.publish("/home/bigroom/lamp2_set", 0, false, "false");
|
||||
mqttClient.publish("/home/midroom/lamp1_set", 0, false, "false");
|
||||
mqttClient.publish("/home/midroom/lamp2_set", 0, false, "false");
|
||||
mqttClient.publish("/home/smallroom/lamp1_set", 0, false, "false");
|
||||
mqttClient.publish("/home/smallroom/lamp2_set", 0, false, "false");
|
||||
mqttClient.publish("/home/kuh/lighttbl", 0, false, "false");
|
||||
}
|
||||
// void longPress()
|
||||
// {
|
||||
// //lStat1 = false;
|
||||
// switchLight(R_LED, 0, true);
|
||||
// mqttClient.publish("/home/bigroom/lamp1_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/bigroom/lamp2_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/midroom/lamp1_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/midroom/lamp2_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/smallroom/lamp1_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/smallroom/lamp2_set", 0, false, "false");
|
||||
// mqttClient.publish("/home/kuh/lighttbl", 0, false, "false");
|
||||
// }
|
||||
|
||||
void setup() {
|
||||
//Serial.begin(115200);
|
||||
@@ -126,9 +128,11 @@ void setup() {
|
||||
mqttClient.setServer(mqtt_server, 1883);
|
||||
mqttClient.setClientId("SW_Koridor");
|
||||
|
||||
button.attachClick(oneClick);
|
||||
button.attachLongPressStart(longPress);
|
||||
button.setPressTicks(2000);
|
||||
//button.attachClick(oneClick);
|
||||
lSwitch.attach(BUTT);
|
||||
lSwitch.interval(5);
|
||||
// button.attachLongPressStart(longPress);
|
||||
//button.setPressTicks(2000);
|
||||
|
||||
//attachInterrupt(digitalPinToInterrupt(BUTT), sw_func, RISING);
|
||||
|
||||
@@ -140,7 +144,10 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
button.tick();
|
||||
//button.tick();
|
||||
lSwitch.update();
|
||||
if(lSwitch.fell())
|
||||
switchLight(R_LED, !digitalRead(R_LED), true);
|
||||
// if(lStat1 != oldLStat1){
|
||||
// digitalWrite(R_LED, lStat1);
|
||||
// oldLStat1 = lStat1;
|
||||
@@ -165,7 +172,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
EEPROM.put(0, state);
|
||||
EEPROM.commit();
|
||||
//if (pub)
|
||||
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
|
||||
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -193,8 +200,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/kor/lamp1_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
@@ -224,7 +231,7 @@ void onMqttUnsubscribe(uint16_t packetId) {
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/kor/lamp1_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
|
||||
if (strncmp("1", payload, 1) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
|
||||
else switchLight(R_LED, 0, false);//lStat1 = false;
|
||||
//rcv = true;
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
String topic = "/home/midroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
//if (pub)
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -140,10 +140,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/midroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/midroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/midroom/lamp1_set", 1);
|
||||
mqttClient.subscribe("/home/midroom/lamp2_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
@@ -158,11 +158,11 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/midroom/lamp1_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
if (strncmp("1", payload, 1) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
else switchLight(R_LED1, 0, false);//lStat1 = false;
|
||||
}
|
||||
if(strcmp(topic, "/home/midroom/lamp2_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
if (strncmp("1", payload, 1) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
else switchLight(R_LED2, 0, false);//lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
String topic = "/home/smallroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
//if (pub)
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0");
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -154,10 +154,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/smallroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/smallroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/smallroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0");
|
||||
mqttClient.publish("/home/smallroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0");
|
||||
mqttClient.subscribe("/home/smallroom/lamp1_set", 1);
|
||||
mqttClient.subscribe("/home/smallroom/lamp2_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
@@ -172,11 +172,11 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/smallroom/lamp1_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
if (strncmp("1", payload, 1) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
else switchLight(R_LED1, 0, false);//lStat1 = false;
|
||||
}
|
||||
if(strcmp(topic, "/home/smallroom/lamp2_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
if (strncmp("1", payload, 1) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
else switchLight(R_LED2, 0, false);//lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <leds.h>
|
||||
#include <Ticker.h>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#define DEBUG
|
||||
#define WIFI_SSID "wf-home"
|
||||
|
||||
@@ -20,3 +20,5 @@ lib_deps =
|
||||
mathertel/RotaryEncoder @ ^1.3.0
|
||||
robtillaart/PCF8574 @ ^0.2.1
|
||||
robtillaart/I2C_EEPROM @ ^1.3.0
|
||||
ottowinter/ESPAsyncWebServer-esphome @ ^1.3.0
|
||||
ayushsharma82/WebSerial @ ^1.2.0
|
||||
|
||||
Reference in New Issue
Block a user