2020-09-03 01:12:43 +03:00
|
|
|
|
#pragma once
|
|
|
|
|
|
#include <Arduino.h>
|
2020-10-07 11:39:08 +03:00
|
|
|
|
|
2020-09-03 01:12:43 +03:00
|
|
|
|
#include "Class/LineParsing.h"
|
|
|
|
|
|
#include "Global.h"
|
2020-09-03 23:29:34 +03:00
|
|
|
|
#include "items/SensorConvertingClass.h"
|
2020-09-03 01:12:43 +03:00
|
|
|
|
|
|
|
|
|
|
class SensorUltrasonic : public SensorConvertingClass {
|
|
|
|
|
|
public:
|
|
|
|
|
|
SensorUltrasonic() : SensorConvertingClass(){};
|
2020-09-03 23:29:34 +03:00
|
|
|
|
void init() {
|
|
|
|
|
|
sensorReadingMap += _key + ",";
|
|
|
|
|
|
String trig = selectFromMarkerToMarker(_pin, ",", 0);
|
|
|
|
|
|
String echo = selectFromMarkerToMarker(_pin, ",", 1);
|
2020-10-07 11:39:08 +03:00
|
|
|
|
pinMode(trig.toInt(), OUTPUT);
|
|
|
|
|
|
pinMode(echo.toInt(), INPUT);
|
2020-09-03 23:29:34 +03:00
|
|
|
|
jsonWriteStr(configOptionJson, _key + "_trig", trig);
|
|
|
|
|
|
jsonWriteStr(configOptionJson, _key + "_echo", echo);
|
|
|
|
|
|
jsonWriteStr(configOptionJson, _key + "_map", _map);
|
|
|
|
|
|
jsonWriteStr(configOptionJson, _key + "_с", _c);
|
|
|
|
|
|
}
|
2020-09-03 01:12:43 +03:00
|
|
|
|
|
2020-09-03 23:29:34 +03:00
|
|
|
|
void SensorUltrasonicRead(String key) {
|
|
|
|
|
|
int trig = jsonReadStr(configOptionJson, key + "_trig").toInt();
|
|
|
|
|
|
int echo = jsonReadStr(configOptionJson, key + "_echo").toInt();
|
2020-09-03 01:12:43 +03:00
|
|
|
|
int value;
|
|
|
|
|
|
|
|
|
|
|
|
digitalWrite(trig, LOW);
|
|
|
|
|
|
delayMicroseconds(2);
|
|
|
|
|
|
digitalWrite(trig, HIGH);
|
|
|
|
|
|
delayMicroseconds(10);
|
|
|
|
|
|
digitalWrite(trig, LOW);
|
|
|
|
|
|
long duration_ = pulseIn(echo, HIGH, 30000); // 3000 µs = 50cm // 30000 µs = 5 m
|
|
|
|
|
|
value = duration_ / 29 / 2;
|
|
|
|
|
|
|
|
|
|
|
|
value = this->mapping(key, value);
|
|
|
|
|
|
float valueFl = this->correction(key, value);
|
|
|
|
|
|
eventGen(key, "");
|
|
|
|
|
|
jsonWriteStr(configLiveJson, key, String(valueFl));
|
2020-10-07 11:39:08 +03:00
|
|
|
|
publishStatus(key, String(valueFl));
|
2020-09-17 17:27:44 +03:00
|
|
|
|
Serial.println("I sensor '" + key + "' data: " + String(valueFl));
|
2020-09-03 01:12:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2020-09-03 02:07:12 +03:00
|
|
|
|
extern SensorUltrasonic mySensorUltrasonic;
|