Browse Source

Create uart_mqtt

Needs work:
This only works from UART to MQTT
Need help to get to the received string from MQTT and send it via serial
rfm69
AlbertWeterings 6 years ago
committed by GitHub
parent
commit
2aab97a1c4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 90 additions and 0 deletions
  1. +90
    -0
      code/espurna/uart_mqtt

+ 90
- 0
code/espurna/uart_mqtt View File

@ -0,0 +1,90 @@
#ifdef UARTtoMQTT
#include <queue>
#include <Ticker.h>
// -----------------------------------------------------------------------------
// GLOBALS TO THE MODULE
// -----------------------------------------------------------------------------
const byte numChars = 100;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void _recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void _sendNewData() {
if (newData == true && MQTT_SUPPORT) {
#if MQTT_SUPPORT
mqttSend(MQTT_TOPIC_UARTIN, receivedChars); // publish: UART -> mqtt bus
#endif
newData = false;
}
}
#if MQTT_SUPPORT
void _UARTtoMQTTMqttCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) {
mqttSubscribe(MQTT_TOPIC_UARTOUT);
DEBUG_MSG_P(PSTR("[UARTtoMQTT] MQTT Subscribe topic:\n"), MQTT_TOPIC_UARTOUT);
}
if (type == MQTT_MESSAGE_EVENT) {
// Match topic
String t = mqttTopicKey((char *) topic);
//DEBUG_MSG_P(PSTR("[UARTtoMQTT] t= :\n"), t);
bool isUARTOut = t.equals(MQTT_TOPIC_UARTOUT);
if (isUARTOut) {
//send the received MQTT message to Serial
}
}
}
#endif
// -----------------------------------------------------------------------------
// SETUP & LOOP
// -----------------------------------------------------------------------------
void UARTtoMQTTSetup() {
#if MQTT_SUPPORT
mqttRegister(_UARTtoMQTTMqttCallback);
#endif
// Register oop
espurnaRegisterLoop(UARTtoMQTTLoop);
}
void UARTtoMQTTLoop() {
_recvWithEndMarker();
_sendNewData();
}
#endif

Loading…
Cancel
Save