Advertisement
Guest User

ArduinoWorkshop

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. /*
  2. Basic WiFi MQTT example
  3.  
  4. This sketch demonstrates the capabilities of the pubsub library in combination
  5. with the WiFi101 library.
  6.  
  7. It connects to an MQTT server then:
  8. - publishes "hello world" to the topic "outTopic" every two seconds
  9. - subscribes to the topic "inTopic", printing out any messages
  10. it receives. NB - it assumes the received payloads are strings not binary
  11. - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
  12. else switch it off
  13.  
  14. It will reconnect to the server if the connection is lost using a blocking
  15. reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  16. achieve the same result without blocking the main loop.
  17.  
  18. */
  19.  
  20. #include <WiFi101.h>
  21. #include <PubSubClient.h>
  22. #include "arduino_secrets.h"
  23. #define TOPIC "NWAFabLab/arduino/test"
  24. #define USERNAME "ahnelson"
  25. char ssid[] = "FabLab";
  26. char password[] = "FreakyF@y";
  27. // Status
  28. int status = WL_IDLE_STATUS;
  29. const char* mqtt_server = "broker.mqtt-dashboard.com";
  30. int led1_status = 0;
  31.  
  32. WiFiClient wifiClient;
  33. PubSubClient client(wifiClient);
  34. long lastMsg = 0;
  35. char msg[100];
  36. int value = 0;
  37.  
  38. void setup() {
  39. Serial.begin(115200);
  40. setup_wifi();
  41. client.setServer(mqtt_server, 1883);
  42. client.setCallback(callback);
  43. }
  44.  
  45. void setup_wifi() {
  46.  
  47. delay(10);
  48. // We start by connecting to a WiFi network
  49. Serial.println();
  50. Serial.print("Connecting to ");
  51. Serial.println(ssid);
  52.  
  53. while (status != WL_CONNECTED) {
  54. status = WiFi.begin(ssid, password);
  55. }
  56.  
  57. Serial.println("");
  58. Serial.println("WiFi connected");
  59. Serial.println("IP address: ");
  60. Serial.println(WiFi.localIP());
  61. }
  62.  
  63.  
  64. void callback(char* topic, byte* payload, unsigned int length) {
  65. Serial.print("Message arrived [");
  66. Serial.print(topic);
  67. Serial.print("] ");
  68. int i = 0;
  69. char angles[length];
  70. for(i = 0;i<length;i++){
  71. angles[i] = (char)payload[i];
  72. }
  73. Serial.println(angles);
  74.  
  75. char* token;
  76. token = strtok(angles,",");
  77. while(token != NULL){
  78. Serial.print(token);
  79. token = strtok(NULL,",");
  80. }
  81. Serial.println("");
  82. }
  83.  
  84. void reconnect() {
  85. // Loop until we're reconnected
  86. while (!client.connected()) {
  87. Serial.print("Attempting MQTT connection...");
  88. // Attempt to connect
  89. if (client.connect(USERNAME)) {
  90. Serial.println("connected");
  91. // Once connected, subscribe to input channel
  92. client.subscribe(TOPIC);
  93. } else {
  94. Serial.print("failed, rc=");
  95. Serial.print(client.state());
  96. Serial.println(" try again in 5 seconds");
  97. // Wait 5 seconds before retrying
  98. delay(5000);
  99. }
  100. }
  101. }
  102. void loop() {
  103.  
  104. if (!client.connected()) {
  105. reconnect();
  106. }
  107. client.loop();
  108. delay(10);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement