mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 11:59:12 +03:00
Добавляем сенсор сонар hc-sr04
This commit is contained in:
@@ -116,7 +116,8 @@
|
|||||||
"int": 10,
|
"int": 10,
|
||||||
"token": "",
|
"token": "",
|
||||||
"autos": 1,
|
"autos": 1,
|
||||||
"receiveMsg": 0
|
"receiveMsg": 0,
|
||||||
|
"chatID": ""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "9. Таймер",
|
"name": "9. Таймер",
|
||||||
@@ -509,12 +510,25 @@
|
|||||||
"int": 15,
|
"int": 15,
|
||||||
"round": 1
|
"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": "Экраны"
|
"header": "Экраны"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "36. LCD экран 2004",
|
"name": "37. LCD экран 2004",
|
||||||
"num": 36,
|
"num": 37,
|
||||||
"type": "Reading",
|
"type": "Reading",
|
||||||
"subtype": "Lcd2004",
|
"subtype": "Lcd2004",
|
||||||
"id": "Lcd",
|
"id": "Lcd",
|
||||||
@@ -528,8 +542,8 @@
|
|||||||
"id2show": "id датчика"
|
"id2show": "id датчика"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "37. LCD экран 1602",
|
"name": "38. LCD экран 1602",
|
||||||
"num": 37,
|
"num": 38,
|
||||||
"type": "Reading",
|
"type": "Reading",
|
||||||
"subtype": "Lcd2004",
|
"subtype": "Lcd2004",
|
||||||
"id": "Lcd",
|
"id": "Lcd",
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ void* getAPI_Max6675(String subtype, String params);
|
|||||||
void* getAPI_Mhz19(String subtype, String params);
|
void* getAPI_Mhz19(String subtype, String params);
|
||||||
void* getAPI_Sds011(String subtype, String params);
|
void* getAPI_Sds011(String subtype, String params);
|
||||||
void* getAPI_Sht20(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_Lcd2004(String subtype, String params);
|
||||||
|
|
||||||
void* getAPI(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_Mhz19(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_Sds011(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_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;
|
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;return nullptr;
|
||||||
}
|
}
|
||||||
64
src/modules/sensors/Sonar/Sonar.cpp
Normal file
64
src/modules/sensors/Sonar/Sonar.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/modules/sensors/Sonar/items.json
Normal file
16
src/modules/sensors/Sonar/items.json
Normal 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
|
||||||
|
}
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user