summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore8
-rw-r--r--platformio.ini27
-rw-r--r--src/main.cpp291
-rw-r--r--src/setenv-master.h10
4 files changed, 336 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7c89bbd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+.pio
+.vscode/
+include/
+lib/
+test/
+setenv.h
+setenvSource.h
+.DS_Store
diff --git a/platformio.ini b/platformio.ini
new file mode 100644
index 0000000..3421e22
--- /dev/null
+++ b/platformio.ini
@@ -0,0 +1,27 @@
+; 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:esp32-c3-devkitm-1]
+; platform = espressif32
+; board = esp32-c3-devkitm-1
+
+[env:esp32-c3-devkitc-02]
+platform = espressif32
+board = esp32-c3-devkitc-02
+
+framework = arduino
+
+monitor_speed = 115200
+upload_speed = 921600 ;460800 ;
+
+lib_deps =
+ milesburton/DallasTemperature
+ knolleary/PubSubClient
+ \ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..a08b902
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,291 @@
+#include <Arduino.h>
+#include <WiFi.h>
+#include <OneWire.h>
+#include <PubSubClient.h>
+#include <DallasTemperature.h>
+
+const uint8_t wire_bus = 1;
+OneWire oneWire(wire_bus);
+
+DallasTemperature DS18B20(&oneWire);
+
+DeviceAddress s0, s1, s2, s3, s4, s5;
+
+#include "setenv.h"
+
+WiFiClient espClient;
+PubSubClient client(espClient);
+
+//------------------------------------------------
+const char *ssid = WiFi_SSID;
+const char *pass = WiFi_PASS;
+
+const char *mqtt_client_name = MQTT_CLIENT_NAME;
+const char *mqtt_client_topic = MQTT_CLIENT_TOPIC;
+const char *mqtt_user = MQTT_USER;
+const char *mqtt_pass = MQTT_PASS;
+const char *mqtt_server = MQTT_SERVER;
+const char *mqtt_port = MQTT_PORT;
+
+const char *outTopicTemp = "/data";
+
+//------------------------------------------------
+uint8_t countConnect = 20; // кол-во попыток соединения
+uint16_t countPause = 500; // пауза между попытками
+uint32_t timeSleep = 30000000; // время сна
+uint16_t TimeBeforeBedtime = 500; // время до засыпания
+uint8_t countMaxSleep = 120; // не передавать данные не более
+ // countMaxSleep х timeSleep (60мин)
+
+RTC_DATA_ATTR struct
+{
+ float t1 = 0; // температура 1
+ float t2 = 0; // температура 2
+ float t3 = 0; // температура 3
+ float t4 = 0; // температура 4
+ // float t5 = 0; // температура 5
+ // float t6 = 0; // температура 6
+} data;
+
+RTC_DATA_ATTR bool flagNotWork = false;
+
+//-----------------------------------
+inline bool mqtt_subscribe(PubSubClient &client, const String &topic)
+{
+ Serial.print("Subscribing to: ");
+ Serial.println(topic);
+ return client.subscribe(topic.c_str());
+}
+
+//-----------------------------------
+inline bool mqtt_publish(PubSubClient &client, const String &topic, const String &value)
+{
+ Serial.print(topic);
+ Serial.print(" = ");
+ Serial.println(value);
+ return client.publish(topic.c_str(), value.c_str());
+}
+
+//-----------------------------------
+void mqttDataOut(float temp1, float temp2, float temp3, float temp4) //, float temp5, float temp6)
+{
+ String topic = "/";
+ String a = "";
+ a += data.t1;
+ a += ";";
+ a += data.t2;
+ a += ";";
+ a += data.t3;
+ a += ";";
+ a += data.t4;
+ a += ";";
+ // a += data.t5;
+ // a += ",";
+ // a += data.t6;
+ topic += mqtt_client_topic;
+ topic += outTopicTemp;
+ mqtt_publish(client, topic, a);
+}
+
+//-----------------------------------
+bool reconnect()
+{
+ client.setServer(mqtt_server, String(mqtt_port).toInt());
+
+ Serial.print("MQTT connect : ");
+ Serial.println(mqtt_server);
+
+ while (!(client.connect(mqtt_client_name, mqtt_user, mqtt_pass)) && countConnect--)
+ {
+ Serial.print(countConnect);
+ Serial.print('>');
+ delay(countPause);
+ }
+
+ if (client.connected())
+ {
+ Serial.println("MQTT connected - OK !");
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+//-----------------------------------
+bool setupWiFi(const char *wifi_ssid, const char *wifi_pass)
+{
+ WiFi.begin(wifi_ssid, wifi_pass);
+
+ Serial.print("Setup WiFi: ");
+ Serial.println(ssid);
+
+ while ((WiFi.status() != WL_CONNECTED) && countConnect--)
+ {
+ Serial.print(countConnect);
+ Serial.print('>');
+ delay(countPause);
+ }
+
+ if (WiFi.status() == WL_CONNECTED)
+ {
+ // индикация IP
+ Serial.print("\nWiFi connected - OK !\n");
+ Serial.println(WiFi.localIP());
+ // индикация силы сигнала
+ int8_t dBm = WiFi.RSSI();
+ Serial.print("RSSI dBm = ");
+ Serial.println(dBm);
+ uint8_t quality_RSSI = 2 * (dBm + 100);
+ if (quality_RSSI >= 100)
+ quality_RSSI = 100;
+ Serial.print("RSSI % = ");
+ Serial.println(quality_RSSI);
+ Serial.println("=================");
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+//-----------------------------------
+// медиана на 3 значения со своим буфером
+uint16_t medianRoom(uint16_t newValRoom)
+{
+ RTC_DATA_ATTR static uint16_t bufRoom[3] = {0, 0, 0};
+ RTC_DATA_ATTR static byte countRoom = 0;
+
+ Serial.println();
+ Serial.print(countRoom);
+ Serial.print(" - ");
+ Serial.print(bufRoom[0]);
+ Serial.print("...");
+ Serial.print(bufRoom[1]);
+ Serial.print("...");
+ Serial.print(bufRoom[2]);
+ Serial.println();
+
+ if (!(bufRoom[0] + bufRoom[1] + bufRoom[2]))
+ bufRoom[0] = bufRoom[1] = bufRoom[2] = newValRoom;
+
+ bufRoom[countRoom] = newValRoom;
+ if (countRoom++ >= 2)
+ countRoom = 0;
+ uint16_t dataRoom = (max(bufRoom[0], bufRoom[1]) == max(bufRoom[1], bufRoom[2]))
+ ? max(bufRoom[0], bufRoom[2])
+ : max(bufRoom[1], min(bufRoom[0], bufRoom[2]));
+ // return expRunningAverage(data);
+ return dataRoom;
+}
+
+//-----------------------------------
+void printAddress(DeviceAddress deviceAddress)
+{
+ for (uint8_t i = 0; i < 8; i++)
+ {
+ // zero pad the address if necessary
+ if (deviceAddress[i] < 16)
+ Serial.print("0");
+ Serial.print(deviceAddress[i], HEX);
+ }
+}
+
+//-----------------------------------
+void requestSensors()
+{
+ oneWire.reset_search();
+ DS18B20.begin();
+
+ if (!DS18B20.getAddress(s0, 0))
+ Serial.println("Unable to find address for Device s0");
+ if (!DS18B20.getAddress(s1, 1))
+ Serial.println("Unable to find address for Device s1");
+ if (!DS18B20.getAddress(s2, 2))
+ Serial.println("Unable to find address for Device s2");
+ if (!DS18B20.getAddress(s3, 3))
+ Serial.println("Unable to find address for Device s3");
+ // if (!DS18B20.getAddress(s4, 4))
+ // Serial.println("Unable to find address for Device s4");
+ // if (!DS18B20.getAddress(s5, 5))
+ // Serial.println("Unable to find address for Device s5");
+}
+
+//-----------------------------------
+float printTemperature(DeviceAddress deviceAddress)
+{
+ float tempC = DS18B20.getTempC(deviceAddress);
+ if (tempC == DEVICE_DISCONNECTED_C)
+ {
+ Serial.println("Error: Could not read temperature data");
+ tempC = 00.00;
+ return tempC;
+ }
+
+ Serial.print("Device Address: ");
+ printAddress(deviceAddress);
+
+ Serial.print(" Temp C: ");
+ Serial.println(tempC);
+ return tempC;
+}
+
+//-----------------------------------
+void readData()
+{
+ Serial.print("\nRequesting temperatures... ");
+ uint32_t t = millis();
+ DS18B20.requestTemperatures();
+ Serial.println(millis() - t);
+
+ data.t1 = printTemperature(s0);
+ data.t2 = printTemperature(s1);
+ data.t3 = printTemperature(s2);
+ data.t4 = printTemperature(s3);
+ // data.t5 = printTemperature(s4);
+ // data.t6 = printTemperature(s5);
+}
+
+//-----------------------------------
+void setup()
+{
+ Serial.begin(115200);
+
+ requestSensors();
+ readData();
+
+ if (!setupWiFi(ssid, pass))
+ {
+ Serial.println("\nSleep WiFi !!!");
+ flagNotWork = true;
+ esp_deep_sleep(timeSleep);
+ }
+ else
+ {
+ flagNotWork = false;
+ }
+
+ if (!reconnect())
+ {
+ Serial.println("\nSleep MQTT !!!");
+ flagNotWork = true;
+ esp_deep_sleep(timeSleep);
+ }
+ else
+ {
+ flagNotWork = false;
+ }
+
+ mqttDataOut(data.t1, data.t2, data.t3, data.t4); //, data.t5, data.t6);
+
+ Serial.println("=================\n");
+ Serial.println("/////////////////////////////\n");
+ Serial.flush();
+
+ delay(TimeBeforeBedtime);
+ esp_deep_sleep(timeSleep);
+}
+
+void loop() {} \ No newline at end of file
diff --git a/src/setenv-master.h b/src/setenv-master.h
new file mode 100644
index 0000000..2e4e56d
--- /dev/null
+++ b/src/setenv-master.h
@@ -0,0 +1,10 @@
+// Установка переменных:
+
+const char *WiFi_SSID = "";
+const char *WiFi_PASS = "";
+const char *MQTT_CLIENT_NAME = "";
+const char *MQTT_CLIENT_TOPIC = "";
+const char *MQTT_USER = "";
+const char *MQTT_PASS = "";
+const char *MQTT_SERVER = "";
+const char *MQTT_PORT = ""; \ No newline at end of file