Guest User

My code

a guest
Aug 4th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include <Bridge.h>
  2. #include <Process.h>
  3. #include <Wire.h>
  4. #include <WiFi.h>
  5. #include <SPI.h>
  6. #include <Console.h>
  7. char ssid[] = "xxxxxxxxxxxxxxx"; // the name of your network
  8. int status = WL_IDLE_STATUS;
  9. IPAddress ip;
  10. String setupHtmlString;
  11. String createHtmlString;
  12. String curlCreate(String a);
  13. String curlSetup(String b);
  14. void setup() {
  15. // put your setup code here, to run once:
  16. Bridge.begin();
  17. Console.begin();
  18. // Console.begin(9600);
  19. while (!Console) {
  20. }
  21. /*
  22. * Defintion Block
  23. */
  24.  
  25.  
  26. char *attribute[][5] = {
  27. {"0x0201", "0x0000", "0x019", "0x0", "15"},
  28. {"0x0201", "0x0000", "0x018", "0x0", "15"},
  29. {"0x0201", "0x0000", "0x017", "0x0", "15"},
  30. {"0x0000", "0x0000", "0x001", "0x0", "15"}
  31. };
  32.  
  33. int numberOfAttributes = (sizeof attribute / 10);
  34.  
  35. /*
  36. * Create Block
  37. */
  38.  
  39. String createHtmlString = String("http://");
  40. String route = String("/OpenEMan/register/create.json?");
  41. String apikey = String("apikey=");
  42. String nodeMAC = String("&nodeMAC=");
  43. String fromAddress = String("&fromAddress=");
  44. String timeout = String("&timeout=");
  45.  
  46. //Data entry from User
  47. String apikeyEntered = String("11133d815882bc03a4a97789d983d189");
  48. String serverAddress = String("xxx.xxx.xxx.xxx");
  49. String timeoutEntered = String("15");
  50.  
  51. String MACAddressFAKE = String("01:02:03:04:05:10");
  52. String IPaddressFAKE = String("xxx.xxx.xxx.xxx");
  53. createHtmlString.concat(serverAddress);
  54. createHtmlString.concat(route);
  55. createHtmlString.concat(apikey);
  56. createHtmlString.concat(apikeyEntered);
  57. createHtmlString.concat(nodeMAC);
  58. createHtmlString.concat(MACAddressFAKE);
  59. createHtmlString.concat(fromAddress);
  60. createHtmlString.concat(IPaddressFAKE);
  61. createHtmlString.concat(timeout);
  62. createHtmlString.concat(timeoutEntered);
  63. Console.print("Create String:");
  64. Console.println(createHtmlString);
  65.  
  66. //String nodeid = String("100");
  67.  
  68. String nodeid;
  69. nodeid=curlCreate(createHtmlString);
  70. Console.print("Nodeid: ");
  71. Console.println(nodeid);
  72.  
  73. /*
  74. * Setup Block
  75. */
  76.  
  77. for ( int go = 0; go < numberOfAttributes; go++) {
  78. Console.println(go);
  79. String groupIDA = attribute[go][0];
  80. groupIDA += ",";
  81. String attributeIDA = attribute[go][1];
  82. attributeIDA += ",";
  83. String attributeNumberA = attribute[go][2];
  84. attributeNumberA += ",";
  85. String attributeDefaultValueA = attribute[go][3];
  86. String timeoutA = attribute[go][4];
  87.  
  88. String setupHtmlString = String("http://xxx.xxx.xxx.xxx");
  89. String route = String("/OpenEMan/register/setup.json?");
  90. // setupHtmlString.concat(IPAddress);
  91. setupHtmlString.concat(route);
  92. String apikeyS = String("apikey=");
  93. apikeyS.concat(apikeyEntered);
  94. Console.println(apikeyS);
  95. String nodeidS = String("&node=");
  96. nodeidS.concat(nodeid);
  97. Console.println(nodeidS);
  98. String json = String("&json=");
  99. json.concat(groupIDA);
  100. json.concat(attributeIDA);
  101. json.concat(attributeNumberA);
  102. json.concat(attributeDefaultValueA);
  103. Console.println(json);
  104. String timeoutS = String("&timeout=");
  105. timeoutS.concat(timeoutA);
  106. // timeoutS.concat(timeoutSV);
  107.  
  108. setupHtmlString.concat(apikeyS);
  109. Console.println(setupHtmlString);
  110. Console.println("Breaks");
  111. setupHtmlString.concat(nodeidS);
  112. Console.println("Here");
  113. Console.println(setupHtmlString);
  114. setupHtmlString.concat(json);
  115. Console.println(setupHtmlString);
  116. setupHtmlString.concat(timeoutS);
  117. Console.println(setupHtmlString);
  118. String responseString;
  119. responseString=curlSetup(setupHtmlString);
  120. Console.println(responseString);
  121.  
  122. }
  123. }
  124. String curlCreate(String a){
  125. Process p; // Create a process and call it "p"
  126. p.begin("curl"); // Process that launch the "curl" command
  127. p.addParameter(a); // Add the URL parameter to "curl"
  128. p.run(); // Run the process and wait for its termination
  129. int i = 0;
  130. char createResponse[100];
  131.  
  132. while (p.available() > 0) {
  133. char c = p.read();
  134. createResponse[i] = c;
  135. i++;
  136. }
  137. Console.flush();
  138. char isReg;
  139. isReg = createResponse[i];
  140.  
  141. int sizeOfResponse = i - 3;
  142.  
  143. String nodeid;
  144. Console.println(createResponse);
  145. for (int aa = 0; aa < sizeOfResponse; aa++) {
  146. nodeid += createResponse[aa + 1];
  147. }
  148. Console.print("1Nodeid: ");
  149. Console.println(nodeid);
  150. return nodeid;
  151. }
  152.  
  153.  
  154. String curlSetup(String b){
  155. Console.println("CurlSetup");
  156. Console.println(b);
  157. Process o; // Create a process and call it "p"
  158. o.begin("curl"); // Process that launch the "curl" command
  159. o.addParameter(b); // Add the URL parameter to "curl"
  160. o.run(); // Run the process and wait for its termination
  161. Console.flush();
  162. int j = 0;
  163. char setupResponse[100];
  164. Console.println("CurlSetup");
  165. while (o.available() > 0) {
  166. char d = o.read();
  167. setupResponse[j] = d;
  168. j++;
  169. Console.println(setupResponse);
  170.  
  171. }
  172. Console.flush();
  173. Console.println("CurlSetup");
  174. return setupResponse;
  175. }
  176.  
  177. void loop() {
  178. // put your main code here, to run repeatedly:
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment