1+ // Example to start ESP8266 in soft access point / hotspot mode
2+ // and also enable mDNS response on local network. This allows
3+ // client to discover the AppleMIDI service and connect to it
4+ // without having to type the IP address and port
5+ // Tested on iOS 9 (old iPad) and iOS 13 (iPhone 6)
6+ // On Win10 (rtpMIDI), ESP8266 did not show in directory,
7+ // but connects fine with default IP(192.168.4.1)/port(5004)
8+
9+ #include < ESP8266WiFi.h>
10+ #include < ESP8266mDNS.h>
11+ #include < WiFiClient.h>
12+ #include < WiFiUdp.h>
13+
14+ #define SerialMon Serial
15+ #define APPLEMIDI_DEBUG SerialMon
16+ #include < AppleMIDI.h>
17+
18+ char ssid[] = " ssid" ; // your network SSID (name)
19+ char pass[] = " password" ; // your network password (use for WPA, or use as key for WEP)
20+
21+ unsigned long t0 = millis();
22+ int8_t isConnected = 0 ;
23+
24+ APPLEMIDI_CREATE_DEFAULTSESSION_INSTANCE ();
25+
26+ // -----------------------------------------------------------------------------
27+ //
28+ // -----------------------------------------------------------------------------
29+ void setup ()
30+ {
31+ DBG_SETUP (115200 );
32+ DBG (" Booting" );
33+
34+ WiFi.softAP (ssid, pass);
35+
36+ DBG (F (" Started soft access point:" ), WiFi.softAPIP (), " Port" , AppleMIDI.getPort ());
37+ DBG (F (" AppleMIDI device name:" ), AppleMIDI.getName ());
38+ // Set up mDNS responder:
39+ if (!MDNS.begin (AppleMIDI.getName ()))
40+ DBG (F (" Error setting up MDNS responder!" ));
41+ char str[128 ] = " " ;
42+ strcat (str, AppleMIDI.getName ());
43+ strcat (str," .local" );
44+ DBG (F (" mDNS responder started at:" ), str);
45+ MDNS.addService (" apple-midi" , " udp" , AppleMIDI.getPort ());
46+ DBG (F (" Open Wifi settings and connect to soft acess point using 'ssid'" ));
47+ DBG (F (" Start MIDI Network app on iPhone/iPad or rtpMIDI on Windows" ));
48+ DBG (F (" AppleMIDI-ESP8266 will show in the 'Directory' list (rtpMIDI) or" ));
49+ DBG (F (" under 'Found on the network' list (iOS). Select and click 'Connect'" ));
50+
51+ MIDI.begin ();
52+
53+ AppleMIDI.setHandleConnected ([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc, const char * name) {
54+ isConnected++;
55+ DBG (F (" Connected to session" ), ssrc, name);
56+ });
57+ AppleMIDI.setHandleDisconnected ([](const APPLEMIDI_NAMESPACE::ssrc_t & ssrc) {
58+ isConnected--;
59+ DBG (F (" Disconnected" ), ssrc);
60+ });
61+
62+ MIDI.setHandleNoteOn ([](byte channel, byte note, byte velocity) {
63+ DBG (F (" NoteOn" ), note);
64+ });
65+ MIDI.setHandleNoteOff ([](byte channel, byte note, byte velocity) {
66+ DBG (F (" NoteOff" ), note);
67+ });
68+
69+ DBG (F (" Sending NoteOn/Off of note 45, every second" ));
70+ }
71+
72+ // -----------------------------------------------------------------------------
73+ //
74+ // -----------------------------------------------------------------------------
75+ void loop ()
76+ {
77+
78+ MDNS.update ();
79+
80+ // Listen to incoming notes
81+ MIDI.read ();
82+
83+ // send a note every second
84+ // (dont cáll delay(1000) as it will stall the pipeline)
85+ if ((isConnected > 0 ) && (millis () - t0) > 1000 )
86+ {
87+ t0 = millis ();
88+
89+ byte note = 45 ;
90+ byte velocity = 55 ;
91+ byte channel = 1 ;
92+
93+ MIDI.sendNoteOn (note, velocity, channel);
94+ MIDI.sendNoteOff (note, velocity, channel);
95+ }
96+ }
0 commit comments