Last active
October 10, 2018 01:52
-
-
Save hocarm/57b9c52151ef1142dc9ca44016c037e3 to your computer and use it in GitHub Desktop.
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 <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "wifi_name"; | |
const char* password = "pass"; | |
const char* mqttServer = "m12.cloudmqtt.com"; | |
const int mqttPort = 10769; | |
const char* mqttUser = "your_username"; | |
const char* mqttPassword = "your_password"; | |
long lastMsg = 0; | |
char msg[50]; | |
int value = 0; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
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); | |
client.setCallback(callback); | |
while (!client.connected()) { | |
Serial.println("Connecting to MQTT..."); | |
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) { | |
Serial.println("connected"); | |
} else { | |
Serial.print("failed with state "); | |
Serial.print(client.state()); | |
delay(2000); | |
} | |
} | |
//Pub Hello to esp/test topic and sub this topic | |
client.publish("esp/test", "Hello from ESP8266"); | |
client.subscribe("esp/test"); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived in topic: "); | |
Serial.println(topic); | |
Serial.print("Message:"); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
Serial.println("-----------------------"); | |
} | |
void loop() { | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 8000) { | |
lastMsg = now; | |
++value; | |
snprintf (msg, 75, "hello world #%ld", value); | |
// client.publish(mqtt_topic_pub, msg); | |
if (value < 10) { | |
Serial.print("Publish message: "); | |
Serial.println(msg); | |
client.publish("esp/test", msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment