If you could make your very own digital, Internet-connected piano, what would you play? In this post we will teach you how to make program a buzzer using a NodeMCU and Ubidots.

This may sound hard at the beginning but with Ubidots, it's pretty simple. We will use a NodeMcu connected to the Ubidots and establish some tones to a few buzzers hooked up to Ubidots.  Now let's make some music! 

Overview

In this post you will learn how to use the NodeMcu with a buzzer and Ubidots. The NodeMcu is an IoT device with GPIO, PWM, IIC, 1-Wire and ADC all in one board, and connects via Wi-Fi. It is easy to learn and prototype with and with the Arduino IDE, programming is a sinch.

Requirements

First, you need the following:

Setup

  1. First, couple the NodeMcu to its base shield and connect the buzzer to pin D1. See photo below for reference:

2. Next, go to the Arduino IDE, click on Files -> Preferences and search "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into the Additional Board Manager URLs field. You can add multiple URLs, separating them with commas if needed.

3. Then open "Boards Manager" from Tools -> Board menu and install "esp8266" platform.

3a. Don’t forget to select your NodeMCU 1.0 board from Tools > Board menu after installation.

4. If you haven't already, download the UbidotsESPMQTT library and click on Sketch -> Include Library -> Add .ZIP Library

6. Select the .ZIP file of Ubidots ESPMQTT and then “Accept” or “Choose” for all the libraries. If you can't add the library, try manually adding by unzipping the downloaded rar / zip and copy the folder from the library to the path C:\Users\ubidots\Documents\Arduino\libraries

7. Reboot Arduino IDE. 

Setting up Ubidots

  1. Add a new Data Source called "piano".

2. Add a new Variable for each musical note. Start with a variable called "do", and then "re", "mi", "fa", "so", "la", "ti". 

3. Verify that the label of the Data Source and the Variables are the same as the name. This allows the communication between Ubidots and the NodeMcu.

4.Create the buttons/switches that allow you to play each musical note. To do this, go to Dashboard and in the upper right of the page click Add widget. Select Control > Switch > piano (data source) > do (variable) > finish. Don't forget do the same for each musical note. When complete it will look like this:

Program the ESP NodeMcu

Once everything is connected correctly, we will go to the IDE and paste the following code.

/**************************************** * Include Libraries ****************************************/#include "UbidotsESPMQTT.h"#include <map>/**************************************** * Define Constants ****************************************/#define TOKEN "..." // Your Ubidots TOKEN#define WIFINAME "..." //Your SSID#define WIFIPASS "..." // Your Wifi Pass#define MQTTCLIENTNAME "sdfcvdhjnbvxw" // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name#define MQTTCLIENTNAME2 "sdfcvdhjnbvxw2"#define MQTTCLIENTNAME3 "sdfcvdhjnbvxw3"#define MQTTCLIENTNAME4 "sdfcvdhjnbvxw4"#define MQTTCLIENTNAME5 "sdfcvdhjnbvxw5"#define MQTTCLIENTNAME6 "sdfcvdhjnbvxw6"#define MQTTCLIENTNAME7 "sdfcvdhjnbvxw7"//Piano constants#define BUZZER D1 //Buzzer pinconst int DO  = 262;const int RE  = 294;const int MI  = 330;const int FA  = 349;const int SOL = 392;const int LA  = 440;const int SI  = 494;int basic_notes[ ] = {DO, RE, MI, FA, SOL, LA, SI};String ubi_switch[ ] = {"do", "re", "mi", "fa", "sol", "la", "si"};Ubidots client(TOKEN, MQTTCLIENTNAME);Ubidots client2(TOKEN, MQTTCLIENTNAME2);Ubidots client3(TOKEN, MQTTCLIENTNAME3);Ubidots client4(TOKEN, MQTTCLIENTNAME4);Ubidots client5(TOKEN, MQTTCLIENTNAME5);Ubidots client6(TOKEN, MQTTCLIENTNAME6);Ubidots client7(TOKEN, MQTTCLIENTNAME7);/**************************************** * Define Initial Values ****************************************//**************************************** * Auxiliar Functions ****************************************/void callback(char* topic, byte* payload, unsigned int length) {  Serial.print("Message arrived [");  Serial.print(topic);  Serial.print("] ");  int flag = 0;      for (int i=0;i<length;i++) {    Serial.print((char)payload[i]);  }  if ((char)payload[0]=='1'){    digitalWrite(16, HIGH);    tone(BUZZER, basic_notes[note(topic)]);    delay((1000/4)*1.30);    noTone(BUZZER);  }  else{    digitalWrite(16, LOW);    noTone(BUZZER);  }  Serial.println();  }int note(char* topic){  char * pch;  char * token = strtok(topic, "/");    while (token != NULL){      for (int i=0; i<8; i++){        if(String(token)==ubi_switch[i]){          Serial.println("");          Serial.println(ubi_switch[i]);          Serial.println(basic_notes[i]);          return i;          //tone(BUZZER, basic_notes[i]);          break;        }      }  token = strtok(NULL, "/");  }  }/**************************************** * Main Functions ****************************************/void setup() {  // put your setup code here, to run once:  Serial.begin(115200);  client.wifiConnection(WIFINAME, WIFIPASS);  client.begin(callback);  client2.begin(callback);  client3.begin(callback);  client4.begin(callback);  client5.begin(callback);  client6.begin(callback);  client7.begin(callback);  client.ubidotsSubscribe("piano", "do");  client2.ubidotsSubscribe("piano", "re");  client3.ubidotsSubscribe("piano", "mi");  client4.ubidotsSubscribe("piano", "fa");  client5.ubidotsSubscribe("piano", "sol");  //client6.ubidotsSubscribe("piano", "la");  //client7.ubidotsSubscribe("piano", "si");  pinMode(16, OUTPUT);  }void loop() {  //Send to Ubidots Routine  if(!client.connected()){      client.reconnect();      client.ubidotsSubscribe("piano", "do");      client2.ubidotsSubscribe("piano", "re");      client3.ubidotsSubscribe("piano", "mi");      client4.ubidotsSubscribe("piano", "fa");      client5.ubidotsSubscribe("piano", "sol");      //client6.ubidotsSubscribe("piano", "la");      //client7.ubidotsSubscribe("piano", "si");     }  client.loop();  client2.loop();  client3.loop();  client4.loop();  client5.loop();  client6.loop();  client7.loop();  }

view rawDigital_Piano hosted with ❤ by GitHub

Results

And that's it! It's time to play the piano.

We've just shown you how to create a low-cost digital piano based using the Internet of Things (IoT). Now it's time for you to give it a try. Happy Hacking :)

 

Did this answer your question?