Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Basic ESP8266 MQTT example - extended with nonblockingrtttl library and a buzzer...
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <NonBlockingRtttl.h>
- //project's contants
- #define BUZZER_PIN D6
- 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#";
- 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";
- const char *song2 = "s2:d=4,o=5,b=140:8g6,16p,16g.6,2a#6,32p,8a6,8g6,8f6,8a6,2g6";
- 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";
- 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";
- //The NODEMCU has a button on it - named FLASH - it is connected to D3:
- #define SWITCH_PIN D3
- // Update these with values suitable for your network.
- const char* ssid = "ali's iPhone";
- const char* password = "XXXX";
- // for the in-class exercise, we'll use the public broker, but for the actual course project:
- // set-up a private mqtt broker at cloudmqtt.com (see Canvas for a howto video)
- //and uncomment / edit the following: [ you also need to change the 'client.connect' part of the code below in the code ]
- //const char *mqtt_server = "XX.cloudmqtt.com";
- //const int mqtt_port = XXXXX;
- //const char *mqtt_user = "XXXXX";
- //const char *mqtt_pass = "XXXXX";
- const char *mqtt_server = "test.mosquitto.org";
- const int mqtt_port = 1883;
- //see the actual function below - this is just a placeholder..
- void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length);
- //Let's define some objects:
- WiFiClient espClient;
- //and the mqtt client:
- PubSubClient client(mqtt_server, mqtt_port, callback, espClient);
- //THIS IS THE CORE OF THE 'SUBSCRIBE'
- //everytime something happens on the topics we've subscribed [down below in the code]; this function will be fired.
- void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
- // it has the 'topic' and the message 'payload'
- //let's convert the topic name [the first array] to a string:
- String topic;
- topic = String(byteArraytopic);
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- // let's convert the incoming message [which arrives as an array of characters] to a string:
- String payload;
- for (int i = 0; i < length; i++) {
- //Serial.print((char)payload[i]);
- payload += (char)byteArrayPayload[i];
- }
- Serial.print("==> ");
- Serial.println(payload);
- if (topic == "mekatronik/groupXX/tone")
- {
- if ( !rtttl::isPlaying() )
- {
- if (payload=="s0") rtttl::begin(BUZZER_PIN, song0);
- if (payload=="s1") rtttl::begin(BUZZER_PIN, song1);
- if (payload=="s2") rtttl::begin(BUZZER_PIN, song2);
- if (payload=="s3") rtttl::begin(BUZZER_PIN, song3);
- if (payload=="s4") rtttl::begin(BUZZER_PIN, song4);
- }
- }
- //now we have the 'topic' and the 'payload'
- //the rest is basically up to you.. this is where you build your 'logic' -
- //depending on what message arrives from which topic
- // make an if statement: check if the incoming topic is e.g. "mekatronik/groupX/led"
- // if it is true; check the message: if it is 'ON' turn the LED 'ON', if 'OFF' - turn OFF
- }
- // Setup the wifi connection...
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- // A function to connect to the mqtt server, and reconnect if the connection breaks, wifi breaks, server problem etc..
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- // CHANGE THE CLIENT NAME to a unique value - e.g. "Group6.1"
- // IF you are using the 'cloudmqtt' broker; you need to change the below function call to :client.connect("UNIQUE_CLIENT_NAME","user","pw")
- // You also need to change server / port setting above - > see the cloud mqtt setup tutorial on canvas if you are in doubt..
- if (client.connect("UNIQUE_CLIENT_NAME_AAA")) {
- Serial.println("connected");
- // Once connected, publish an announcement to the 'welcome' topic...
- client.publish("mekatronik/groupXX", "hello from GroupXX");
- // ... and subscribe to these topics:
- client.subscribe("mekatronik/groupXX/tone");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- //NOTE: if the following line does not work - try changing the BUILTIN_LED to LED_BUILTIN
- //if still does not work - attach a real LED to e.g. pin D5..
- pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
- pinMode(D3, INPUT_PULLUP); // Initialize the on-board button [named FLASH] pin as an output
- Serial.begin(115200);
- setup_wifi(); // self-explanatory.. check the function to see what it does..
- client.setServer(mqtt_server, mqtt_port); // connect to the mqtt server at the specified port [defined at the top]
- // set the 'callback' function, which is defined above.
- // We tell our client to fire the 'callback' function everytime there is a new message on the subscribed topics
- client.setCallback(callback);
- }
- void loop() {
- //if we're not connected to the mqtt broker:
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- rtttl::play();
- //WRITE SOME CODE HERE:
- // to check if the button at SWITCH_PIN is pressed [LOW]
- // if it is - publish the message 'pressed' to the topic 'mekatronik/groupXX/btn'
- // the function is : client.publish(topic,message)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement