Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Wifi
- #include <ESP8266WiFi.h>
- #ifndef STASSID
- #define STASSID ""
- #define STAPSK ""
- #endif
- const char* ssid = STASSID;
- const char* password = STAPSK;
- char* netbios_name = "RFLINK";
- //Netbios
- #include <ESP8266NetBIOS.h>
- //MQTT
- #include "Adafruit_MQTT.h"
- #include "Adafruit_MQTT_Client.h"
- char* MQTT_SERVER = "";
- int MQTT_PORT = 8883;
- char* MQTT_USERNAME = "";
- char* MQTT_KEY = "";
- const char *fingerprint = "";
- WiFiClientSecure client;
- Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_KEY);
- //Topic publish
- Adafruit_MQTT_Publish serial_out = Adafruit_MQTT_Publish(&mqtt, "/rflink/out");
- //Topic subscribe
- Adafruit_MQTT_Subscribe serial_in = Adafruit_MQTT_Subscribe(&mqtt, "/rflink/in");
- // Bug workaround for Arduino 1.6.6, it seems to need a function declaration
- // for some reason (only affects ESP8266, likely an arduino-builder bug).
- //void MQTT_connect();
- //void verifyFingerprint();
- // Serial
- bool dataAvailable = false;
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- String serialData = "";
- void setup() {
- Serial.begin(57600);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- //Serial.print(".");
- }
- //Serial.println("");
- //Serial.print("Connected to ");
- //Serial.println(ssid);
- //Serial.print("IP address: ");
- //Serial.println(WiFi.localIP());
- NBNS.begin(netbios_name);
- verifyFingerprint();
- mqtt.subscribe(&serial_in);
- }
- void loop() {
- MQTT_connect();
- char serialChar;
- //Subscribe
- Adafruit_MQTT_Subscribe *subscription;
- while ((subscription = mqtt.readSubscription(5000))) {
- if (subscription == &serial_in) {
- Serial.println((char *)serial_in.lastread);
- }
- }
- //Publish
- while (Serial.available() > 0){
- serialChar = Serial.read(); // Reading Serial Data and saving in data variable
- String data2(serialChar);
- serialData = serialData + data2;
- delay(1);
- dataAvailable = true;
- }
- if (dataAvailable == true) {
- int str_len = serialData.length() + 1;
- char msg[str_len];
- serialData.toCharArray(msg, str_len);
- if (serial_out.publish(msg)) {
- //Serial.println(F("OK"));
- serialData = "";
- dataAvailable = false;
- } else {
- //Serial.println(F("Failed"));
- }
- }
- }
- void verifyFingerprint() {
- const char* host = MQTT_SERVER;
- Serial.print("Connecting to ");
- Serial.print(host);
- Serial.println(MQTT_PORT);
- if (! client.connect(host, MQTT_PORT)) {
- Serial.println("Connection failed. Halting execution.");
- delay(1000);
- }
- if (client.verify(fingerprint, host) != 0) {
- Serial.println("Connection secure.");
- } else {
- Serial.println("Connection insecure! Halting execution.");
- while (1);
- }
- }
- // Function to connect and reconnect as necessary to the MQTT server.
- // Should be called in the loop function and it will take care if connecting.
- void MQTT_connect() {
- int8_t ret;
- // Stop if already connected.
- if (mqtt.connected()) {
- return;
- }
- Serial.print("Connecting to MQTT... ");
- uint8_t retries = 3;
- while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
- Serial.println(mqtt.connectErrorString(ret));
- Serial.println("Retrying MQTT connection in 5 seconds...");
- mqtt.disconnect();
- delay(5000); // wait 5 seconds
- retries--;
- if (retries == 0) {
- // basically die and wait for WDT to reset me
- while (1);
- }
- }
- Serial.println("MQTT Connected!");
- }
Add Comment
Please, Sign In to add comment