Guest User

pubsub

a guest
Aug 3rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1.  
  2. down vote
  3. accepted
  4. This code works if you want to publish while subscribing.
  5.  
  6. /*
  7. * ESP8266 (Adafruit HUZZAH) Mosquitto MQTT Publish Example
  8. * Thomas Varnish (https://github.com/tvarnish), (https://www.instructables.com/member/Tango172)
  9. * Made as part of my MQTT Instructable - "How to use MQTT with the Raspberry Pi and ESP8266"
  10. */
  11. #include <Bounce2.h> // Used for "debouncing" the pushbutton
  12. #include <ESP8266WiFi.h> // Enables the ESP8266 to connect to the local network (via WiFi)
  13. #include <PubSubClient.h> // Allows us to connect to, and publish to the MQTT broker
  14.  
  15. const int ledPin = 0; // This code uses the built-in led for visual feedback that the button has been pressed
  16. const int buttonPin = 13; // Connect your button to pin #13
  17.  
  18. // WiFi
  19. // Make sure to update this for your own WiFi network!
  20. const char* ssid = "TP-LINK_7224";
  21. const char* wifi_password = "RFID7890";
  22.  
  23. // MQTT
  24. // Make sure to update this for your own MQTT Broker!
  25. const char* mqtt_server = "192.168.58.117";
  26. const char* mqtt_topic = "Flash_Message";
  27. const char* mqtt_topic_sub = "flash";
  28. const char* mqtt_username = "pi";
  29. const char* mqtt_password = "pi123";
  30. // The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg).
  31. const char* clientID = "ESP01";
  32.  
  33. // Initialise the Pushbutton Bouncer object
  34. Bounce bouncer = Bounce();
  35.  
  36. // Initialise the WiFi and MQTT Client objects
  37. WiFiClient wifiClient;
  38. PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker
  39.  
  40. void ReceivedMessage(char* topic, byte* payload, unsigned int length) {
  41. // Output the first character of the message to serial (debug)
  42. Serial.println((char)payload[0]);
  43.  
  44. // Handle the message we received
  45. // Here, we are only looking at the first character of the received message (payload[0])
  46. // If it is 0, turn the led off.
  47. // If it is 1, turn the led on.
  48. if ((char)payload[0] == 'E' && (char)payload[1] == 'S' && (char)payload[2] == 'P' && (char)payload[3] == '0' && (char)payload[4] == '1' ) {
  49.  
  50. digitalWrite(ledPin, HIGH);
  51. delay(4000);// Notice for the HUZZAH Pin 0, HIGH is OFF and LOW is ON. Normally it is the other way around.
  52. digitalWrite(ledPin, LOW);
  53. }
  54. if ((char)payload[0] == '0') {
  55. digitalWrite(ledPin, LOW);
  56. delay(1000);
  57. }
  58. }
  59.  
  60. void setup() {
  61. pinMode(ledPin, OUTPUT);
  62. pinMode(buttonPin, INPUT);
  63.  
  64. // Switch the on-board LED off to start with
  65. digitalWrite(ledPin, HIGH);
  66.  
  67. // Setup pushbutton Bouncer object
  68. bouncer.attach(buttonPin);
  69. bouncer.interval(5);
  70.  
  71. // Begin Serial on 115200
  72. // Remember to choose the correct Baudrate on the Serial monitor!
  73. // This is just for debugging purposes
  74. Serial.begin(115200);
  75.  
  76. Serial.print("Connecting to ");
  77. Serial.println(ssid);
  78.  
  79. // Connect to the WiFi
  80. WiFi.begin(ssid, wifi_password);
  81.  
  82. // Wait until the connection has been confirmed before continuing
  83. while (WiFi.status() != WL_CONNECTED) {
  84. delay(500);
  85. Serial.print(".");
  86. }
  87.  
  88. // Debugging - Output the IP Address of the ESP8266
  89. Serial.println("WiFi connected");
  90. Serial.print("IP address: ");
  91. Serial.println(WiFi.localIP());
  92. client.setCallback(ReceivedMessage);
  93. // Connect to MQTT Broker
  94. // client.connect returns a boolean value to let us know if the connection was successful.
  95. // If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
  96. if (client.connect(clientID, mqtt_username, mqtt_password)) {
  97. client.subscribe(mqtt_topic_sub);
  98. Serial.println("Connected to MQTT Broker!");
  99. }
  100. else {
  101. Serial.println("Connection to MQTT Broker failed...");
  102. }
  103.  
  104. }
  105.  
  106. bool Connect() {
  107. // Connect to MQTT Server and subscribe to the topic
  108. if (client.connect(clientID, mqtt_username, mqtt_password)) {
  109. client.subscribe(mqtt_topic_sub);
  110. return true;
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116.  
  117.  
  118. void loop() {
  119. // Update button state
  120. // This needs to be called so that the Bouncer object can check if the button has been pressed
  121. bouncer.update();
  122.  
  123. if (bouncer.rose()) {
  124. // Turn light on when button is pressed down
  125. // (i.e. if the state of the button rose from 0 to 1 (not pressed to pressed))
  126. digitalWrite(ledPin, LOW);
  127. if (!client.connected()) {
  128. Connect();
  129. }
  130. // PUBLISH to the MQTT Broker (topic = mqtt_topic, defined at the beginning)
  131. // Here, "Button pressed!" is the Payload, but this could be changed to a sensor reading, for example.
  132. if (client.publish(mqtt_topic, "Button pressed!")) {
  133. Serial.println("Button pushed and message sent!");
  134. }
  135. // Again, client.publish will return a boolean value depending on whether it succeded or not.
  136. // If the message failed to send, we will try again, as the connection may have broken.
  137. else {
  138. Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
  139. client.connect(clientID, mqtt_username, mqtt_password);
  140. delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
  141. client.publish(mqtt_topic, "Button pressed!");
  142. }
  143. }
  144. else if (bouncer.fell()) {
  145. // Turn light off when button is released
  146. // i.e. if state goes from high (1) to low (0) (pressed to not pressed)
  147. digitalWrite(ledPin, HIGH);
  148. }
  149. client.loop();
  150. }
Add Comment
Please, Sign In to add comment