Behind today’s door is a piezo sound transducer. It’s possible to generate sounds with it, which will become clear in today’s test. Today you’re building a type of piano that can be used via a website. See the following picture for the layout:

Der Piezzo richtig angeschlossen
The correct connection of the piezo sound transducer
The sketch: A16_P16_WebserverPiano.ino
/*
  TCP-Server Piano
  Change SSID and PASSWORD.
*/

#include <NanoESP.h>
#include <NanoESP_HTTP.h> //special library for http-connection
#include <SoftwareSerial.h>
#include "Websites.h" //websites in a special file as progmem

#define SSID "YourSSID"
#define PASSWORD "YourPassword"

#define LED_WLAN 13

NanoESP nanoesp = NanoESP();
NanoESP_HTTP http = NanoESP_HTTP(nanoesp);

void setup() {
  Serial.begin(19200);
  nanoesp.init();

  //! Only if you want to change the ACCESPOINT configuration
  //nanoesp.configWifi(ACCESSPOINT, "NanoESP", "");
  
  //! Only if you use a new Network
  // nanoesp.configWifi(DUAL, SSID, PASSWORD);
    
  //Dual Mode
  nanoesp.configWifiMode(DUAL);
  //delay(1000);
  
  // Wait till Connected to WiFi
  if (nanoesp.wifiConnected())digitalWrite(LED_WLAN, HIGH); else Serial.println(F("Error WLAN Connect"));

  //Start TCP Server
  if (nanoesp.startTcpServer(80))    Serial.println(F("TCP Server Activ")); else   Serial.println(F("TCP Server Error"));

  //Print IP in Terminal
  Serial.println(nanoesp.getIp());
}

void loop() {
  String method, ressource, parameter;
  int id;

  if (http.recvRequest(id, method, ressource, parameter)) { //Incoming request, parameters by reference

    Serial.println("New Request from id :" + String(id) + ", method: " + method +  ", ressource: " + ressource +  " parameter: " + parameter);

    if (ressource == "/") {
      http.sendFromFlash(id, index, sizeof(index));
      nanoesp.closeConnection(id);
    }

    else if (ressource == "/dataonly") {  //only data is recieved on this url, no need to send a website
      nanoesp.closeConnection(id);
    }

    else {
      http.sendFromFlash(id, error404, sizeof(error404));
      nanoesp.closeConnection(id);
    }

    if (parameter != "")      //the request included parameters
    {
      String strTone;
      if (method == GET)  strTone = parameter.substring(6); else   strTone = parameter.substring(5); //get note values
      noTone(8);
      tone(8, strTone.toInt(), 200);    //play note for 200ms
    }

  }
}

(The former Arduino Create embed is no longer available; the code is taken from the download of all sketches.)

When you’ve uploaded the programme, you can open the board’s website using the IP address in the browser. Today it doesn’t matter whether you connect to the board through your home network or the access point, since no external data is loaded. On the website you find a few buttons with which you can trigger a tone when you press them. The transfer is really fast. However, you should give the board a quarter of a second to play the tone before you send another tone. Otherwise, the note sounds very strange. But try to play one of the classics of the beginner songs: All My Ducklings. The notes are:

C D E F G G A A A A G A A A A G F F F F E E G G G G C

Die Piano Webseite
The piano website