|
|
|
@ -1,3 +1,17 @@
|
|
|
|
|
//{"amount":40}
|
|
|
|
|
//1 офис
|
|
|
|
|
//aastudio/act/94B97EACF00C/set/0
|
|
|
|
|
|
|
|
|
|
//2белая
|
|
|
|
|
// 14:06:40.942 -> aastudio/act/24A160461D08/set/0
|
|
|
|
|
// 14:06:40.942 -> aastudio/act/24A160461D08/data/0
|
|
|
|
|
// 14:06:40.975 -> aastudio/act/24A160461D08/status
|
|
|
|
|
|
|
|
|
|
//3 кухня
|
|
|
|
|
// 19:38:23.635 -> aastudio/act/94B97E929DE0/set/0
|
|
|
|
|
// 19:38:23.669 -> aastudio/act/94B97E929DE0/data/0
|
|
|
|
|
// 19:38:23.669 -> aastudio/act/94B97E929DE0/status
|
|
|
|
|
|
|
|
|
|
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
|
|
|
|
#include <Preferences.h> //Сохранение настроек хеша прошивки
|
|
|
|
|
#include <HTTPClient.h>
|
|
|
|
@ -16,11 +30,11 @@
|
|
|
|
|
#define Resist 33
|
|
|
|
|
|
|
|
|
|
#define TRIGGER_PIN 39
|
|
|
|
|
unsigned int VersionSW = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int VersionSW = 7;
|
|
|
|
|
|
|
|
|
|
//3 - Первый код для анемостата
|
|
|
|
|
// 5 - перед установкой
|
|
|
|
|
//7 - выполнение команд последовательно
|
|
|
|
|
|
|
|
|
|
WiFiManager wm; // обьект менеджера
|
|
|
|
|
Preferences OTApreferences; //Обьект хранения настроек хеша прошивки
|
|
|
|
@ -45,7 +59,7 @@ String setTopic = "aastudio/act/" + macc + "/set/0";
|
|
|
|
|
String willTopicTemp = "aastudio/act/" + macc + "/status";
|
|
|
|
|
const PROGMEM char *mqttLogin = "AA_Lab", *mqttPass = "automation.art$";
|
|
|
|
|
|
|
|
|
|
byte setPercent = 0, setSpeedAnemo = 0;
|
|
|
|
|
byte setPercent = 0, prevPercent=100, setSpeedAnemo = 0;
|
|
|
|
|
int stateResistor = 0;
|
|
|
|
|
|
|
|
|
|
const char *mqttIPHost; //тут хранится IP хоста по хостнейму
|
|
|
|
@ -57,19 +71,17 @@ int reqCounter = 0;
|
|
|
|
|
long rssi = 0, minRes=1480, maxRes=2600;
|
|
|
|
|
char bufTopic[140], bufWillTopic[150], bufSetTopic[150];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int moveAnemo(byte speed, byte percent);
|
|
|
|
|
int moveAnemo(byte percent);
|
|
|
|
|
int setMotor(bool Finput, int Binput);
|
|
|
|
|
void callback(char *topic, byte *payload, unsigned int length);
|
|
|
|
|
int disconnectMQTT();
|
|
|
|
|
int disconnectMQTT();
|
|
|
|
|
int SendMqttReq(bool sendVal, bool sendStatus, byte statusConn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ledset(byte color = 0)
|
|
|
|
|
{
|
|
|
|
|
M5.dis.setBrightness(60); //Половина яркости
|
|
|
|
|
switch (color)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
case 2:
|
|
|
|
|
M5.dis.drawpix(0, 0xf00000); //Зеленый
|
|
|
|
|
break;
|
|
|
|
@ -183,7 +195,7 @@ int reqNtpTime()
|
|
|
|
|
{
|
|
|
|
|
uint32_t timeEpoch = timeClient.getEpochTime();
|
|
|
|
|
if (timeEpoch < 1600000000)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
Serial.println("Error reqNtpTime() Time");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -195,15 +207,13 @@ int reqNtpTime()
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//{"percent":50,"speed":20}
|
|
|
|
|
//{"amount":50}
|
|
|
|
|
void callback(char *topic, byte *payload, unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
|
Serial.print(topic);
|
|
|
|
|
Serial.print("] ");
|
|
|
|
|
|
|
|
|
|
char jsonResult[250];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
@ -211,73 +221,74 @@ void callback(char *topic, byte *payload, unsigned int length)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeserializationError error = deserializeJson(docResult, jsonResult);
|
|
|
|
|
if (error)
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPercent = (byte)docResult["percent"]; //0-100
|
|
|
|
|
setSpeedAnemo = (byte)docResult["speed"]; //speed
|
|
|
|
|
setPercent = (byte)docResult["amount"]; //0-100
|
|
|
|
|
//setSpeedAnemo = (byte)docResult["speed"]; //speed
|
|
|
|
|
|
|
|
|
|
moveAnemo(setSpeedAnemo, setPercent);
|
|
|
|
|
}
|
|
|
|
|
if(setPercent!=prevPercent) {
|
|
|
|
|
setMotor(false,false);
|
|
|
|
|
delay(1000);
|
|
|
|
|
if(setPercent<=100 && setPercent>=0 && abs(prevPercent-setPercent)>=10) {
|
|
|
|
|
moveAnemo(setPercent);
|
|
|
|
|
prevPercent=setPercent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nowPercent(int analogStatus)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Serial.print("Percent NOW: ");
|
|
|
|
|
int percentNow = map(analogStatus, minRes, maxRes, 0, 100);
|
|
|
|
|
Serial.print(percentNow);
|
|
|
|
|
Serial.println(" %");
|
|
|
|
|
|
|
|
|
|
return percentNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nowResist()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Serial.print("NOW: ");
|
|
|
|
|
int analogStatus = analogRead(Resist);
|
|
|
|
|
Serial.print(analogStatus);
|
|
|
|
|
|
|
|
|
|
Serial.println(analogStatus);
|
|
|
|
|
return analogStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int moveAnemo(byte speed, byte percent)
|
|
|
|
|
int moveAnemo(byte percent)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Serial.print("Set: ");
|
|
|
|
|
Serial.print(speed);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.println(percent);
|
|
|
|
|
|
|
|
|
|
int setResist = map(percent, 0, 100, minRes, maxRes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println(" -------------------- ");
|
|
|
|
|
|
|
|
|
|
if (setResist <= stateResistor)
|
|
|
|
|
{
|
|
|
|
|
setMotor(false, true);
|
|
|
|
|
while (setResist <= stateResistor)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
stateResistor = analogRead(Resist);
|
|
|
|
|
Serial.print("Now: ");
|
|
|
|
|
Serial.print(map(stateResistor, minRes, maxRes, 0, 100));
|
|
|
|
|
Serial.println(" %");
|
|
|
|
|
delay(200);
|
|
|
|
|
Serial.println(" %");
|
|
|
|
|
MqttClient.loop();
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
setMotor(true, false);
|
|
|
|
|
while (setResist >= stateResistor)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
stateResistor = analogRead(Resist);
|
|
|
|
|
Serial.print("Now: ");
|
|
|
|
|
Serial.print(map(stateResistor, minRes, maxRes, 0, 100));
|
|
|
|
|
Serial.println(" %");
|
|
|
|
|
delay(200);
|
|
|
|
|
MqttClient.loop();
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -326,9 +337,8 @@ void OTAUpdate()
|
|
|
|
|
OTApreferences.begin("ota-config");
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
HTTPClient http;
|
|
|
|
|
String serverPath = "http://meteosence.s-host.net/airqa/airquality.php?meteopas=sfddsg38r327kjsdgf8932r&mac = " + macc + " &meteodata = gethash";
|
|
|
|
|
String serverPath = "http://meteosence.s-host.net/airqa/airquality.php?meteopas=sfdd2retert45645yre932r&mac = " + macc + " &meteodata = gethash";
|
|
|
|
|
http.begin(serverPath.c_str());
|
|
|
|
|
int httpResponseCode = http.GET();
|
|
|
|
|
|
|
|
|
@ -378,8 +388,8 @@ void OTAUpdate()
|
|
|
|
|
{
|
|
|
|
|
flagOTA = false;
|
|
|
|
|
//Serial.println("flagOTA = false;");
|
|
|
|
|
|
|
|
|
|
t_httpUpdate_return ret = ESPhttpUpdate.update("http://meteosence.s-host.net/airqa/co2/co2.bin");
|
|
|
|
|
|
|
|
|
|
t_httpUpdate_return ret = ESPhttpUpdate.update("http://meteosence.s-host.net/airqa/anemo/anemoatoms.bin");
|
|
|
|
|
|
|
|
|
|
//После update ничего не происходит, такая вот особенность.
|
|
|
|
|
//Если все прошло хорошо, перезагрузка на новую прошивку
|
|
|
|
@ -437,15 +447,17 @@ int SendMqttReq(bool sendVal = true, bool sendStatus = true, byte statusConn = 1
|
|
|
|
|
unsigned long timeNow = now();
|
|
|
|
|
|
|
|
|
|
doc["analog"] = nowResist();
|
|
|
|
|
doc["percent"] = nowPercent(nowResist());
|
|
|
|
|
doc["amount"] = setPercent;
|
|
|
|
|
doc["calc"] = nowPercent(nowResist());
|
|
|
|
|
|
|
|
|
|
doc["ts"] = timeNow;
|
|
|
|
|
|
|
|
|
|
serializeJson(doc, JsonData);
|
|
|
|
|
doc.remove("ts");
|
|
|
|
|
doc.remove("analog");
|
|
|
|
|
doc.remove("percent");
|
|
|
|
|
doc.clear();
|
|
|
|
|
doc.remove("amount");
|
|
|
|
|
doc.remove("calc");
|
|
|
|
|
doc.clear();
|
|
|
|
|
doc.garbageCollect();
|
|
|
|
|
|
|
|
|
|
Serial.println(JsonData); //Вывод JSON строки в консоль
|
|
|
|
|