Stap 2: Code
De code is rommelig, maar het werkt. Op dit ogenblik u kunt alleen liedjes afspelen en stoppen hen.
Sommige dingen op te merken. Moet u voor het invoeren van uw naam van het toegangspunt en het wachtwoord in de code. Ook uw test-apparaat apparaat-id en sleutel. Als je niet weet hoe dat te doen check out dit instructable waar ik die stappen in meer detail uitleggen.
<p>//#include <br>#include #include #include #include #include #include #include #define WIFI_AP "" #define WIFI_PASSWORD "" #define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP. #define per 50 #define per1 3 #define DEVICEID "" // Input your deviceId #define DEVICEKEY "" // Input your deviceKey #define SITE_URL "api.mediatek.com"</p><p>LWiFiClient c; unsigned int rtc; unsigned int lrtc; unsigned int rtc1; unsigned int lrtc1; char port[4]={0}; char connection_info[21]={0}; char ip[21]={0}; int portnum; int val = 0; String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; String music_status; String tcpcmd_play1_on = "play1,1"; String tcpcmd_play1_off = "play1,0"; String tcpcmd_play2_on = "play2,1"; String tcpcmd_play2_off = "play2,0"; String tcpcmd_play3_on = "play3,1"; String tcpcmd_play3_off = "play3,0"; String tcpcmd_play4_on = "play4,1"; String tcpcmd_play4_off = "play4,0"; String tcpcmd_play5_on = "play5,1"; String tcpcmd_play5_off = "play5,0"; String tcpcmd_play6_on = "play6,1"; String tcpcmd_play6_off = "play6,0"; String tcpcmd_play7_on = "play7,1"; String tcpcmd_play7_off = "play7,0"; String tcpcmd_play8_on = "play8,1"; String tcpcmd_play8_off = "play8,0"; String tcpcmd_play9_on = "play9,1"; String tcpcmd_play9_off = "play9,0"; String tcpcmd_play10_on = "play10,1"; String tcpcmd_play10_off = "play10,0";</p><p>LWiFiClient c2; HttpClient http(c2);</p><p>void setup() { LTask.begin(); LWiFi.begin(); Serial.begin(115200); /*while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */</p><p> Serial.println("Connecting to AP"); while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { delay(1000); } Serial.println("calling connection");</p><p> while (!c2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100);</p><p> getconnectInfo(); connectTCP(); }</p><p>void getconnectInfo(){ //calling RESTful API to get TCP socket connection c2.print("GET /mcs/v2/devices/"); c2.print(DEVICEID); c2.println("/connections.csv HTTP/1.1"); c2.print("Host: "); c2.println(SITE_URL); c2.print("deviceKey: "); c2.println(DEVICEKEY); c2.println("Connection: close"); c2.println(); delay(500);</p><p> int errorcount = 0; while (!c2.available()) { Serial.println("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { c2.stop(); return; } delay(100); } int err = http.skipResponseHeaders();</p><p> int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); char c; int ipcount = 0; int count = 0; int separater = 0; while (c2) { int v = c2.read(); if (v != -1) { c = v; Serial.print(c); connection_info[ipcount]=c; if(c==',') separater=ipcount; ipcount++; } else { Serial.println("no more content, disconnect"); c2.stop();</p><p> } } Serial.print("The connection info: "); Serial.println(connection_info); int i; for(i=0;i</p><p>} //getconnectInfo</p><p>void uploadstatus(){ //calling RESTful API to upload datapoint to MCS to report Music status Serial.println("calling connection"); LWiFiClient c2; </p><p> while (!c2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); if(digitalRead(13)==1) music_status = "music_stat,,1"; else music_status = "music_stat,,0"; int thislength = music_status.length(); HttpClient http(c2); c2.print("POST /mcs/v2/devices/"); c2.print(DEVICEID); c2.println("/datapoints.csv HTTP/1.1"); c2.print("Host: "); c2.println(SITE_URL); c2.print("deviceKey: "); c2.println(DEVICEKEY); c2.print("Content-Length: "); c2.println(thislength); c2.println("Content-Type: text/csv"); c2.println("Connection: close"); c2.println(); c2.println(music_status); delay(500);</p><p> int errorcount = 0; while (!c2.available()) { Serial.print("waiting HTTP response: "); Serial.println(errorcount); errorcount += 1; if (errorcount > 10) { c2.stop(); return; } delay(100); } int err = http.skipResponseHeaders();</p><p> int bodyLen = http.contentLength(); Serial.print("Content length is: "); Serial.println(bodyLen); Serial.println(); while (c2) { int v = c2.read(); if (v != -1) { Serial.print(char(v)); } else { Serial.println("no more content, disconnect"); c2.stop();</p><p> } } }</p><p>void connectTCP(){ //establish TCP connection with TCP Server with designate IP and Port c.stop(); Serial.println("Connecting to TCP"); Serial.println(ip); Serial.println(portnum); while (0 == c.connect(ip, portnum)) { Serial.println("Re-Connecting to TCP"); delay(1000); } Serial.println("send TCP connect"); c.println(tcpdata); c.println(); Serial.println("waiting TCP response:"); } //connectTCP</p><p>void heartBeat(){ Serial.println("send TCP heartBeat"); c.println(tcpdata); c.println(); } //heartBeat</p><p>void loop() //Red led { //Check for TCP socket command from MCS Server String tcpcmd=""; while (c.available()) { int v = c.read(); if (v != -1) { Serial.print((char)v); tcpcmd += (char)v; if (tcpcmd.substring(40).equals(tcpcmd_play1_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play1_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play2_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play2_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play3_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play3_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play4_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play4_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play5_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play5_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play6_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play6_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; }</p><p> else if (tcpcmd.substring(40).equals(tcpcmd_play7_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play7_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play8_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"001.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play8_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play9_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"009.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play9_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } else if (tcpcmd.substring(40).equals(tcpcmd_play10_on)){ LAudio.setVolume(3); LAudio.playFile( storageSD,(char*)"010.mp3"); Serial.print("Play Song 1"); tcpcmd=""; } else if(tcpcmd.substring(40).equals(tcpcmd_play10_off)){ LAudio.stop(); Serial.println("stop"); tcpcmd=""; } } }</p><p> LDateTime.getRtc(&rtc); if ((rtc - lrtc) >= per) { heartBeat(); lrtc = rtc; } //Check for report datapoint status interval LDateTime.getRtc(&rtc1); if ((rtc1 - lrtc1) >= per1) { uploadstatus(); lrtc1 = rtc1; } }</p>