Last active
June 3, 2017 17:54
-
-
Save maxpromer/5f0e26214c5a85ca5874c8731262a6cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
| #define WIFI_STA_NAME "xxxxxxxxxx" | |
| #define WIFI_STA_PASS "xxxxxxxxxx" | |
| #define MQTT_SERVER "m11.cloudmqtt.com" | |
| #define MQTT_PORT 10384 | |
| #define MQTT_USERNAME "esp32_1" | |
| #define MQTT_PASSWORD "123456" | |
| #define MQTT_NAME "ESP32_1" | |
| #define LED_PIN 23 | |
| WiFiClient client; | |
| PubSubClient mqtt(client); | |
| void callback(char* topic, byte* payload, unsigned int length) { | |
| payload[length] = '\0'; | |
| String topic_str = topic, payload_str = (char*)payload; | |
| Serial.println("[" + topic_str + "]: " + payload_str); | |
| digitalWrite(LED_PIN, (payload_str == "ON") ? HIGH : LOW); | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| pinMode(LED_BUILTIN, OUTPUT); | |
| pinMode(LED_PIN, OUTPUT); | |
| Serial.println(); | |
| Serial.println(); | |
| Serial.print("Connecting to "); | |
| Serial.println(WIFI_STA_NAME); | |
| WiFi.mode(WIFI_STA); | |
| WiFi.begin(WIFI_STA_NAME, WIFI_STA_PASS); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
| } | |
| digitalWrite(LED_BUILTIN, HIGH); | |
| Serial.println(""); | |
| Serial.println("WiFi connected"); | |
| Serial.println("IP address: "); | |
| Serial.println(WiFi.localIP()); | |
| mqtt.setServer(MQTT_SERVER, MQTT_PORT); | |
| mqtt.setCallback(callback); | |
| } | |
| void loop() { | |
| if (mqtt.connected() == false) { | |
| Serial.print("MQTT connection... "); | |
| if (mqtt.connect(MQTT_NAME, MQTT_USERNAME, MQTT_PASSWORD)) { | |
| Serial.println("connected"); | |
| mqtt.subscribe("/ESP32_1/LED"); | |
| } else { | |
| Serial.println("failed"); | |
| delay(5000); | |
| } | |
| } else { | |
| mqtt.loop(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment