Last active
September 22, 2018 14:15
-
-
Save sankarcheppali/963901f5fd707be1be11fcb4c021270d to your computer and use it in GitHub Desktop.
esp32 will publish message received from serial line to mqtt broker, writes messages received from mqtt broker to serial line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <PubSubClient.h> | |
// Update these with values suitable for your network. | |
const char* ssid = "SSID"; | |
const char* password = "pwd"; | |
const char* mqtt_server = "iot.eclipse.org"; | |
#define mqtt_port 1883 | |
#define MQTT_USER "username" | |
#define MQTT_PASSWORD "password" | |
#define MQTT_SERIAL_PUBLISH_CH "/icircuit/ESP32/serialdata/tx" | |
#define MQTT_SERIAL_RECEIVER_CH "/icircuit/ESP32/serialdata/rx" | |
WiFiClient wifiClient; | |
PubSubClient client(wifiClient); | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Create a random client ID | |
String clientId = "ESP32Client-"; | |
clientId += String(random(0xffff), HEX); | |
// Attempt to connect | |
if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) { | |
Serial.println("connected"); | |
//Once connected, publish an announcement... | |
client.publish("/icircuit/presence/ESP32/", "hello world"); | |
// ... and resubscribe | |
client.subscribe(MQTT_SERIAL_RECEIVER_CH); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void callback(char* topic, byte *payload, unsigned int length) { | |
Serial.println("-------new message from broker-----"); | |
Serial.print("channel:"); | |
Serial.println(topic); | |
Serial.print("data:"); | |
Serial.write(payload, length); | |
Serial.println(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.setTimeout(500);// Set time out for | |
setup_wifi(); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
reconnect(); | |
} | |
void publishSerialData(char *serialData){ | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.publish(MQTT_SERIAL_PUBLISH_CH, serialData); | |
} | |
void loop() { | |
client.loop(); | |
if (Serial.available() > 0) { | |
char bfr[501]; | |
memset(bfr,0, 501); | |
Serial.readBytesUntil( '\n',bfr,500); | |
publishSerialData(bfr); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
It seems that it is dropping some data from the serial input.