Advertisement
Guest User

Wemos D1 Button Shield MQTT

a guest
Jun 26th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char *ssid = "_SSID_"; // cannot be longer than 32 characters!
  5. const char *pass = "_Wifi_password_"; //
  6. const char *mqtt_server = "_IP_Of_MQTT_Server";
  7. const int mqtt_port = 1883;
  8. const char* connection_id = "wemo_button";
  9. const char* client_name = "_USERNAME_";
  10. const char* client_password = "_PASSWORD_";
  11. const char* topic = "wemobutton/state";
  12. char somebigthing[100];
  13.  
  14. const int buttonPin = D3;
  15. const int ledPin = BUILTIN_LED;
  16.  
  17. WiFiClient espClient;
  18. PubSubClient client(espClient);
  19.  
  20. // the current state of the LED and button
  21. int ledState = LOW;
  22. int buttonState = LOW;
  23.  
  24. // the current and previous readings from the input pin
  25. int thisButtonState = LOW;
  26. int lastButtonState = LOW;
  27.  
  28. // time is measured in milliseconds and will quickly exceed limitations of an integer, so we use a long for these two
  29. unsigned long lastDebounceTime = 0; // the time the button state last switched
  30. unsigned long debounceDelay = 50; // the state must remain the same for this many millis to register the button press
  31.  
  32. void setup() {
  33. Serial.begin(9600);
  34. pinMode(buttonPin, INPUT);
  35. client.setServer(mqtt_server, mqtt_port); // MQTT!!!
  36. }
  37.  
  38. void loop() {
  39. Serial.println("Hello world1");
  40. WiFi.begin(ssid, pass);
  41. client.connect(connection_id, client_name, client_password);
  42. Serial.println("Hello world2");
  43. // the buttonPin is read multiple times and the value must remain the same for debounceDelay millis to toggle the LED
  44.  
  45. // read button state, HIGH when pressed, LOW when not
  46. thisButtonState = digitalRead(buttonPin);
  47.  
  48. // if the current state does not match the previous state
  49. // the button was just pressed/released, or is transition noise
  50. if (thisButtonState != lastButtonState) {
  51. // reset the timer
  52. lastDebounceTime = millis();
  53. }
  54.  
  55. // once delay millis have elapsed, if the state remains the same, register the press
  56. if ((millis() - lastDebounceTime) > debounceDelay) {
  57.  
  58. // if the button state has changed
  59. if (thisButtonState != buttonState) {
  60. buttonState = thisButtonState;
  61.  
  62. // only toggle the LED if the buttonState has switched from LOW to HIGH
  63. if (buttonState == HIGH) {
  64. ledState = !ledState;
  65. // toggle the LED
  66. if (ledState == HIGH) {
  67. SendOn();
  68. }
  69. else {
  70. SendOff();
  71. }
  72. }
  73. }
  74. }
  75.  
  76. // persist for next loop iteration
  77. lastButtonState = thisButtonState;
  78. }
  79.  
  80. void SendOn(){
  81. strcpy(somebigthing, "on");
  82. client.publish(topic, somebigthing);
  83. }
  84.  
  85.  
  86. void SendOff(){
  87. strcpy(somebigthing, "off");
  88. client.publish(topic, somebigthing);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement