/* Basic ESP8266 MQTT example. 20 Mar 2016 Adapted for use on Digistump Oak by Pegman, Digistump Forums. This sketch demonstrates the capabilities of the pubsub library in combination with the ESP8266 board/library. It connects to an MQTT server then: - publishes "hello world" to the topic "outTopic" every two seconds - subscribes to the topic "inTopic", printing out any messages it receives. NB - it assumes the received payloads are strings not binary - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, else switch it off It will reconnect to the server if the connection is lost using a blocking reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to achieve the same result without blocking the main loop. */ #include #include #include const char* ssid = "ssid"; const char* password = "wifi_pass"; const char* mqtt_server = "m11.cloudmqtt.com"; const unsigned int mqtt_port = 22002; const char* connection_id = "chipid"; const char* client_name = "chipid"; const char* client_password = "password"; const char* fingerprint = "A5 02 FF 13 99 9F 8B 39 8E F1 83 4F 11 23 65 0B 32 36 FC 07"; WiFiClientSecure wifiClient; PubSubClient client(wifiClient); long lastMsg = 0; char msg[50]; int value = 0; void setup() { Serial.begin(115200); Serial.println(); Serial.print("booting up..."); Serial.println(); 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("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.print("connecting to "); Serial.println(mqtt_server); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); client.connect(connection_id, client_name, client_password); Serial.print("connected..."); Serial.println(); if (wifiClient.verify(fingerprint, mqtt_server)) { Serial.println("certificate matches"); } else { Serial.println("certificate doesn't match"); } } void callback(char* topic, byte* payload, unsigned int length) { // Switch on the LED if an 1 was received as first character if ((char)payload[0] == '1') { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { // Attempt to connect if (client.connect(connection_id, client_name, client_password)) { // Once connected, publish an announcement... client.publish("/outbox/chipid/test", "hello world SSL"); // ... and resubscribe client.subscribe("/inbox/chipid/test"); } else { // Wait 5 seconds before retrying delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); long now = millis(); if (now - lastMsg > 2000) { lastMsg = now; ++value; snprintf (msg, 75, "hello world SSL #%ld", value); client.publish("/outbox/chipid/test", msg); } }