Добавляем сенсор сонар hc-sr04

This commit is contained in:
2022-05-17 21:18:39 +03:00
parent 879a67de6f
commit fe64723ecd
4 changed files with 101 additions and 5 deletions

View File

@@ -116,7 +116,8 @@
"int": 10,
"token": "",
"autos": 1,
"receiveMsg": 0
"receiveMsg": 0,
"chatID": ""
},
{
"name": "9. Таймер",
@@ -509,12 +510,25 @@
"int": 15,
"round": 1
},
{
"name": "36. Сонар HC-SR04",
"num": 36,
"type": "Reading",
"subtype": "Sonar",
"id": "sonar",
"widget": "anydata",
"page": "Сенсоры",
"descr": "Расстояние",
"pinTrig": 5,
"pinEcho": 4,
"int": 5
},
{
"header": "Экраны"
},
{
"name": "36. LCD экран 2004",
"num": 36,
"name": "37. LCD экран 2004",
"num": 37,
"type": "Reading",
"subtype": "Lcd2004",
"id": "Lcd",
@@ -528,8 +542,8 @@
"id2show": "id датчика"
},
{
"name": "37. LCD экран 1602",
"num": 37,
"name": "38. LCD экран 1602",
"num": 38,
"type": "Reading",
"subtype": "Lcd2004",
"id": "Lcd",

View File

@@ -22,6 +22,7 @@ void* getAPI_Max6675(String subtype, String params);
void* getAPI_Mhz19(String subtype, String params);
void* getAPI_Sds011(String subtype, String params);
void* getAPI_Sht20(String subtype, String params);
void* getAPI_Sonar(String subtype, String params);
void* getAPI_Lcd2004(String subtype, String params);
void* getAPI(String subtype, String params) {
@@ -48,5 +49,6 @@ if ((tmpAPI = getAPI_Max6675(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Mhz19(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sds011(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sonar(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;return nullptr;
}

View File

@@ -0,0 +1,64 @@
#include "Global.h"
#include "classes/IoTItem.h"
extern IoTGpio IoTgpio;
class Sonar : public IoTItem {
private:
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long difference;
unsigned int _pinTrig, _pinEcho;
public:
Sonar(String parameters): IoTItem(parameters) {
_pinTrig = jsonReadInt(parameters, "pinTrig");
_pinEcho = jsonReadInt(parameters, "pinEcho");
pinMode(_pinTrig, OUTPUT);
pinMode(_pinEcho, INPUT);
}
void doByInterval() {
// value.valD = IoTgpio.analogRead(_pin);
// для большей точности установим значение LOW на пине Trig
digitalWrite(_pinTrig, LOW);
delayMicroseconds(2);
// Теперь установим высокий уровень на пине Trig
digitalWrite(_pinTrig, HIGH);
// Подождем 10 μs
delayMicroseconds(10);
digitalWrite(_pinTrig, LOW);
// Рассчитаем расстояние
value.valD = pulseIn(_pinEcho, HIGH) / 58;
// Выведем значение в Serial Monitor
Serial.print(value.valD);
Serial.println(" cm");
regEvent(value.valD, "Sonar");
}
// void loop() {
// // currentMillis = millis();
// // difference = currentMillis - prevMillis;
// // if (difference >= _interval) {
// // prevMillis = millis();
// // }
// }
~Sonar() {};
};
void* getAPI_Sonar(String subtype, String param) {
if (subtype == F("Sonar")) {
return new Sonar(param);
} else {
return nullptr;
}
}

View File

@@ -0,0 +1,16 @@
[
{
"name": "Сонар HC-SR04",
"num": 1,
"type": "Reading",
"subtype": "Sonar",
"id": "sonar",
"widget": "anydataTmp",
"page": "Сенсоры",
"descr": "Расстояние",
"pinTrig": 5,
"pinEcho": 4,
"int": 5
}
]