Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
areve committed Apr 13, 2019
1 parent 841619a commit f6dc8b9
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions examples/example-mqtt-ws.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "WebSocketStreamClient.h"
#include "WebSocketClient250.h"

#define DEBUG_MAIN(...) Serial.printf(__VA_ARGS__)

#ifndef DEBUG_MAIN
#define DEBUG_MAIN(...)
#endif

// config
const char *ssid = "YOURSSID";
const char *password = "YOURPASSWORD";
const char *host = (char *)"web-socket-mqtt.herokuapp.com";
const int port = 443;
const char *fingerprint = "08 3B 71 72 02 43 6E CA ED 42 86 93 BA 7E DF 81 C4 BC 62 30";
const char *path = (char *)"/mqtt";

// globals
unsigned int counter = 0;
unsigned long lastPublishTime = 0;
unsigned long publishInterval = 1000;
uint8_t buffer[1000];

void onMqttPublish(char *topic, byte *payload, unsigned int length)
{
DEBUG_MAIN("received %s %.*s\n", topic, length, payload);
}

WiFiClientSecure wiFiClient;
WebSocketClient250 wsClient(wiFiClient, host, port);
WebSocketStreamClient wsStreamClient(wsClient, path);
PubSubClient mqtt(wsStreamClient);

void setup()
{
Serial.begin(9600);
delay(2000);

DEBUG_MAIN("WiFi connecting to %s\n", ssid);
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
DEBUG_MAIN(".");
delay(500);
}
DEBUG_MAIN("\nWiFi connected to %s\n", WiFi.SSID().c_str());

wiFiClient.setFingerprint(fingerprint);
mqtt.setCallback(onMqttPublish);

DEBUG_MAIN("mqtt connecting\n");
if (mqtt.connect("topic1"))
{
DEBUG_MAIN("mqtt connected OK\n");
mqtt.publish("log", "hello world");
mqtt.subscribe("topic1");
}
}

void loop()
{
mqtt.loop();
if (lastPublishTime + publishInterval < millis())
{
lastPublishTime = millis();
DEBUG_MAIN("tick %d\n", ++counter);
if (mqtt.connected())
{
String msg = (String("esp8266 counter ") + String(counter));
mqtt.publish("log", msg.c_str());
}
}
}

0 comments on commit f6dc8b9

Please sign in to comment.