2022-09-26 21:56:54 +02:00
|
|
|
|
|
|
|
|
#include "Global.h"
|
|
|
|
|
#include "classes/IoTItem.h"
|
|
|
|
|
#include <Arduino.h>
|
2022-09-27 00:52:53 +02:00
|
|
|
|
2022-09-27 01:24:22 +02:00
|
|
|
#include "modules/sensors/UART/Uart.h"
|
2022-09-26 21:56:54 +02:00
|
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
SoftwareSerial* myUART = nullptr;
|
|
|
|
|
#else
|
|
|
|
|
HardwareSerial* myUART = nullptr;
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-27 01:24:22 +02:00
|
|
|
class UART : public IoTItem {
|
2022-09-26 21:56:54 +02:00
|
|
|
private:
|
|
|
|
|
int tx;
|
|
|
|
|
int rx;
|
2022-09-27 00:52:53 +02:00
|
|
|
int speed;
|
2022-09-26 21:56:54 +02:00
|
|
|
|
|
|
|
|
public:
|
2022-09-27 01:24:22 +02:00
|
|
|
UART(String parameters) : IoTItem(parameters) {
|
2022-09-26 21:56:54 +02:00
|
|
|
tx = jsonReadInt(parameters, "tx");
|
|
|
|
|
rx = jsonReadInt(parameters, "rx");
|
|
|
|
|
speed = jsonReadInt(parameters, "speed");
|
|
|
|
|
|
|
|
|
|
if (!myUART) {
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
myUART = new SoftwareSerial(tx, rx);
|
|
|
|
|
myUART->begin(speed);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
myUART = new HardwareSerial(2);
|
|
|
|
|
myUART->begin(speed, SERIAL_8N1, rx, tx);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
SerialPrint("i", F("UART"), F("UART Init"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void uartHandle() {
|
|
|
|
|
if (myUART) {
|
|
|
|
|
static String incStr;
|
|
|
|
|
if (myUART->available()) {
|
|
|
|
|
char inc;
|
|
|
|
|
inc = myUART->read();
|
|
|
|
|
incStr += inc;
|
|
|
|
|
if (inc == '\n') {
|
|
|
|
|
parse(incStr);
|
|
|
|
|
incStr = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void parse(String& incStr) {
|
|
|
|
|
SerialPrint("I", "=>UART", incStr);
|
|
|
|
|
}
|
2022-09-27 01:07:50 +02:00
|
|
|
|
|
|
|
|
void uartPrint(String msg) {
|
|
|
|
|
myUART->print(msg);
|
|
|
|
|
}
|
2022-09-26 21:56:54 +02:00
|
|
|
};
|
|
|
|
|
|
2022-09-27 01:24:22 +02:00
|
|
|
void* getAPI_UART(String subtype, String param) {
|
|
|
|
|
if (subtype == F("UART")) {
|
|
|
|
|
return new UART(param);
|
2022-09-26 21:56:54 +02:00
|
|
|
} else {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|