Skip to content

Instantly share code, notes, and snippets.

@TheRochVoices
Created May 25, 2018 16:32
Show Gist options
  • Save TheRochVoices/f6b1e19c7fe03e8577a2737f713e5c60 to your computer and use it in GitHub Desktop.
Save TheRochVoices/f6b1e19c7fe03e8577a2737f713e5c60 to your computer and use it in GitHub Desktop.
Arduino MQTT test
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "******";
const char* password = "*******";
const char* mqttServer = "******";
const int mqttPort = 14314;
const char* mqttUser = "aidjmvrz";
const char* mqttPassword = "5ZQBhUygU0f0";
WiFiClient espClient;
PubSubClient client(espClient);
String toSend = "Hello from the other side";
char toSendf[30];
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
toSend.toCharArray(toSendf, 30);
client.publish("esp/test", toSendf);
}
void loop() {
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment