Guest User

Untitled

a guest
Sep 25th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [code]
  2. /*
  3.  WiFiEsp example: ConnectWPA
  4.  
  5.  This example connects to an encrypted WiFi network using an ESP8266 module.
  6.  Then it prints the  MAC address of the WiFi shield, the IP address obtained
  7.  and other network details.
  8.  For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-connect.html
  9. */
  10.  
  11. #include "WiFiEsp.h"
  12. #include "WiFiEspUdp.h"
  13.  
  14. #include "AppleMidi.h"
  15.  
  16. APPLEMIDI_CREATE_INSTANCE(WiFiEspUDP, AppleMIDI);
  17.  
  18.  
  19.  
  20. char ssid[] = "cie_yang_gak_punya_paket";            // your network SSID (name)
  21. char pass[] = "gakadapassword";        // your network password
  22. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  23.  
  24.  
  25. unsigned long t0 = millis();
  26. bool isConnected = false;
  27.  
  28.  
  29. void setup()
  30. {
  31.   // initialize serial for debugging
  32.   Serial.begin(115200);
  33.   // initialize serial for ESP module
  34.   Serial3.begin(115200);
  35.   // initialize ESP module
  36.   WiFi.init(&Serial3);
  37.  
  38.   // check for the presence of the shield
  39.   if (WiFi.status() == WL_NO_SHIELD) {
  40.     Serial.println("WiFi shield not present");
  41.     // don't continue
  42.     while (true);
  43.   }
  44.  
  45.   // attempt to connect to WiFi network
  46.   while ( status != WL_CONNECTED) {
  47.     Serial.print("Attempting to connect to WPA SSID: ");
  48.     Serial.println(ssid);
  49.     // Connect to WPA/WPA2 network
  50.     status = WiFi.begin(ssid, pass);
  51.   }
  52.  
  53.   Serial.println("You're connected to the network");
  54.   // print the network connection information every 10 seconds
  55.   Serial.println();
  56.   printCurrentNet();
  57.   printWifiData();
  58.  
  59.   Serial.println("OK, now make sure you an rtpMIDI session that is Enabled");
  60.   Serial.print("Add device named Arduino with Port ");
  61.   Serial.println(":5004");
  62.   Serial.println("Then press the Connect button");
  63.   Serial.println("Then open a MIDI listener (eg MIDI-OX) and monitor incoming notes");
  64.  
  65.   // Create a session and wait for a remote host to connect to us
  66.   AppleMIDI.begin("test");
  67.  
  68.   AppleMIDI.OnConnected(OnAppleMidiConnected);
  69.   AppleMIDI.OnDisconnected(OnAppleMidiDisconnected);
  70.  
  71.   AppleMIDI.OnReceiveNoteOn(OnAppleMidiNoteOn);
  72.   AppleMIDI.OnReceiveNoteOff(OnAppleMidiNoteOff);
  73.  
  74.   Serial.println("Sending NoteOn/Off of note 45, every second");
  75.  
  76. }
  77.  
  78. void loop()
  79. {
  80.   // Listen to incoming notes
  81.   AppleMIDI.run();
  82.  
  83.   // send a note every second
  84.   // (dont cรกll delay(1000) as it will stall the pipeline)
  85.   if (isConnected && (millis() - t0) > 1000)
  86.   {
  87.     t0 = millis();
  88.     //   Serial.print(".");
  89.  
  90.     int note = 45;
  91.     int velocity = 55;
  92.     int channel = 2;
  93.  
  94.     AppleMIDI.noteOn(note, velocity, channel);
  95.     Serial.println("Note on sent");
  96.     AppleMIDI.noteOff(note, velocity, channel);
  97.     Serial.println("Note off sent");
  98.   }
  99. }
  100.  
  101. void printWifiData()
  102. {
  103.   // print your WiFi shield's IP address
  104.   IPAddress ip = WiFi.localIP();
  105.   Serial.print("IP Address: ");
  106.   Serial.println(ip);
  107.  
  108.   // print your MAC address
  109.   byte mac[6];
  110.   WiFi.macAddress(mac);
  111.   char buf[20];
  112.   sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  113.   Serial.print("MAC address: ");
  114.   Serial.println(buf);
  115.  
  116.  
  117. }
  118.  
  119. void printCurrentNet()
  120. {
  121.   // print the SSID of the network you're attached to
  122.   Serial.print("SSID: ");
  123.   Serial.println(WiFi.SSID());
  124.  
  125.   // print the MAC address of the router you're attached to
  126.   byte bssid[6];
  127.   WiFi.BSSID(bssid);
  128.   char buf[20];
  129.   sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  130.   Serial.print("BSSID: ");
  131.   Serial.println(buf);
  132.  
  133.   // print the received signal strength
  134.   long rssi = WiFi.RSSI();
  135.   Serial.print("Signal strength (RSSI): ");
  136.   Serial.println(rssi);
  137. }
  138.  
  139.  
  140.  
  141. // ====================================================================================
  142. // Event handlers for incoming MIDI messages
  143. // ====================================================================================
  144.  
  145. // -----------------------------------------------------------------------------
  146. // rtpMIDI session. Device connected
  147. // -----------------------------------------------------------------------------
  148. void OnAppleMidiConnected(uint32_t ssrc, char* name) {
  149.   isConnected = true;
  150.   Serial.print("Connected to session ");
  151.   Serial.println(name);
  152. }
  153.  
  154. // -----------------------------------------------------------------------------
  155. // rtpMIDI session. Device disconnected
  156. // -----------------------------------------------------------------------------
  157. void OnAppleMidiDisconnected(uint32_t ssrc) {
  158.   isConnected = false;
  159.   Serial.println("Disconnected");
  160. }
  161.  
  162. // -----------------------------------------------------------------------------
  163. //
  164. // -----------------------------------------------------------------------------
  165. void OnAppleMidiNoteOn(byte channel, byte note, byte velocity) {
  166.   Serial.print("Incoming NoteOn from channel:");
  167.   Serial.print(channel);
  168.   Serial.print(" note:");
  169.   Serial.print(note);
  170.   Serial.print(" velocity:");
  171.   Serial.print(velocity);
  172.   Serial.println();
  173. }
  174.  
  175. // -----------------------------------------------------------------------------
  176. //
  177. // -----------------------------------------------------------------------------
  178. void OnAppleMidiNoteOff(byte channel, byte note, byte velocity) {
  179.   Serial.print("Incoming NoteOff from channel:");
  180.   Serial.print(channel);
  181.   Serial.print(" note:");
  182.   Serial.print(note);
  183.   Serial.print(" velocity:");
  184.   Serial.print(velocity);
  185.   Serial.println();
  186. }
  187. [/code]
Advertisement
Add Comment
Please, Sign In to add comment