Last active
September 6, 2015 18:35
-
-
Save compilerexe/6a3d320153edb34fcaff to your computer and use it in GitHub Desktop.
Publish and Subscribe
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 = "__Your_SSID_WiFi__"; | |
const char *pass = "__Your_Password_WiFi__"; | |
String MQTT_server("m11.cloudmqtt.com"); | |
String MQTT_username = "sintilob"; | |
String MQTT_password = "ZNUXlWaU6Emv"; | |
String MQTT_topic = "compiler/data"; | |
String MQTT_sub = "compiler/data"; | |
int MQTT_port = 14878; | |
uint8_t led_pin = 12; | |
void callback(const MQTT::Publish& pub) { | |
String payload = pub.payload_string(); | |
if (payload == "ON") { | |
digitalWrite(led_pin, 1); | |
Serial.println("TURN ON"); | |
Serial.println("=============================="); | |
} else if (payload == "OFF"){ | |
digitalWrite(led_pin, 0); | |
Serial.println("TURN OFF"); | |
Serial.println("=============================="); | |
} | |
} | |
WiFiClient wclient; | |
PubSubClient client(wclient, MQTT_server, MQTT_port); | |
void setup() { | |
Serial.begin(115200); | |
pinMode(12, OUTPUT); | |
delay(10); | |
Serial.println(); | |
} | |
void loop() { | |
if (WiFi.status() != WL_CONNECTED) { | |
WiFi.begin(ssid, pass); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.println(); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(50); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
} | |
if (!client.connected()) { | |
Serial.println("Connecting to MQTT server"); | |
while (!client.connect(MQTT::Connect("ESP8266-12").set_auth(MQTT_username, MQTT_password))) { | |
Serial.print("."); | |
delay(10); | |
} | |
Serial.println(); | |
Serial.println("Connected MQTT"); | |
} else { | |
client.set_callback(callback); | |
while (!client.publish(MQTT_topic,"hello world")) { | |
delay(10); | |
} | |
Serial.print("Publish to "); | |
Serial.print(MQTT_topic); | |
Serial.println(); | |
Serial.println("=============================="); | |
while (!client.subscribe(MQTT_sub)) { | |
delay(10); | |
} | |
} | |
if (client.connected()){ | |
delay(1000); | |
client.loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment