стабилизация кода
This commit is contained in:
5
Devices/rancho/meteo-out/.gitignore
vendored
Normal file
5
Devices/rancho/meteo-out/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
10
Devices/rancho/meteo-out/.vscode/extensions.json
vendored
Normal file
10
Devices/rancho/meteo-out/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
@@ -20,3 +20,9 @@ lib_deps =
|
||||
isobit/ArduinoNATS@^1.1.2
|
||||
; https://gitflic.ru/project/vpodberezsky/hal9000.git#1.1
|
||||
https://github.com/GyverLibs/FastBot2#1.2.4
|
||||
|
||||
arduino-libraries/NTPClient@^3.2.1
|
||||
; bblanchon/ArduinoJson@^7.2.1
|
||||
milesburton/DallasTemperature@^3.11.0
|
||||
https://gitflic.ru/project/vpodberezsky/hal9000.git#1.1
|
||||
; knolleary/PubSubClient@^2.8
|
||||
|
||||
263
Devices/rancho/meteo-out/src/main.cpp
Normal file
263
Devices/rancho/meteo-out/src/main.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ArduinoNATS.h>
|
||||
#include <FastBot2.h>
|
||||
#include <OneWire.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
FastBot2 bot;
|
||||
|
||||
String WIFI_SSID[] = {
|
||||
"fazenda",
|
||||
"paranetm",
|
||||
};
|
||||
|
||||
#define hello_str "MeteoOut is online [20251203]"
|
||||
|
||||
#define WIFI_PASS "78515169"
|
||||
|
||||
#define BOT_TOKEN "5363807811:AAHRz_rDIY5AV2NpEzS9jy0xXwjScAJyUOs"
|
||||
#define CHAT_ID "317081354"
|
||||
|
||||
#define ONE_WIRE_BUS D3
|
||||
|
||||
OneWire oneWire(ONE_WIRE_BUS);
|
||||
DallasTemperature sensors(&oneWire);
|
||||
|
||||
WiFiClient clientNats;
|
||||
NATS nats(
|
||||
&clientNats,
|
||||
"nats.paranet.ru", NATS_DEFAULT_PORT);
|
||||
|
||||
float currentT;
|
||||
|
||||
void getTemper()
|
||||
{
|
||||
Serial.print("Requesting temperatures...");
|
||||
sensors.requestTemperatures(); // Send the command to get temperatures
|
||||
Serial.println("DONE");
|
||||
// After we got the temperatures, we can print them here.
|
||||
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
|
||||
float tempC = sensors.getTempCByIndex(0);
|
||||
|
||||
// Check if reading was successful
|
||||
if (tempC != DEVICE_DISCONNECTED_C)
|
||||
{
|
||||
Serial.print("Temperature for the device 1 (index 0) is: ");
|
||||
Serial.println(tempC);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Error: Could not read temperature data");
|
||||
}
|
||||
|
||||
currentT = tempC;
|
||||
}
|
||||
|
||||
String bot_last_message;
|
||||
String bot_chat_id;
|
||||
|
||||
void updateh(fb::Update &u)
|
||||
{
|
||||
// Serial.println("NEW MESSAGE");
|
||||
// Serial.println(u.message().from().username());
|
||||
// Serial.println(u.message().text());
|
||||
|
||||
if (u.message().text() == "update meteoout" && u.message().document().name().endsWith(".bin"))
|
||||
{ // .bin - значит это ОТА
|
||||
bot.sendMessage(fb::Message("OTA begin", u.message().chat().id()));
|
||||
|
||||
// между downloadFile и updateFlash/updateFS/writeTo не должно быть отправки сообщений!
|
||||
// OTA обновление тип 1
|
||||
// bot.updateFlash(u.message().document(), u.message().chat().id());
|
||||
|
||||
// OTA обновление тип 2
|
||||
fb::Fetcher fetch = bot.downloadFile(u.message().document().id());
|
||||
if (fetch)
|
||||
{
|
||||
if (fetch.updateFlash())
|
||||
{
|
||||
Serial.println("OTA done");
|
||||
bot.sendMessage(fb::Message("OTA done", u.message().chat().id()), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("OTA error");
|
||||
bot.sendMessage(fb::Message("OTA error", u.message().chat().id()), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String text = u.message().text();
|
||||
String id = u.message().chat().id();
|
||||
bot_last_message = text;
|
||||
bot_chat_id = id;
|
||||
|
||||
// #1
|
||||
// отправить обратно в чат (эхо)
|
||||
// bot.sendMessage(fb::Message(u.message().text(), u.message().chat().id()));
|
||||
|
||||
// #2
|
||||
// String text = u.message().text();
|
||||
// text += " - ответ";
|
||||
// bot.sendMessage(fb::Message(text, u.message().chat().id()));
|
||||
|
||||
// #3
|
||||
// fb::Message msg;
|
||||
// msg.text = u.message().text();
|
||||
// msg.text += " - ответ";
|
||||
// msg.chatID = u.message().chat().id();
|
||||
// bot.sendMessage(msg);
|
||||
}
|
||||
|
||||
void nats_blink_handler(NATS::msg msg)
|
||||
{
|
||||
// nats.publish(msg.reply, msg.data);
|
||||
String str = F("reply\n");
|
||||
str += msg.reply;
|
||||
str += F("\ndata\n");
|
||||
str += msg.data;
|
||||
Serial.println(str);
|
||||
// _telegram.send(str, _config.CHAT_ID);
|
||||
|
||||
// int count = atoi(msg.data);
|
||||
// while (count-- > 0)
|
||||
// {
|
||||
// digitalWrite(LED_BUILTIN, LOW);
|
||||
// delay(100);
|
||||
// digitalWrite(LED_BUILTIN, HIGH);
|
||||
// delay(100);
|
||||
// }
|
||||
}
|
||||
|
||||
void nats_on_connect()
|
||||
{
|
||||
nats.subscribe("switches/k1", nats_blink_handler);
|
||||
}
|
||||
|
||||
void wifi_check()
|
||||
{
|
||||
if (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
for (byte i = 0; i < WIFI_SSID->length(); i++)
|
||||
{
|
||||
Serial.println("");
|
||||
Serial.print("try connected... ");
|
||||
Serial.println(WIFI_SSID[i]);
|
||||
byte tries = 20;
|
||||
WiFi.begin(WIFI_SSID[i], WIFI_PASS);
|
||||
while (tries-- > 0 && WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
yield();
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
Serial.println("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(1000);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.println(F("SYSTEM: HAL9000"));
|
||||
Serial.println(F("Module: MeteoOut"));
|
||||
|
||||
sensors.begin();
|
||||
sensors.setResolution(9);
|
||||
getTemper();
|
||||
|
||||
wifi_check();
|
||||
Serial.println("WiFi connected");
|
||||
|
||||
// ============
|
||||
bot.attachUpdate(updateh); // подключить обработчик обновлений
|
||||
bot.setToken(F(BOT_TOKEN)); // установить токен
|
||||
|
||||
// режим опроса обновлений. Самый быстрый - Long
|
||||
// особенности читай тут в самом низу
|
||||
// https://github.com/GyverLibs/FastBot2/blob/main/docs/3.start.md
|
||||
|
||||
// bot.setPollMode(fb::Poll::Sync, 4000); // умолч
|
||||
// bot.setPollMode(fb::Poll::Async, 4000);
|
||||
bot.setPollMode(fb::Poll::Long, 60000);
|
||||
|
||||
// поприветствуем админа
|
||||
bot.sendMessage(fb::Message(hello_str, CHAT_ID));
|
||||
|
||||
Serial.print("connect NATS...");
|
||||
nats.on_connect = nats_on_connect;
|
||||
if (nats.connect())
|
||||
{
|
||||
Serial.println(" OK");
|
||||
bot.sendMessage(fb::Message("NATS connected.", CHAT_ID));
|
||||
}
|
||||
}
|
||||
|
||||
ulong ticks_1000 = 0;
|
||||
ulong ticks_10000 = 0;
|
||||
ulong ticks_10mins = 0;
|
||||
ulong ticks_60mins = 0;
|
||||
|
||||
void telegram_handler()
|
||||
{
|
||||
String text = bot_last_message;
|
||||
if (text.length() > 0)
|
||||
{
|
||||
Serial.println(F("CMD: ") + text);
|
||||
Serial.println(F("from chat: ") + bot_chat_id);
|
||||
|
||||
if (text == "/get-meteo-out")
|
||||
{
|
||||
String msg = "T: " + (String)currentT;
|
||||
bot.sendMessage(fb::Message(msg, bot_chat_id));
|
||||
Serial.println("send to chat > " + bot_chat_id);
|
||||
}
|
||||
|
||||
bot_last_message = "";
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (millis() - ticks_1000 > 1000)
|
||||
{
|
||||
ticks_1000 = millis();
|
||||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
|
||||
wifi_check();
|
||||
telegram_handler();
|
||||
}
|
||||
if (millis() - ticks_10000 > 10000)
|
||||
{
|
||||
ticks_10000 = millis();
|
||||
getTemper();
|
||||
// Serial.println("t = " + (String)currentT);
|
||||
}
|
||||
if (millis() - ticks_10mins > 600000UL)
|
||||
{
|
||||
ticks_10mins = millis();
|
||||
Serial.println("send to nats... ");
|
||||
nats.publish("/sensors/meteoout", ((String)currentT).c_str());
|
||||
Serial.println("OK");
|
||||
}
|
||||
if (millis() - ticks_60mins > 3600000UL)
|
||||
{
|
||||
ticks_60mins = millis();
|
||||
bot.sendMessage(fb::Message("MeteoOut T: " + (String)currentT, bot_chat_id));
|
||||
}
|
||||
|
||||
bot.tick();
|
||||
nats.process();
|
||||
yield();
|
||||
}
|
||||
0
Devices/rancho/meteo-out/src/main.h
Normal file
0
Devices/rancho/meteo-out/src/main.h
Normal file
Reference in New Issue
Block a user