Guest User

Untitled

a guest
Jan 11th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. //Wifi
  2. #include <ESP8266WiFi.h>
  3. #ifndef STASSID
  4. #define STASSID ""
  5. #define STAPSK ""
  6. #endif
  7. const char* ssid = STASSID;
  8. const char* password = STAPSK;
  9. char* netbios_name = "RFLINK";
  10.  
  11. //Netbios
  12. #include <ESP8266NetBIOS.h>
  13.  
  14. //MQTT
  15. #include "Adafruit_MQTT.h"
  16. #include "Adafruit_MQTT_Client.h"
  17.  
  18. char* MQTT_SERVER = "";
  19. int MQTT_PORT = 8883;
  20. char* MQTT_USERNAME = "";
  21. char* MQTT_KEY = "";
  22.  
  23. const char *fingerprint = "";
  24.  
  25. WiFiClientSecure client;
  26. Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_KEY);
  27.  
  28.  
  29. //Topic publish
  30. Adafruit_MQTT_Publish serial_out = Adafruit_MQTT_Publish(&mqtt, "/rflink/out");
  31.  
  32.  
  33. //Topic subscribe
  34. Adafruit_MQTT_Subscribe serial_in = Adafruit_MQTT_Subscribe(&mqtt, "/rflink/in");
  35.  
  36. // Bug workaround for Arduino 1.6.6, it seems to need a function declaration
  37. // for some reason (only affects ESP8266, likely an arduino-builder bug).
  38. //void MQTT_connect();
  39. //void verifyFingerprint();
  40.  
  41. // Serial
  42. bool dataAvailable = false;
  43. long lastMsg = 0;
  44. char msg[50];
  45. int value = 0;
  46. String serialData = "";
  47.  
  48. void setup() {
  49. Serial.begin(57600);
  50. WiFi.mode(WIFI_STA);
  51. WiFi.begin(ssid, password);
  52. while (WiFi.status() != WL_CONNECTED) {
  53. delay(500);
  54. //Serial.print(".");
  55. }
  56. //Serial.println("");
  57. //Serial.print("Connected to ");
  58. //Serial.println(ssid);
  59. //Serial.print("IP address: ");
  60. //Serial.println(WiFi.localIP());
  61. NBNS.begin(netbios_name);
  62.  
  63. verifyFingerprint();
  64.  
  65. mqtt.subscribe(&serial_in);
  66.  
  67. }
  68.  
  69. void loop() {
  70. MQTT_connect();
  71. char serialChar;
  72. //Subscribe
  73. Adafruit_MQTT_Subscribe *subscription;
  74. while ((subscription = mqtt.readSubscription(5000))) {
  75. if (subscription == &serial_in) {
  76. Serial.println((char *)serial_in.lastread);
  77. }
  78. }
  79.  
  80. //Publish
  81. while (Serial.available() > 0){
  82. serialChar = Serial.read(); // Reading Serial Data and saving in data variable
  83. String data2(serialChar);
  84. serialData = serialData + data2;
  85. delay(1);
  86. dataAvailable = true;
  87. }
  88.  
  89. if (dataAvailable == true) {
  90. int str_len = serialData.length() + 1;
  91. char msg[str_len];
  92. serialData.toCharArray(msg, str_len);
  93. if (serial_out.publish(msg)) {
  94. //Serial.println(F("OK"));
  95. serialData = "";
  96. dataAvailable = false;
  97. } else {
  98. //Serial.println(F("Failed"));
  99. }
  100. }
  101. }
  102.  
  103. void verifyFingerprint() {
  104.  
  105. const char* host = MQTT_SERVER;
  106.  
  107. Serial.print("Connecting to ");
  108. Serial.print(host);
  109. Serial.println(MQTT_PORT);
  110.  
  111. if (! client.connect(host, MQTT_PORT)) {
  112. Serial.println("Connection failed. Halting execution.");
  113. delay(1000);
  114. }
  115.  
  116. if (client.verify(fingerprint, host) != 0) {
  117. Serial.println("Connection secure.");
  118. } else {
  119. Serial.println("Connection insecure! Halting execution.");
  120. while (1);
  121. }
  122.  
  123. }
  124.  
  125. // Function to connect and reconnect as necessary to the MQTT server.
  126. // Should be called in the loop function and it will take care if connecting.
  127. void MQTT_connect() {
  128. int8_t ret;
  129.  
  130. // Stop if already connected.
  131. if (mqtt.connected()) {
  132. return;
  133. }
  134.  
  135. Serial.print("Connecting to MQTT... ");
  136.  
  137. uint8_t retries = 3;
  138. while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  139. Serial.println(mqtt.connectErrorString(ret));
  140. Serial.println("Retrying MQTT connection in 5 seconds...");
  141. mqtt.disconnect();
  142. delay(5000); // wait 5 seconds
  143. retries--;
  144. if (retries == 0) {
  145. // basically die and wait for WDT to reset me
  146. while (1);
  147. }
  148. }
  149.  
  150. Serial.println("MQTT Connected!");
  151. }
Add Comment
Please, Sign In to add comment