Advertisement
gozkil

MQTT-buzzer w/ RTTTL

Mar 27th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Basic ESP8266 MQTT example - extended with nonblockingrtttl library and a buzzer...
  3. */
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <PubSubClient.h>
  7. #include <NonBlockingRtttl.h>
  8.  
  9. //project's contants
  10. #define BUZZER_PIN D6
  11. const char *song0 = "s0:d=4,o=5,b=160:8c#6,8a#,2p,8a#,8g#,8f#,8g#,8a#,c#6,8a#,c#6,8d#6,8a#,2p,8a#,8g#,8f#,8g#,8a#,c#6,8a#,c#6,8d#6,8b,2p,8b,8a#,8g#,8a#,8b,d#6,8f#6,d#6,f.6,d#.6,c#.6,b.,a#,g#";
  12. const char *song1 = "s1:d=4,o=5,b=160:e6,8b,8c6,8d6,16e6,16d6,8c6,8b,a,8a,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,2a,8p,d6,8f6,a6,8g6,8f6,e6,8e6,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,a";
  13. const char *song2 = "s2:d=4,o=5,b=140:8g6,16p,16g.6,2a#6,32p,8a6,8g6,8f6,8a6,2g6";
  14. const char *song3 = "s3:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6";
  15. const char *song4 = "s4:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6";
  16.  
  17.  
  18. //The NODEMCU has a button on it - named FLASH - it is connected to D3:
  19. #define  SWITCH_PIN  D3
  20.  
  21.  
  22.  
  23. // Update these with values suitable for your network.
  24.  
  25. const char* ssid = "ali's iPhone";
  26. const char* password = "XXXX";
  27.  
  28. // for the in-class exercise, we'll use the public broker, but for the actual course project:
  29. // set-up a private mqtt broker at cloudmqtt.com (see Canvas for a howto video)
  30. //and uncomment / edit the following:      [ you also need to change the 'client.connect' part of the code below in the code ]
  31.  
  32. //const char *mqtt_server = "XX.cloudmqtt.com";
  33. //const int mqtt_port = XXXXX;
  34. //const char *mqtt_user = "XXXXX";
  35. //const char *mqtt_pass = "XXXXX";
  36.  
  37. const char *mqtt_server = "test.mosquitto.org";
  38. const int mqtt_port = 1883;
  39.  
  40.  
  41. //see the actual function below - this is just a placeholder..
  42. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length);
  43.  
  44. //Let's define some objects:
  45. WiFiClient espClient;
  46.  
  47. //and the mqtt client:
  48. PubSubClient client(mqtt_server, mqtt_port, callback, espClient);
  49.  
  50.  
  51. //THIS IS THE CORE OF THE 'SUBSCRIBE'
  52. //everytime something happens on the topics we've subscribed [down below in the code]; this function will be fired.
  53.  
  54. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
  55.  
  56.   // it has the 'topic' and the message 'payload'
  57.  
  58.   //let's convert the topic name [the first array] to a string:
  59.   String topic;
  60.   topic = String(byteArraytopic);
  61.   Serial.print("Message arrived [");
  62.   Serial.print(topic);
  63.   Serial.print("] ");
  64.  
  65.   // let's convert the incoming message [which arrives as an array of characters] to a string:
  66.   String payload;
  67.   for (int i = 0; i < length; i++) {
  68.     //Serial.print((char)payload[i]);
  69.     payload += (char)byteArrayPayload[i];
  70.   }
  71.   Serial.print("==> ");
  72.   Serial.println(payload);
  73.  
  74.   if (topic == "mekatronik/groupXX/tone")
  75.  
  76.   {
  77.     if ( !rtttl::isPlaying() )
  78.     {
  79.       if (payload=="s0")           rtttl::begin(BUZZER_PIN, song0);
  80.       if (payload=="s1")           rtttl::begin(BUZZER_PIN, song1);
  81.       if (payload=="s2")           rtttl::begin(BUZZER_PIN, song2);
  82.       if (payload=="s3")           rtttl::begin(BUZZER_PIN, song3);
  83.       if (payload=="s4")           rtttl::begin(BUZZER_PIN, song4);
  84.  
  85.       }
  86.     }
  87.  
  88.  
  89.   //now we have the 'topic' and the 'payload'
  90.   //the rest is basically up to you.. this is where you build your 'logic' -
  91.   //depending on what message arrives from which topic
  92.  
  93.   // make an if statement: check if the incoming topic is e.g. "mekatronik/groupX/led"
  94.   // if it is true; check the message: if it is 'ON' turn the LED 'ON', if 'OFF' - turn OFF
  95.  
  96. }
  97.  
  98.  
  99. // Setup the wifi connection...
  100. void setup_wifi() {
  101.  
  102.   delay(10);
  103.   // We start by connecting to a WiFi network
  104.   Serial.println();
  105.   Serial.print("Connecting to ");
  106.   Serial.println(ssid);
  107.  
  108.   WiFi.begin(ssid, password);
  109.  
  110.   while (WiFi.status() != WL_CONNECTED) {
  111.     delay(500);
  112.     Serial.print(".");
  113.   }
  114.  
  115.   Serial.println("");
  116.   Serial.println("WiFi connected");
  117.   Serial.println("IP address: ");
  118.   Serial.println(WiFi.localIP());
  119. }
  120.  
  121.  
  122. // A function to connect to the mqtt server, and reconnect if the connection breaks, wifi breaks, server problem etc..
  123.  
  124. void reconnect() {
  125.   // Loop until we're reconnected
  126.   while (!client.connected()) {
  127.     Serial.print("Attempting MQTT connection...");
  128.     // Attempt to connect
  129.     // CHANGE THE CLIENT NAME to a unique value - e.g. "Group6.1"
  130.     // IF you are using the 'cloudmqtt' broker; you need to change the below function call to :client.connect("UNIQUE_CLIENT_NAME","user","pw")
  131.     // You also need to change server / port setting above - > see the cloud mqtt setup tutorial on canvas if you are in doubt..
  132.     if (client.connect("UNIQUE_CLIENT_NAME_AAA")) {
  133.       Serial.println("connected");
  134.       // Once connected, publish an announcement to the 'welcome' topic...
  135.       client.publish("mekatronik/groupXX", "hello from GroupXX");
  136.       // ... and subscribe to these topics:
  137.       client.subscribe("mekatronik/groupXX/tone");
  138.  
  139.     } else {
  140.       Serial.print("failed, rc=");
  141.       Serial.print(client.state());
  142.       Serial.println(" try again in 5 seconds");
  143.       // Wait 5 seconds before retrying
  144.       delay(5000);
  145.     }
  146.   }
  147. }
  148.  
  149. void setup() {
  150.  
  151.   //NOTE: if the following line does not work - try changing the BUILTIN_LED to LED_BUILTIN
  152.   //if still does not work - attach a real LED to e.g. pin D5..
  153.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  154.  
  155.   pinMode(D3, INPUT_PULLUP);     // Initialize the on-board button [named FLASH] pin as an output
  156.  
  157.   Serial.begin(115200);
  158.   setup_wifi(); // self-explanatory.. check the function to see what it does..
  159.   client.setServer(mqtt_server, mqtt_port); // connect to the mqtt server at the specified port [defined at the top]
  160.  
  161.   // set the 'callback' function, which is defined above.
  162.   // We tell our client to fire the 'callback' function everytime there is a new message on the subscribed topics
  163.   client.setCallback(callback);
  164.  
  165. }
  166.  
  167.  
  168. void loop() {
  169.  
  170.   //if we're not connected to the mqtt broker:
  171.   if (!client.connected()) {
  172.     reconnect();
  173.   }
  174.   client.loop();
  175.   rtttl::play();
  176.  
  177.   //WRITE SOME CODE HERE:
  178.  
  179.   // to check if the button at SWITCH_PIN is pressed [LOW]
  180.   // if it is - publish the message 'pressed' to the topic 'mekatronik/groupXX/btn'
  181.   // the function is : client.publish(topic,message)
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement