diff --git a/data_svelte/items.json b/data_svelte/items.json index af81b107..88d1dfad 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -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", diff --git a/src/modules/API.cpp b/src/modules/API.cpp index 4174ebd0..cb2ef008 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -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; } \ No newline at end of file diff --git a/src/modules/sensors/Sonar/Sonar.cpp b/src/modules/sensors/Sonar/Sonar.cpp new file mode 100644 index 00000000..bc339973 --- /dev/null +++ b/src/modules/sensors/Sonar/Sonar.cpp @@ -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; + } +} diff --git a/src/modules/sensors/Sonar/items.json b/src/modules/sensors/Sonar/items.json new file mode 100644 index 00000000..ee00644c --- /dev/null +++ b/src/modules/sensors/Sonar/items.json @@ -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 + } +] \ No newline at end of file