//#include #include #include #include #include #include #define WIFI_AP "IT Portable Hotspot" #define WIFI_PASSWORD "12345678" #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" 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 upload_led; String tcpcmd_led_on = "LED_Control,1"; String tcpcmd_led_off = "LED_Control,0"; LWiFiClient c2; HttpClient http(c2); 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 */ Serial.println("Connecting to AP"); while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { delay(1000); } Serial.println("calling connection"); while (!c2.connect(SITE_URL, 80)) { Serial.println("Re-Connecting to WebSite"); delay(1000); } delay(100); pinMode(13, OUTPUT); getconnectInfo(); connectTCP(); } 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); 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(); 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(); } } Serial.print("The connection info: "); Serial.println(connection_info); int i; for(i=0;i 10) { c2.stop(); return; } delay(100); } int err = http.skipResponseHeaders(); 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(); } } } 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 void heartBeat(){ Serial.println("send TCP heartBeat"); c.println(tcpdata); c.println(); } //heartBeat void loop() { //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_led_on)){ digitalWrite(13, HIGH); Serial.print("Switch LED ON "); tcpcmd=""; }else if(tcpcmd.substring(40).equals(tcpcmd_led_off)){ digitalWrite(13, LOW); Serial.print("Switch LED OFF"); tcpcmd=""; } } } 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; } }