Управление скокростью не удалось, отменяю

master
SmartUA 2021-08-03 15:53:58 +03:00
parent 9fa3fb43e1
commit 1d66960a3d
1 changed files with 65 additions and 30 deletions

View File

@ -10,13 +10,19 @@
#include <PubSubClient.h> //Mtqq
#include <ArduinoJson.h> //Упакова в JSon - удобная библиотека
#include <ESPmDNS.h>
#include "esp32-hal-ledc.h"
#define Fpin 22
#define Bpin 19
#define Resist 33
#define TRIGGER_PIN 39
unsigned int VersionSW = 2;
unsigned int VersionSW = 4;
const int freq = 5000;
const int ledChannel1 = 0;
const int ledChannel2 = 1;
const int resolution = 8;
//3 - Первый код для анемостата
@ -48,11 +54,11 @@ int stateResistor = 0;
const char *mqttIPHost; //тут хранится IP хоста по хостнейму
unsigned long timingUpdate = 0, timingReqSensor = 0, timingSendMqtt = 0; //Таймеры для millis()
int PROGMEM nextM5Update = 450000, nextMqttSend = 60000; //каждые 7.5 минут запрос обновления с сервера
int PROGMEM nextM5Update = 450000, nextMqttSend = 10000; //каждые 7.5 минут запрос обновления с сервера
bool mqttSendFlag = false;
int reqCounter = 0;
long rssi = 0;
long rssi = 0, minRes = 1450, maxRes = 2600;
char bufTopic[140], bufWillTopic[150], bufSetTopic[150];
int moveAnemo(byte speed, byte percent);
@ -218,6 +224,27 @@ void callback(char *topic, byte *payload, unsigned int length)
moveAnemo(setSpeedAnemo, 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);
return analogStatus;
}
int moveAnemo(byte speed, byte percent)
{
@ -226,15 +253,9 @@ int moveAnemo(byte speed, byte percent)
Serial.print(" ");
Serial.println(percent);
int setResist = map(percent, 0, 100, 1420, 2630);
int setResist = map(percent, 0, 100, minRes, maxRes);
Serial.print("NOW: ");
Serial.print(setResist);
Serial.print(" ");
Serial.println(map(setResist, 1420, 2630, 0, 100));
Serial.println(" %");
Serial.println(" -------------------- ");
Serial.println(" -------------------- ");
if (setResist <= stateResistor)
{
@ -243,25 +264,26 @@ int moveAnemo(byte speed, byte percent)
{
stateResistor = analogRead(Resist);
Serial.print("Now: ");
Serial.print(map(stateResistor, 1420, 2630, 0, 100));
Serial.println(" %");
Serial.print(map(stateResistor, minRes, maxRes, 0, 100));
Serial.println(" %");
delay(200);
}
}
if (setResist >= stateResistor)
}
}
else
{
setMotor(true, false);
while (setResist >= stateResistor)
{
stateResistor = analogRead(Resist);
Serial.print("Now: ");
Serial.print(map(stateResistor, 1420, 2630, 0, 100));
Serial.println(" %");
Serial.print(map(stateResistor, minRes, maxRes, 0, 100));
Serial.println(" %");
delay(200);
}
}
setMotor(false, false);
// 0 - 1460
// 10 - 1535 - 1547
// 20 - 1656 - 1668
@ -414,17 +436,16 @@ int SendMqttReq(bool sendVal = true, bool sendStatus = true, byte statusConn = 1
rssi = WiFi.RSSI();
unsigned long timeNow = now();
doc["t"] = "";
doc["h"] = "";
doc["analog"] = nowResist();
doc["percent"] = nowPercent(nowResist());
doc["co2"] = "";
doc["ts"] = timeNow;
serializeJson(doc, JsonData);
doc.remove("ts");
doc.remove("t");
doc.remove("h");
doc.remove("co2");
doc.remove("analog");
doc.remove("percent");
doc.clear();
doc.garbageCollect();
@ -461,7 +482,7 @@ int SendMqttReq(bool sendVal = true, bool sendStatus = true, byte statusConn = 1
doc["rssi"] = rssi;
doc["bsid"] = WiFi.BSSIDstr();
doc["ts"] = timeNow;
doc["exp"] = timeNow + 600;
doc["exp"] = timeNow + 11000;
doc["sv"] = VersionSW;
JsonData = "";
@ -532,8 +553,11 @@ void setup()
delay(50);
pinMode(TRIGGER_PIN, INPUT);
pinMode(Fpin, OUTPUT);
pinMode(Bpin, OUTPUT);
ledcSetup(ledChannel1, freq, resolution);
ledcSetup(ledChannel2, freq, resolution);
ledcAttachPin(Fpin, ledChannel1);
ledcAttachPin(Bpin, ledChannel2);
setMotor(false, false);
@ -580,6 +604,7 @@ void setup()
}
setMqttServer(mqttHostName, mqttPort);
nowPercent(nowResist());
}
void loop()
@ -636,14 +661,24 @@ int setMotor(bool Finput, int Binput)
if (Finput == true && Binput == false)
{
digitalWrite(Fpin, HIGH);
// digitalWrite(Fpin, HIGH);
ledcDetachPin(Bpin);
digitalWrite(Bpin, LOW);
ledcAttachPin(Fpin, ledChannel1);
ledcWrite(ledChannel1, 100);
}
if (Finput == false && Binput == true)
{
ledcDetachPin(Fpin);
digitalWrite(Fpin, LOW);
digitalWrite(Bpin, HIGH);
ledcWrite(ledChannel2, 100);
// digitalWrite(Bpin, HIGH);
}
//stop