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
|
||||
Reference in New Issue
Block a user