|
| 1 | +//#include <b64.h> |
| 2 | +#include <HttpClient.h> |
| 3 | +#include <LTask.h> |
| 4 | +#include <LWiFi.h> |
| 5 | +#include <LWiFiClient.h> |
| 6 | +#include <LDateTime.h> |
| 7 | +#define WIFI_AP "WIFI_AP" //Input your wifi ap name |
| 8 | +#define WIFI_PASSWORD "WIFI_password" // Input your wifi password |
| 9 | +#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP. |
| 10 | +#define per 50 |
| 11 | +#define per1 3 |
| 12 | +#define DEVICEID "*********" // Input your deviceId |
| 13 | +#define DEVICEKEY "*************" // Input your deviceKey |
| 14 | +#define SITE_URL "api.mediatek.io" |
| 15 | + |
| 16 | +LWiFiClient c; |
| 17 | +unsigned int rtc; |
| 18 | +unsigned int lrtc; |
| 19 | +unsigned int rtc1; |
| 20 | +unsigned int lrtc1; |
| 21 | +char port[4]={0}; |
| 22 | +char connection_info[21]={0}; |
| 23 | +char ip[21]={0}; |
| 24 | +int portnum; |
| 25 | +int val = 0; |
| 26 | +String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0"; |
| 27 | +String upload_led; |
| 28 | +String datachannel = "analogTest"; |
| 29 | +int parseString = 40 + datachannel.length() + 1; |
| 30 | +LWiFiClient c2; |
| 31 | +HttpClient http(c2); |
| 32 | + |
| 33 | +void setup() |
| 34 | +{ |
| 35 | + |
| 36 | + LTask.begin(); |
| 37 | + LWiFi.begin(); |
| 38 | + Serial.begin(115200); |
| 39 | + while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */ |
| 40 | + |
| 41 | + Serial.println("Connecting to AP"); |
| 42 | + while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) |
| 43 | + { |
| 44 | + delay(1000); |
| 45 | + } |
| 46 | + |
| 47 | + Serial.println("calling connection"); |
| 48 | + |
| 49 | + while (!c2.connect(SITE_URL, 80)) |
| 50 | + { |
| 51 | + Serial.println("Re-Connecting to WebSite"); |
| 52 | + delay(1000); |
| 53 | + } |
| 54 | + delay(100); |
| 55 | + |
| 56 | + pinMode(13, OUTPUT); |
| 57 | + getconnectInfo(); |
| 58 | + connectTCP(); |
| 59 | +} |
| 60 | + |
| 61 | +void getconnectInfo(){ |
| 62 | + //calling RESTful API to get TCP socket connection |
| 63 | + c2.print("GET /mcs/v2/devices/"); |
| 64 | + c2.print(DEVICEID); |
| 65 | + c2.println("/connections.csv HTTP/1.1"); |
| 66 | + c2.print("Host: "); |
| 67 | + c2.println(SITE_URL); |
| 68 | + c2.print("deviceKey: "); |
| 69 | + c2.println(DEVICEKEY); |
| 70 | + c2.println("Connection: close"); |
| 71 | + c2.println(); |
| 72 | + |
| 73 | + delay(500); |
| 74 | + |
| 75 | + int errorcount = 0; |
| 76 | + while (!c2.available()) |
| 77 | + { |
| 78 | + Serial.println("waiting HTTP response: "); |
| 79 | + Serial.println(errorcount); |
| 80 | + errorcount += 1; |
| 81 | + if (errorcount > 10) { |
| 82 | + c2.stop(); |
| 83 | + return; |
| 84 | + } |
| 85 | + delay(100); |
| 86 | + } |
| 87 | + int err = http.skipResponseHeaders(); |
| 88 | + |
| 89 | + int bodyLen = http.contentLength(); |
| 90 | + Serial.print("Content length is: "); |
| 91 | + Serial.println(bodyLen); |
| 92 | + Serial.println(); |
| 93 | + char c; |
| 94 | + int ipcount = 0; |
| 95 | + int count = 0; |
| 96 | + int separater = 0; |
| 97 | + while (c2) |
| 98 | + { |
| 99 | + int v = c2.read(); |
| 100 | + if (v != -1) |
| 101 | + { |
| 102 | + c = v; |
| 103 | + Serial.print(c); |
| 104 | + connection_info[ipcount]=c; |
| 105 | + if(c==',') |
| 106 | + separater=ipcount; |
| 107 | + ipcount++; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + Serial.println("no more content, disconnect"); |
| 112 | + c2.stop(); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + Serial.print("The connection info: "); |
| 118 | + Serial.println(connection_info); |
| 119 | + int i; |
| 120 | + for(i=0;i<separater;i++) |
| 121 | + { ip[i]=connection_info[i]; |
| 122 | + } |
| 123 | + int j=0; |
| 124 | + separater++; |
| 125 | + for(i=separater;i<21 && j<5;i++) |
| 126 | + { port[j]=connection_info[i]; |
| 127 | + j++; |
| 128 | + } |
| 129 | + Serial.println("The TCP Socket connection instructions:"); |
| 130 | + Serial.print("IP: "); |
| 131 | + Serial.println(ip); |
| 132 | + Serial.print("Port: "); |
| 133 | + Serial.println(port); |
| 134 | + portnum = atoi (port); |
| 135 | + Serial.println(portnum); |
| 136 | + |
| 137 | +} //getconnectInfo |
| 138 | + |
| 139 | +void connectTCP(){ |
| 140 | + //establish TCP connection with TCP Server with designate IP and Port |
| 141 | + c.stop(); |
| 142 | + Serial.println("Connecting to TCP"); |
| 143 | + Serial.println(ip); |
| 144 | + Serial.println(portnum); |
| 145 | + while (0 == c.connect(ip, portnum)) |
| 146 | + { |
| 147 | + Serial.println("Re-Connecting to TCP"); |
| 148 | + delay(1000); |
| 149 | + } |
| 150 | + Serial.println("send TCP connect"); |
| 151 | + c.println(tcpdata); |
| 152 | + c.println(); |
| 153 | + Serial.println("waiting TCP response:"); |
| 154 | +} //connectTCP |
| 155 | + |
| 156 | +void heartBeat(){ |
| 157 | + Serial.println("send TCP heartBeat"); |
| 158 | + c.println(tcpdata); |
| 159 | + c.println(); |
| 160 | + |
| 161 | +} //heartBeat |
| 162 | + |
| 163 | +void loop() |
| 164 | +{ |
| 165 | + //Check for TCP socket command from MCS Server |
| 166 | + String tcpcmd = ""; |
| 167 | + String Value = ""; |
| 168 | + bool processed = 0; |
| 169 | + while (c.available()) |
| 170 | + { |
| 171 | + int v = c.read(); |
| 172 | + if (v != -1) |
| 173 | + { |
| 174 | + tcpcmd += (char)v; |
| 175 | + Value = tcpcmd.substring(parseString); |
| 176 | + if (Value.length() > 0) { |
| 177 | + processed = 1; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (processed){ |
| 183 | + Serial.println(Value); |
| 184 | + analogWrite(3, Value.toInt()); |
| 185 | + Value=""; |
| 186 | + tcpcmd=""; |
| 187 | + } |
| 188 | + |
| 189 | + LDateTime.getRtc(&rtc); |
| 190 | + if ((rtc - lrtc) >= per) { |
| 191 | + heartBeat(); |
| 192 | + lrtc = rtc; |
| 193 | + } |
| 194 | + //Check for report datapoint status interval |
| 195 | + LDateTime.getRtc(&rtc1); |
| 196 | + if ((rtc1 - lrtc1) >= per1) { |
| 197 | + lrtc1 = rtc1; |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments