Advertisement
Guest User

Code for button

a guest
Jun 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. //The NODEMCU has a button on it - named FLASH - it is connected to D3:
  5. #define SWITCH_PIN D3
  6. #define buttonPin D7
  7.  
  8. int buttonState = 0;
  9. int spamFix = 0;
  10.  
  11. // Update these with values suitable for your network.
  12.  
  13. const char* ssid = "DSB Free Wifi";
  14. const char* password = "skridspader";
  15.  
  16. // for the in-class exercise, we'll use the public broker, but for the actual course project:
  17. // set-up a private mqtt broker at cloudmqtt.com (see Canvas for a howto video)
  18. //and uncomment / edit the following: [ you also need to change the 'client.connect' part of the code below in the code ]
  19.  
  20. //const char *mqtt_server = "mXX.cloudmqtt.com";
  21. //const int mqtt_port = 17323;
  22. const char *mqtt_server = "m21.cloudmqtt.com";
  23. const int mqtt_port = 14939;
  24. const char *mqtt_user = "button1";
  25. const char *mqtt_pass = "hejhejhej";
  26.  
  27.  
  28. //see the actual function below - this is just a placeholder..
  29. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length);
  30.  
  31. //Let's define some objects:
  32. WiFiClient espClient;
  33.  
  34. //and the mqtt client:
  35. PubSubClient client(mqtt_server,mqtt_port,callback,espClient);
  36.  
  37.  
  38. //THIS IS THE CORE OF THE 'SUBSCRIBE'
  39. //everytime something happens on the topics we've subscribed [down below in the code]; this function will be fired.
  40.  
  41. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
  42.  
  43. // it has the 'topic' and the message 'payload'
  44.  
  45. //let's convert the topic name [the first array] to a string:
  46. String topic;
  47. topic=String(byteArraytopic);
  48. Serial.print("Message arrived [");
  49. Serial.print(topic);
  50. Serial.print("] ");
  51.  
  52. // let's convert the incoming message [which arrives as an array of characters] to a string:
  53. String payload;
  54. for (int i = 0; i < length; i++) {
  55. //Serial.print((char)payload[i]);
  56. payload+=(char)byteArrayPayload[i];
  57. }
  58.  
  59. if (String(topic) == "groupsix/controller_online"){
  60. client.publish("groupsix/readyCheckBut1", "1");
  61. }
  62. }
  63.  
  64. // Setup the wifi connection...
  65. void setup_wifi() {
  66.  
  67. delay(10);
  68. // We start by connecting to a WiFi network
  69. Serial.println();
  70. Serial.print("Connecting to ");
  71. Serial.println(ssid);
  72.  
  73. WiFi.begin(ssid, password);
  74.  
  75. while (WiFi.status() != WL_CONNECTED) {
  76. delay(500);
  77. Serial.print(".");
  78. }
  79.  
  80. Serial.println("");
  81. Serial.println("WiFi connected");
  82. Serial.println("IP address: ");
  83. Serial.println(WiFi.localIP());
  84. }
  85.  
  86.  
  87. // A function to connect to the mqtt server, and reconnect if the connection breaks, wifi breaks, server problem etc..
  88.  
  89. void reconnect() {
  90. // Loop until we're reconnected
  91. while (!client.connected()) {
  92. Serial.print("Attempting MQTT connection...");
  93. // Attempt to connect:
  94. // if you are using cloudmqtt, uncomment the following line, and comment the one below...
  95. if (client.connect("but1","button1","hejhejhej")){ // make sure 'myclientID' is something unique, e.g. 'groupXX'
  96. //if (client.connect("myclientID")) { // make sure 'myclientID' is something unique, e.g. 'groupXX'
  97.  
  98. Serial.println("connected");
  99. // Once connected, publish an announcement...
  100. client.publish("groupsix/readyCheckBut1", "1");
  101. // ... and resubscribe to all messages coming to the 'OLED' topic...
  102.  
  103. client.subscribe("groupsix/#");
  104.  
  105. } else {
  106. Serial.print("failed, rc=");
  107. Serial.print(client.state());
  108. Serial.println(" try again in 5 seconds");
  109. // Wait 5 seconds before retrying
  110. delay(5000);
  111. }
  112. }
  113. }
  114.  
  115. void setup() {
  116.  
  117. //NOTE: if the following line does not work - try changing the BUILTIN_LED to LED_BUILTIN
  118. //if still does not work - attach a real LED to e.g. pin D5..
  119. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  120.  
  121. pinMode(D3, INPUT_PULLUP); // Initialize the on-board button [named FLASH] pin as an output
  122.  
  123. pinMode(buttonPin, INPUT);
  124.  
  125. Serial.begin(115200);
  126. setup_wifi(); // self-explanatory.. check the function to see what it does..
  127. client.setServer(mqtt_server, mqtt_port); // connect to the mqtt server at the specified port [defined at the top]
  128.  
  129. // set the 'callback' function, which is defined above.
  130. // We tell our client to fire the 'callback' function everytime there is a new message on the subscribed topics
  131. client.setCallback(callback);
  132.  
  133. }
  134.  
  135.  
  136. void loop() {
  137.  
  138. //if we're not connected to the mqtt broker:
  139. if (!client.connected()) {
  140. reconnect();
  141. }
  142. client.loop();
  143.  
  144. Serial.println(buttonState);
  145. buttonState = digitalRead(buttonPin);
  146.  
  147. if (buttonState==LOW){
  148. spamFix=0;
  149. }
  150. if (buttonState==HIGH){
  151. if (spamFix==0){
  152. client.publish("groupsix/b1p", "1");
  153. spamFix=1;
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement