Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include <Bridge.h>
  2. #include <HttpClient.h>
  3. #include <BridgeClient.h>
  4.  
  5. #define DEVICEID "Dfxi2wSa" // Input your deviceId
  6. #define DEVICEKEY "7Ts96bmxlHvh6rLE" // Input your deviceKey
  7. #define SITE_URL "api.mediatek.com"
  8.  
  9. static unsigned long beat = 0;
  10. static String commandServer;
  11. static int commandPort = 0;
  12.  
  13. // This will be used to connect to command server
  14. BridgeClient bc;
  15.  
  16. void setup()
  17. {
  18. Bridge.begin();
  19. Serial.begin(115200);
  20.  
  21. while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */
  22.  
  23. getCommandServer();
  24. beat = millis();
  25. }
  26.  
  27. void getCommandServer()
  28. {
  29. Serial.print("Query command server:");
  30.  
  31. // Prepare header for MCS API authentication
  32. String header = "deviceKey: ";
  33. header += DEVICEKEY;
  34. header += "\r\n";
  35. header += "Connection: close";
  36.  
  37. HttpClient c;
  38. c.setHeader(header);
  39. c.get(SITE_URL "/mcs/v2/devices/" DEVICEID "/connections.csv");
  40. c.setTimeout(1000);
  41.  
  42. const String resp = c.readString();
  43. const int sep = resp.indexOf(',');
  44. if (-1 == sep)
  45. {
  46. Serial.println("Fail to get command server. Make sure you already configured LinkIt Smart 7688 to internet and setup DEVICEID and DEVICEKEY in the sketch.");
  47. Serial.flush();
  48. return;
  49. }
  50. commandServer = resp.substring(0, sep);
  51. commandPort = resp.substring(sep+1).toInt();
  52. Serial.print(commandServer);
  53. Serial.print(":");
  54. Serial.println(commandPort);
  55. int ret = bc.connect(commandServer.c_str(), commandPort);
  56. Serial.print("connect result = ");
  57. Serial.println(ret);
  58. Serial.flush();
  59. }
  60.  
  61. void heartBeat(Client &c)
  62. {
  63. // Send a heart beat data;
  64. // format reference: https://mcs.mediatek.com/resources/latest/api_references/#get-connection
  65. static const char* heartbeat = DEVICEID "," DEVICEKEY ",0";
  66. c.println(heartbeat);
  67. c.println();
  68. Serial.println("HeartBeat sent.");
  69. Serial.flush();
  70. delay(100);
  71. }
  72.  
  73. void loop()
  74. {
  75. if(!bc.connected())
  76. {
  77. Serial.println("command server offline, try re-connect");
  78. int ret = bc.connect(commandServer.c_str(), commandPort);
  79. Serial.print("connect = ");
  80. Serial.println(ret);
  81. Serial.flush();
  82. }
  83.  
  84. // send heart beat for every 30 seconds;
  85. // or our connection will be closed by command server.
  86. if(30000 < (millis() - beat))
  87. {
  88. heartBeat(bc);
  89. beat = millis();
  90. }
  91.  
  92. // process commands if any
  93. // note that the command server will also echo our hearbeat.
  94. String tcpcmd;
  95. static const char* tcpcmd_led_on = "LED_Control,1";
  96. static const char* tcpcmd_led_off = "LED_Control,0";
  97. bool sendUpdate = false;
  98. while (bc.available())
  99. {
  100. int v = bc.read();
  101. if (v != -1)
  102. {
  103. Serial.print((char)v);
  104. tcpcmd += (char)v;
  105. if (tcpcmd.substring(40).equals(tcpcmd_led_on)){
  106. digitalWrite(13, HIGH);
  107. Serial.println("Switch LED ON ");
  108. tcpcmd="";
  109. sendUpdate = true;
  110. }else if(tcpcmd.substring(40).equals(tcpcmd_led_off)){
  111. digitalWrite(13, LOW);
  112. Serial.println("Switch LED OFF");
  113. tcpcmd="";
  114. sendUpdate = true;
  115. }
  116. }
  117. }
  118.  
  119. // POST LED value back to MCS.
  120. if(sendUpdate)
  121. {
  122. uploadstatus();
  123. }
  124.  
  125. delay(300);
  126. }
  127.  
  128. void uploadstatus()
  129. {
  130. // We update the LED_Display data value
  131. // to reflect the status of the board.
  132. String upload_led = (digitalRead(13)==1) ? "LED_Display,,1" : "LED_Display,,0";
  133.  
  134. // Prepare HTTP header for MCS API authentication.
  135. // note that we don't have to add content-length and content-type.
  136. // these will be automatically added by HttpClient (with curl).
  137. //
  138. // !We must put Content-Type in the first line to prevent
  139. // curl from sending "application/x-www-form-urlencoded"
  140. // as content-type. MCS blocks "application/x-www-form-urlencoded"
  141. // just like many web services.
  142. String header = "Content-Type: text-csv\r\n";
  143. header += "deviceKey: ";
  144. header += DEVICEKEY;
  145. header += "\r\n";
  146. header += "Connection: close";
  147.  
  148. // refer to https://mcs.mediatek.com/resources/latest/api_references/#upload-data-points
  149. HttpClient http;
  150. http.setHeader(header);
  151. http.post(SITE_URL "/mcs/v2/devices/" DEVICEID "/datapoints.csv", upload_led.c_str());
  152. http.setTimeout(1000);
  153.  
  154. delay(300);
  155.  
  156. String resp = http.readString();
  157. Serial.print("POST result:");
  158. Serial.println(resp);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement