First commit

This commit is contained in:
2020-08-01 13:47:02 +03:00
commit ac18805ff4
142 changed files with 8325 additions and 0 deletions

5
VT_ESP8266/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

67
VT_ESP8266/.travis.yml Normal file
View File

@@ -0,0 +1,67 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choose one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#
#
# Template #1: General project. Test it using existing `platformio.ini`.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio run
#
# Template #2: The project is intended to be used as a library with examples.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

7
VT_ESP8266/.vscode/extensions.json vendored Normal file
View 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
VT_ESP8266/include/README Normal file
View 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

21
VT_ESP8266/include/leds.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef __LEDS_H__
#define __LEDS_H__
#include <Arduino.h>
#include <PCF8574.h>
class leds
{
private:
int ledPin;
bool inv, state;
int onMS;
unsigned long curMS;
PCF8574 *_pcf;
public:
leds(PCF8574 *pcf, int ledPin, int onms = 300, bool inverse = false);
//~leds();
void start();
void tick();
};
#endif

119
VT_ESP8266/include/main.h Normal file
View File

@@ -0,0 +1,119 @@
#include <Arduino.h>
#include <RotaryEncoder.h>
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
#include <OneButton.h>
#include <PCF8574.h>
#include <LiquidCrystal_PCF8574.h>
#include <ArduinoOTA.h>
#include <pcf_butt.h>
#include <I2C_eeprom.h>
//#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <leds.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#define DEBUG
#define WIFI_SSID "wf-home"
#define WIFI_PASSWORD "0ndthnrf"
#define MY_HOSTNAME "ESP8266_GW"
#define MQTT_SERV "192.168.1.250"
#define MAX_PAGE (4)
#define MAX_HSP (99)
#define MAX_HDB (99)
#define MAX_TWORK (99)
#define MAX_TPAUSE (99)
#define MAX_LCDPAUSE (99)
//#define LIGHT_SENS D0
#define VENT_VANNA (D0)
#define ASDA (D1)
#define ASCL (D2)
#define ENC_A (D3)
#define ENC_B (D4)
#define ONE_WIRE (D5)
#define VENT_TUAL (D6)
#define BUTT_ENC (D7)
#define LIGHT_SENS (0)
#define LED_RED (4)
#define LED_BLUE (5)
#define LED_GREEN (6)
#define LED_YELLOW (7)
#define FLOOD (1)
#define Q_C (3)
#define Q_H (2)
ClosedCube_HDC1080 hdc1080;
unsigned long cRun;
int8_t page;
float hum, temp;
float cTemp, hTemp;
uint8_t pcf_w;
uint8_t LCDtimeout, LCDsec;
bool modeEdit;
uint8_t itemEdit;
uint16_t adc;
float mws;
long curPos;
bool vvannaSent1, vvannaSent0;
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;
const char* mqtt_server = "192.168.1.250";
char v[12];
struct contacts
{
bool QC;
bool QH;
bool Flood;
} wconts;
struct wCnt
{
float QC;
float QH;
} wCounter, oldwCounter;
struct wUst{
uint8_t hSP;
uint8_t hDB;
} wUstavki, oldwUstavki;
struct stTualVent
{
uint8_t tWork;
uint8_t tPause;
} ventUst, oldventUst;
void printSerialNumber();
void buttClick();
void chkCount();
// void callback(char* topic, byte* payload, unsigned int length);
// void reconnect();
void showLCD(uint8_t page, bool chgd);
void chkEnc();
void getTemps();
void buttLongClick();
void chkLight();
void connectToWifi();
void connectToMqtt();
void onWifiConnect(const WiFiEventStationModeGotIP& event);
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttSubscribe(uint16_t packetId, uint8_t qos);
void onMqttUnsubscribe(uint16_t packetId);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void onMqttPublish(uint16_t packetId);
void sendData();

View File

@@ -0,0 +1,18 @@
#ifndef __PCF_BUTT__
#define __PCF_BUTT__
class pcf_butt
{
private:
bool lastState;
bool curState;
public:
pcf_butt();
pcf_butt(bool state);
void update(bool state);
bool chngUp(bool state);
bool chngDown(bool state);
~pcf_butt();
};
#endif

46
VT_ESP8266/lib/README Normal file
View 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

17
VT_ESP8266/platformio.ini Normal file
View File

@@ -0,0 +1,17 @@
; 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:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200
upload_protocol = espota
upload_port = 192.168.1.134

27
VT_ESP8266/src/leds.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include <leds.h>
leds::leds(PCF8574 *pcf, int pinLED, int onms, bool inverse)
{
_pcf = pcf;
ledPin = pinLED;
inv = inverse;
onMS = onms;
state = false;
}
void leds::start()
{
curMS = millis();
state = true;
_pcf->write(ledPin, !inv);
//digitalWrite(ledPin, !inv);
}
void leds::tick()
{
if(state && ((curMS + onMS) < millis())){
state = false;
_pcf->write(ledPin, inv);
//digitalWrite(ledPin, inv);
}
}

743
VT_ESP8266/src/main.cpp Normal file
View File

@@ -0,0 +1,743 @@
#include <main.h>
RotaryEncoder encoder(ENC_B, ENC_A);
OneButton button(BUTT_ENC, true);
LiquidCrystal_PCF8574 lcd(0x27);
PCF8574 pcf(0x38);
I2C_eeprom ee(0x50, 0x1000);
OneWire oneWire(ONE_WIRE);
DallasTemperature sensors(&oneWire);
WiFiClient espClient;
//PubSubClient client(espClient);
pcf_butt c_qc;
pcf_butt c_qh;
pcf_butt c_flood;
//leds ledRed(&pcf, LED_RED, 300, true);
leds ledGreen(&pcf, LED_GREEN, 300, true);
//leds ledBlue(&pcf, LED_BLUE, 100, true);
//leds ledYellow(&pcf, LED_YELLOW, 100, true);
//leds pin0(&pcf, 0, 100, true);
bool led = false;
uint16_t cnt = 0;
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.setCursor(0, 0);
lcd.print(F("Booting"));
WiFi.hostname("VT_ESP");
// WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// if(WiFi.waitForConnectResult() == WL_CONNECTED){
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print(F("Conn.to wf-home"));
// lcd.setCursor(0, 1);
// //lcd.print("IP address: ");
// lcd.println(WiFi.localIP());
// Serial.println(WiFi.localIP());
ArduinoOTA.onStart([]() {
Serial.println(F("ArduinoOTA start"));
});
ArduinoOTA.onEnd([]() {
Serial.println(F("\nArduinoOTA end"));
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println(F("Auth Failed"));
} else if (error == OTA_BEGIN_ERROR) {
Serial.println(F("Begin Failed"));
} else if (error == OTA_CONNECT_ERROR) {
Serial.println(F("Connect Failed"));
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println(F("Receive Failed"));
} else if (error == OTA_END_ERROR) {
Serial.println(F("End Failed"));
}
});
ArduinoOTA.begin();
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(mqtt_server, 1883);
Serial.println("WF");
connectToWifi();
pcf.begin();
hdc1080.begin(0x40);
ee.begin();
button.attachClick(buttClick);
button.attachLongPressStart(buttLongClick);
ee.readBlock(0, (uint8_t *)&wCounter, sizeof(wCounter));
ee.readBlock(8, (uint8_t *)&wUstavki, sizeof(wUstavki));
ee.readBlock(16, (uint8_t *)&ventUst, sizeof(ventUst));
LCDtimeout = ee.readByte(20);
Serial.printf("QCold=%f, QHot=%f\n", wCounter.QC, wCounter.QH);
Serial.printf("H_SP=%d, H_DB=%d\n", wUstavki.hSP, wUstavki.hDB);
Serial.printf("TWork=%d, TPause=%d\n", ventUst.tWork, ventUst.tPause);
Serial.println(LCDtimeout);
//c_qc.update(b & 0x01);
//c_qh.update((b >> 1) & 0x01);
//c_flood.update((b >> 2) & 0x01);
// client.setServer(MQTT_SERV, 1883);
// client.setCallback(callback);
sensors.begin();
sensors.requestTemperatures();
sensors.setResolution(11);
cTemp = sensors.getTempCByIndex(0);
hTemp = sensors.getTempCByIndex(1);
sensors.setWaitForConversion(false);
pinMode(VENT_TUAL, OUTPUT);
pinMode(VENT_VANNA, OUTPUT);
digitalWrite(VENT_TUAL, 1);
digitalWrite(VENT_VANNA, 1);
pcf.write(LED_RED, 1);
pcf.write(LED_GREEN, 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Init completed"));
cRun = 0;
page = 0;
LCDsec = 0;
vvannaSent0 = vvannaSent1 = false;
modeEdit = 0;
} // setup()
// Read the current position of the encoder and print out when changed.
void loop()
{
ArduinoOTA.handle();
// client.loop();
button.tick();
chkEnc();
//ledBlue.tick();
ledGreen.tick();
//ledRed.tick();
//ledYellow.tick();
if(cRun + 99 < millis()){
cRun = millis();
//ledGreen.start();
chkCount();
if(cnt % 10 == 0){
ledGreen.start();
chkLight();
adc = analogRead(A0);
mws = (adc - 186) / 7.303396f;
temp = hdc1080.readTemperature();
hum = hdc1080.readHumidity();
// Serial.print(F("cnt:"));
// Serial.print(cnt);
// Serial.print(F("\t"));
// Serial.print(cRun);
// Serial.print(F("\tT="));
// Serial.print(temp);
// Serial.print(F("C, RH="));
// Serial.print(hum);
// Serial.print("% adc: ");
// Serial.println(adc);
getTemps();
if(++LCDsec >= LCDtimeout){
LCDsec = LCDtimeout;
lcd.setBacklight(0);
modeEdit = 0;
}
showLCD(page, false);
}
if(++cnt == 600){
cnt = 0;
sendData();
}
}
if(hum >= wUstavki.hSP + wUstavki.hDB){
digitalWrite(VENT_VANNA, 0);
if (!vvannaSent1){
vvannaSent1 = true;
vvannaSent0 = false;
mqttClient.publish("/home/vt/vvanna", 1, false, "1");
}
}
else if(hum <= wUstavki.hSP){
digitalWrite(VENT_VANNA, 1);
if (!vvannaSent0){
vvannaSent0 = true;
vvannaSent1 = false;
mqttClient.publish("/home/vt/vvanna", 1, false, "0");
}
}
} // loop ()
void buttClick()
{
Serial.println("Button Click");
LCDsec = 0;
lcd.setBacklight(255);
if(modeEdit == 1){
if (++itemEdit == 2) itemEdit = 0;
switch(page){
case 0:
if(itemEdit == 0) encoder.setPosition(wUstavki.hSP);
else encoder.setPosition(wUstavki.hDB);
break;
case 1:
if(itemEdit == 0) encoder.setPosition(ventUst.tWork);
else encoder.setPosition(ventUst.tPause);
break;
case 2:
if(itemEdit == 0) encoder.setPosition((int)(wCounter.QC*100));
else encoder.setPosition((int)(wCounter.QH*100));
break;
}
}
}
void buttLongClick()
{
Serial.println("Button LongClick");
LCDsec = 0;
lcd.setBacklight(255);
if (page == 3) return;
if (modeEdit == 0){
modeEdit = 1;
itemEdit = 0;
switch(page){
case 0:
encoder.setPosition(wUstavki.hSP);
break;
case 1:
encoder.setPosition(ventUst.tWork);
break;
case 2:
encoder.setPosition((int)(wCounter.QC*100));
break;
}
}
else{
modeEdit = 0;
switch(page){
case 0:
ee.writeBlock(8, (uint8_t *)&wUstavki, sizeof(wUstavki));
break;
case 1:
ee.writeBlock(16, (uint8_t *)&ventUst, sizeof(ventUst));
break;
case 2:
ee.writeBlock(0, (uint8_t *)&wCounter, sizeof(wCounter));
break;
}
encoder.setPosition(page);
}
curPos = encoder.getPosition();
}
void chkCount()
{
byte b = pcf.read8();
if(c_qc.chngDown((b >> Q_C) & 0x01)){
wCounter.QC += 0.01f;
ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
sprintf(v, "%.2f", wCounter.QC);
mqttClient.publish("/home/vt/qcold", 1, false, v);
Serial.printf("QCold=%f\n", wCounter.QC);
//pcf.write(7, led);
//led = !led;
}
if(c_qh.chngDown((b >> Q_H) & 0x01)){
wCounter.QH += 0.01f;
ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
sprintf(v, "%.2f", wCounter.QH);
mqttClient.publish("/home/vt/qhot", 1, false, v);
Serial.printf("QHot=%f\n", wCounter.QH);
//pcf.write(7, led);
//led = !led;
}
if(c_flood.chngDown((b >> FLOOD) & 0x01)){
mqttClient.publish("/home/vt/flood", 1, false, "1");
pcf.write(LED_RED, 0);
//Send MQTT Flood
Serial.printf("Flood\n");
//pcf.write(7, led);
//led = !led;
}
}
// void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Message arrived [");
// Serial.print(topic);
// Serial.print("] ");
// for (unsigned int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
// }
// Serial.println();
// if (strcmp(topic, "/home/vt/h_sp_set") == 0){
// wUstavki.hSP = atoi((char*)payload);
// ee.writeBlock(8, (uint8_t*)&wUstavki, sizeof(wUstavki));
// return;
// }
// if (strcmp(topic, "/home/vt/h_db_set") == 0){
// wUstavki.hDB = atoi((char*)payload);
// ee.writeBlock(8, (uint8_t*)&wUstavki, sizeof(wUstavki));
// return;
// }
// if (strcmp(topic, "/home/vt/v_work_set") == 0){
// ventUst.tWork = atoi((char*)payload);
// ee.writeBlock(16, (uint8_t*)&ventUst, sizeof(ventUst));
// return;
// }
// if (strcmp(topic, "/home/vt/v_pause_set") == 0){
// ventUst.tPause = atoi((char*)payload);
// ee.writeBlock(16, (uint8_t*)&ventUst, sizeof(ventUst));
// return;
// }
// if (strcmp(topic, "/home/vt/QC_set") == 0){
// wCounter.QC = atof((char*)payload);
// ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
// return;
// }
// if (strcmp(topic, "/home/vt/QH_set") == 0){
// wCounter.QH = atof((char*)payload);
// ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
// return;
// }
// if (strcmp(topic, "/home/vt/LCDto_set") == 0){
// LCDtimeout = atoi((char*)payload);
// ee.writeByte(20, LCDtimeout);
// }
// }
// void reconnect() {
// uint8_t cnt = 0;
// // Loop until we're reconnected
// ledRed.start();
// while (!client.connected() && (cnt < 5)) {
// Serial.print(F("Attempting MQTT connection..."));
// // Create a random client ID
// String clientId = "ESP_VT";
// // Attempt to connect
// if (client.connect(clientId.c_str())) {
// Serial.println(F("connected"));
// client.subscribe("/home/vt/h_sp_set");
// client.subscribe("/home/vt/h_db_set");
// client.subscribe("/home/vt/v_work_set");
// client.subscribe("/home/vt/v_pause_set");
// client.subscribe("/home/vt/LCDto_set");
// client.subscribe("/home/vt/QC_set");
// client.subscribe("/home/vt/QH_set");
// } else {
// Serial.print(F("failed, rc="));
// Serial.print(client.state());
// }
// cnt++;
// }
// }
void showLCD(uint8_t page, bool chgd)
{
char c = ' ';
if(chgd) c = '*';
switch (page)
{
case 0:
lcd.setCursor(0, 0);
lcd.printf("V H%4.1f%% T%4.1fC%c", hum, temp, c);
lcd.setCursor(0, 1);
lcd.printf("SP:%2d DB:%2d ", wUstavki.hSP, wUstavki.hDB);
if (modeEdit == 1){
lcd.cursor();
lcd.blink();
if(itemEdit == 0)
lcd.setCursor(4, 1);
else
lcd.setCursor(10, 1);
}
else{
lcd.noCursor();
lcd.noBlink();
}
break;
case 1:
lcd.setCursor(0, 0);
lcd.printf("TW:%2ds TP:%2ds %c", ventUst.tWork, ventUst.tPause, c);
lcd.setCursor(0, 1);
lcd.printf("TC:%4.1fcTH:%4.1fc", cTemp, hTemp);
if (modeEdit == 1){
lcd.cursor();
lcd.blink();
if(itemEdit == 0)
lcd.setCursor(4, 0);
else
lcd.setCursor(11, 0);
}
else{
lcd.noCursor();
lcd.noBlink();
}
break;
case 2:
lcd.setCursor(0, 0);
lcd.printf("QC: %7.2f m3 %c", wCounter.QC, c);
lcd.setCursor(0, 1);
lcd.printf("QH: %7.2f m3 ", wCounter.QH);
if (modeEdit == 1){
lcd.cursor();
lcd.blink();
if(itemEdit == 0)
lcd.setCursor(10, 0);
else
lcd.setCursor(10, 1);
}
else{
lcd.noCursor();
lcd.noBlink();
}
break;
case 3:
lcd.setCursor(0, 0);
lcd.printf("ADC: %4d %c", adc, c);
lcd.setCursor(0, 1);
lcd.printf("P: %5.2f mws ", mws);
break;
default:
break;
}
//Serial.println(F("ShLCD"));
}
void chkEnc()
{
encoder.tick();
int newPos = encoder.getPosition();
if (curPos != newPos) {
LCDsec = 0;
lcd.setBacklight(255);
// if((newPos < 0) || (newPos > 3)){
// encoder.setPosition(curPos);
// return;
// }
// Serial.println();
curPos = newPos;
//Serial.print(curPos);
if(modeEdit == 1){
if (newPos < 0) encoder.setPosition(0);
else switch(page){
case 0:
if(itemEdit == 0){
if(curPos > MAX_HSP){
encoder.setPosition(MAX_HSP);
curPos = MAX_HSP;
}
else
wUstavki.hSP = curPos;
}
else{
if(curPos > MAX_HDB){
encoder.setPosition(MAX_HDB);
curPos = MAX_HDB;
}
else
wUstavki.hDB = curPos;
}
break;
case 1:
if(itemEdit == 0){
if(curPos > MAX_TWORK){
encoder.setPosition(MAX_TWORK);
curPos = MAX_TWORK;
}
else
ventUst.tWork = curPos;
}
else{
if(curPos > MAX_TPAUSE){
encoder.setPosition(MAX_TPAUSE);
curPos = MAX_TPAUSE;
}
else
ventUst.tPause = curPos;
}
break;
case 2:
if(itemEdit == 0){
if(curPos > 999999){
encoder.setPosition(999999);
curPos = 999999;
}
else
wCounter.QC = curPos / 100.0f;
}
else{
if(curPos > 999999){
encoder.setPosition(999999);
curPos = 999999;
}
else
wCounter.QH = curPos / 100.0f;
}
break;
}
}
else{
page = curPos;
if(page > MAX_PAGE - 1) page = curPos = 0;
if(page < 0) page = curPos = MAX_PAGE - 1;
}
encoder.setPosition(curPos);
showLCD(page, false);
// Serial.print(page);
// Serial.println();
} // if
}
void getTemps()
{
static uint8_t cnt = 0;
switch (cnt)
{
case 0:
sensors.requestTemperatures();
break;
case 1:
cTemp = sensors.getTempCByIndex(0);
break;
case 2:
hTemp = sensors.getTempCByIndex(1);
break;
}
if(++cnt > 2){
cnt = 0;
}
}
void chkLight()
{
static int sec = 0;
static unsigned long currun = millis();
static bool lOn = false;
//Lamp is on
if(!pcf.read(LIGHT_SENS)){
if((currun + 999 < millis()) && !lOn){
sec++;
currun = millis();
#ifdef DEBUG
Serial.print("Sec On: ");Serial.println(sec);
Serial.print("Lamp State: ");Serial.println(lOn);
#endif
}
if((sec >= ventUst.tPause) && !lOn){
//lamp on
#ifdef DEBUG
Serial.print("Lamp On: ");Serial.println(sec);
#endif
lOn = true;
digitalWrite(VENT_TUAL, LOW);
mqttClient.publish("/home/vt/vtual", 1, false, "1");
//sec = 0;
}
if(lOn) sec = 0;
}
else{
if(lOn == true){
if(currun + 999 < millis()){
sec++;
currun = millis();
#ifdef DEBUG
Serial.print("Sec Off: ");Serial.println(sec);
Serial.print("Lamp State: ");Serial.println(lOn);
#endif
}
if(sec >= ventUst.tWork){
#ifdef DEBUG
Serial.print("Lamp Off: ");Serial.println(sec);
#endif
lOn = false;
digitalWrite(VENT_TUAL, HIGH);
mqttClient.publish("/home/vt/vtual", 1, false, "0");
sec = 0;
}
}
else{
sec = 0;
}
}
}
void connectToWifi() {
Serial.println(F("Connecting to Wi-Fi..."));
//Serial1.flush();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println(F("Connecting to MQTT..."));
//Serial1.flush();
mqttClient.connect();
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println(F("Connected to Wi-Fi."));
Serial.print(F("IP: "));
//Serial1.flush();
Serial.println(WiFi.localIP());
pcf.write(LED_YELLOW, 0);
connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println(F("Disconnected from Wi-Fi."));
//Serial1.flush();
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
pcf.write(LED_YELLOW, 1);
}
void onMqttConnect(bool sessionPresent) {
Serial.println(F("Connected to MQTT."));
Serial.print(F("Session present: "));
//Serial1.flush();
Serial.println(sessionPresent);
//digitalWrite(B_LED, HIGH);
//digitalWrite(R_LED, LOW);
//uint16_t packetIdSub =
// //Serial1.print("Subscribing Lamp1, packetId: ");
// //Serial1.println(packetIdSub);
//packetIdSub =
mqttClient.subscribe("/home/vt/qhot_set", 1);
mqttClient.subscribe("/home/vt/qcold_set", 1);
mqttClient.subscribe("/home/vt/tpause_set", 1);
mqttClient.subscribe("/home/vt/twork_set", 1);
mqttClient.subscribe("/home/vt/hsp_set", 1);
mqttClient.subscribe("/home/vt/hdb_set", 1);
sprintf(v, "%.2f", wCounter.QH);
mqttClient.publish("/home/vt/qhot", 1, false, v);
sprintf(v, "%.2f", wCounter.QC);
mqttClient.publish("/home/vt/qcold", 1, false, v);
itoa(wUstavki.hDB, v, 10);
mqttClient.publish("/home/vt/hdb", 1, false, v);
itoa(wUstavki.hSP, v, 10);
mqttClient.publish("/home/vt/hsp", 1, false, v);
itoa(ventUst.tWork, v, 10);
mqttClient.publish("/home/vt/twork", 1, false, v);
itoa(ventUst.tPause, v, 10);
mqttClient.publish("/home/vt/tpause", 1, false, v);
////Serial1.print("Subscribing Lamp2, packetId: ");
////Serial1.println(packetIdSub);
////Serial1.println("Publishing at Lamp 1");
//mqttClient.publish("/home/kor/lamp2", 1, false, lStat2 ? "1" : "0");
////Serial1.println("Publishing at Lamp 2");
pcf.write(LED_BLUE, 0);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
//digitalWrite(B_LED, LOW);
//digitalWrite(R_LED, HIGH);
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
pcf.write(LED_BLUE, 0);
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
// //Serial1.println("Subscribe acknowledged.");
// //Serial1.print(" packetId: ");
// //Serial1.println(packetId);
// //Serial1.print(" qos: ");
// //Serial1.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
// //Serial1.println("Unsubscribe acknowledged.");
// //Serial1.print(" packetId: ");
// //Serial1.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
if(strcmp(topic, "/home/vt/qhot_set") == 0){
wCounter.QH = atof(payload);
ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
sprintf(v, "%.2f", wCounter.QH);
mqttClient.publish("/home/vt/qhot", 1, false, v);
} else
if(strcmp(topic, "/home/vt/qcold_set") == 0){
wCounter.QC = atof(payload);
ee.writeBlock(0, (uint8_t*)&wCounter, sizeof(wCounter));
sprintf(v, "%.2f", wCounter.QC);
mqttClient.publish("/home/vt/qcold", 1, false, v);
} else
if(strcmp(topic, "/home/vt/tpause_set") == 0){
ventUst.tPause = atoi(payload);
ee.writeBlock(16, (uint8_t*)&ventUst, sizeof(wCounter));
itoa(ventUst.tPause, v, 10);
mqttClient.publish("/home/vt/tpause", 1, false, v);
} else
if(strcmp(topic, "/home/vt/twork_set") == 0){
ventUst.tWork = atoi(payload);
ee.writeBlock(16, (uint8_t*)&ventUst, sizeof(wCounter));
itoa(ventUst.tWork, v, 10);
mqttClient.publish("/home/vt/twork", 1, false, v);
} else
if(strcmp(topic, "/home/vt/hsp_set") == 0){
wUstavki.hSP = atoi(payload);
ee.writeBlock(8, (uint8_t*)&wUstavki, sizeof(wCounter));
itoa(wUstavki.hSP, v, 10);
mqttClient.publish("/home/vt/hsp", 1, false, v);
} else
if(strcmp(topic, "/home/vt/hdb_set") == 0){
wUstavki.hDB = atoi(payload);
ee.writeBlock(8, (uint8_t*)&wUstavki, sizeof(wCounter));
itoa(wUstavki.hDB, v, 10);
mqttClient.publish("/home/vt/hdb", 1, false, v);
}
}
void onMqttPublish(uint16_t packetId) {
//Serial1.println("Publish acknowledged.");
//Serial1.print(" packetId: ");
//Serial1.println(packetId);
//g_led.start();
}
void sendData(){
sprintf(v, "%.2f", temp);
mqttClient.publish("/home/vt/vtemp", 1, false, v);
sprintf(v, "%.2f", hum);
mqttClient.publish("/home/vt/vhum", 1, false, v);
sprintf(v, "%.2f", cTemp);
mqttClient.publish("/home/vt/tcold", 1, false, v);
sprintf(v, "%.2f", hTemp);
mqttClient.publish("/home/vt/thot", 1, false, v);
sprintf(v, "%.2f", mws);
mqttClient.publish("/home/vt/wpress", 1, false, v);
Serial.print("millis: "); Serial.print(millis());
Serial.println(" Sent data");
}

View File

@@ -0,0 +1,49 @@
#include <Arduino.h>
#include "pcf_butt.h"
pcf_butt::pcf_butt()
{
curState = false;
}
pcf_butt::pcf_butt(bool state)
{
curState = state;
}
bool pcf_butt::chngUp(bool state)
{
curState = state;
if((lastState != curState) && curState == true){
Serial.println("Down");
lastState = curState;
return true;
}
else {
lastState = curState;
return false;
}
}
bool pcf_butt::chngDown(bool state)
{
curState = state;
if((lastState != curState) && curState == false){
Serial.println("Down");
lastState = curState;
return true;
}
else {
lastState = curState;
return false;
}
}
void pcf_butt::update(bool state)
{
curState = state;
}
pcf_butt::~pcf_butt()
{
}

11
VT_ESP8266/test/README Normal file
View File

@@ -0,0 +1,11 @@
This directory is intended for PIO 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 PIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html