wifiwebservices.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. goog.provide('Blockly.Python.wifiwebservices');
  3. goog.require('Blockly.Python');
  4. var eventIdentifier = '';
  5. Blockly.Python['wifiwebservices_ifttt_simple'] = function(block) {
  6. var key = Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC).replace(/\"/g, '') || "";
  7. var event = Blockly.Python.valueToCode(block, 'EVENT', Blockly.Python.ORDER_ATOMIC).replace(/\"/g, '') || "";
  8. var value1 = Blockly.Python.valueToCode(block, 'VALUE1', Blockly.Python.ORDER_ATOMIC) || "";
  9. var value2 = Blockly.Python.valueToCode(block, 'VALUE2', Blockly.Python.ORDER_ATOMIC) || "";
  10. var value3 = Blockly.Python.valueToCode(block, 'VALUE3', Blockly.Python.ORDER_ATOMIC) || "";
  11. var jsontool_declaration = "#include <ArduinoJson.h>";
  12. Blockly.Python.addInclude("jsontool_declaration", jsontool_declaration);
  13. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  14. Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  15. var httpclient = "HTTPClient http;";
  16. Blockly.Python.addDeclaration("httpclient", httpclient);
  17. var precode = "char JSONmessageBuffer[240]\n";
  18. Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  19. var url = "http://maker.ifttt.com/trigger/" + event + "/with/key/" + key;
  20. var code = "StaticJsonBuffer<800> JSONbuffer\n" +
  21. "JsonObject& JSONencoder = JSONbuffer.createObject()\n" +
  22. "JSONencoder[\"value1\"] = String(" + value1 + ")\n" +
  23. "JSONencoder[\"value2\"] = String(" + value2 + ")\n" +
  24. "JSONencoder[\"value3\"] = String(" + value3 + ")\n" +
  25. "JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
  26. "Serial.println(JSONmessageBuffer)\n" +
  27. "if (WiFi.status() == WL_CONNECTED) {\n" +
  28. " http.begin(\"" + url + "\")\n" +
  29. " http.addHeader(\"Content-Type\", \"application/json\")\n" +
  30. " int httpCode = http.POST(JSONmessageBuffer)\n" +
  31. " String payload = http.getString()\n" +
  32. " // Serial.println(httpCode)\n" +
  33. " // Serial.println(payload)\n" +
  34. " http.end()\n" +
  35. // " delay(500)\n" +
  36. "} else {\n" +
  37. " Serial.println(\"Error in WiFi connection\")\n" +
  38. "}\n";
  39. return code;
  40. };
  41. Blockly.Python['wifiwebservices_thinkspeak_simple'] = function(block) {
  42. var key = Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC).replace(/\"/g, '') || "";
  43. var size = block.itemCount_;
  44. var item_field = '',
  45. item_value = '';
  46. var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + key;
  47. for (var n = 0; n < this.itemCount_; n++) {
  48. item_field = block.getFieldValue('FIELD' + n);
  49. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  50. Blockly.Python.ORDER_NONE) || '';
  51. thingspeak_url += '&' + item_field + '=\"+String(' + item_value + ')+\"';
  52. }
  53. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  54. Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  55. Blockly.Python.addDeclaration("httpclient", "HTTPClient http;");
  56. var code = "http.begin(\"" + thingspeak_url + "\")\n" +
  57. "http.GET()\n" +
  58. "http.end()\n" +
  59. "delay(500)\n";
  60. return code;
  61. };
  62. /*************************
  63. * version 2 *
  64. * eaysmode *
  65. *************************/
  66. Blockly.Python['wifiServices_thingspeak'] = function(block) {
  67. var key = block.getFieldValue("API_KEY");
  68. var item_field = '',
  69. item_value = '';
  70. var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + key;
  71. for (var n = 0; n < this.itemCount_; n++) {
  72. item_field = "field" + (n + 1);
  73. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  74. Blockly.Python.ORDER_NONE) || '';
  75. thingspeak_url += '&' + item_field + '=\"+String(' + item_value + ')+\"';
  76. }
  77. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  78. Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  79. Blockly.Python.addDeclaration("httpclient", "HTTPClient http;");
  80. var code = "http.begin(\"" + thingspeak_url + "\")\n" +
  81. "http.GET()\n" +
  82. "http.end()\n" +
  83. "delay(500)\n";
  84. return code;
  85. };
  86. Blockly.Python['wifiServices_IFTTT'] = function(block) {
  87. var jsontool_declaration = "#include <ArduinoJson.h>";
  88. Blockly.Python.addInclude("jsontool_declaration", jsontool_declaration);
  89. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  90. Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  91. var httpclient = "HTTPClient http;";
  92. Blockly.Python.addDeclaration("httpclient", httpclient);
  93. var HTTPCode = "int httpCode;";
  94. Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  95. Blockly.Python.addDeclaration("payload", "String payload;");
  96. var precode = "char JSONmessageBuffer[240]\n";
  97. Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  98. var key = block.getFieldValue('IFTTT_KEY').replace(/\"/g, '') || "";
  99. var event = block.getFieldValue('IFTTT_EVENT').replace(/\"/g, '') || "";
  100. var url = "http://maker.ifttt.com/trigger/" + event + "/with/key/" + key;
  101. var type = block.getType();
  102. block.onChange();
  103. if (type == 2) {
  104. var code = "if (WiFi.status() == WL_CONNECTED) {\n" +
  105. " if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  106. " http.begin(\"" + url + "\")\n" +
  107. " httpCode = http.GET()\n" +
  108. " payload = http.getString()\n" +
  109. " // Serial.println(httpCode)\n" +
  110. " // Serial.println(payload)\n" +
  111. " http.end()\n" +
  112. " }\n" +
  113. // " delay(500)\n" +
  114. "} else {\n" +
  115. " Serial.println(\"Error in WiFi connection\")\n" +
  116. "}\n";
  117. } else if (type == 1) {
  118. var value1 = Blockly.Python.valueToCode(block, 'data0', Blockly.Python.ORDER_ATOMIC) || "";
  119. var value2 = Blockly.Python.valueToCode(block, 'data1', Blockly.Python.ORDER_ATOMIC) || "";
  120. var value3 = Blockly.Python.valueToCode(block, 'data2', Blockly.Python.ORDER_ATOMIC) || "";
  121. var code = "" +
  122. // "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  123. " StaticJsonBuffer<800> JSONbuffer_ifttt\n" +
  124. " JsonObject& JSONencoder = JSONbuffer_ifttt.createObject()\n" +
  125. " JSONencoder[\"value1\"] = String(" + value1 + ")\n" +
  126. " JSONencoder[\"value2\"] = String(" + value2 + ")\n" +
  127. " JSONencoder[\"value3\"] = String(" + value3 + ")\n" +
  128. " JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
  129. " Serial.println(JSONmessageBuffer)\n" +
  130. " if (WiFi.status() == WL_CONNECTED) {\n" +
  131. " http.begin(\"" + url + "\")\n" +
  132. " http.addHeader(\"Content-Type\", \"application/json\")\n" +
  133. " httpCode = http.POST(JSONmessageBuffer)\n" +
  134. " payload = http.getString()\n" +
  135. " Serial.println(httpCode)\n" +
  136. " Serial.println(payload)\n" +
  137. " http.end()\n" +
  138. " delay(500)\n" +
  139. " } else {\n" +
  140. " Serial.println(\"Error in WiFi connection\")\n" +
  141. " }\n"
  142. // "}\n";
  143. }
  144. return code;
  145. }
  146. Blockly.Python['wifiServices_cococloud_post'] = function(block) {
  147. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  148. Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  149. var HTTPResponse = "String http_response;";
  150. Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  151. var HTTPCode = "int httpCode;";
  152. Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  153. let serial_setup = "Serial.begin(9600);";
  154. Blockly.Python.addSetup('serial_serial', serial_setup);
  155. var httpclient = "HTTPClient http;";
  156. Blockly.Python.addDeclaration("httpclient", httpclient);
  157. Blockly.Python.addDeclaration("payload", "String payload;");
  158. var json_include = "#include <ArduinoJson.h>";
  159. Blockly.Python.addInclude("json_declaration", json_include);
  160. var precode = "char JSONmessageBuffer[240]\n";
  161. Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  162. var code = "";
  163. var item_url = "http://api.cocorobo.cn/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  164. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  165. var code = "StaticJsonBuffer<800> JSONbuffer_easymode\n" +
  166. "JsonObject& Json_easymode = JSONbuffer_easymode.createObject()\n";
  167. for (var n = 0; n < this.itemCount_; n++) {
  168. var item_field = block.getFieldValue("field" + n);
  169. var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  170. Blockly.Python.ORDER_NONE) || '0';
  171. code += "Json_easymode[\"" + item_field + "\"]=" + item_value + "\n";
  172. }
  173. code += "" +
  174. "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  175. " Json_easymode.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer))\n" +
  176. " http.begin(\"" + item_url + "\")\n" +
  177. " http.addHeader(\"Content-Type\", \"application/json\")\n" +
  178. " httpCode = http.POST(JSONmessageBuffer)\n" +
  179. " payload = http.getString()\n" +
  180. " Serial.println(httpCode)\n" +
  181. " Serial.println(payload)\n" +
  182. " http.end()\n" +
  183. "}\n"
  184. // "delay(500)\n";
  185. return code;
  186. }
  187. Blockly.Python['wifiServices_cococloud_get'] = function(block) {
  188. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  189. Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  190. var HTTPResponse = "String http_response;";
  191. Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  192. var HTTPCode = "int httpCode;";
  193. Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  194. let serial_setup = "Serial.begin(9600);";
  195. Blockly.Python.addSetup('serial_serial', serial_setup);
  196. var httpclient = "HTTPClient http;";
  197. Blockly.Python.addDeclaration("httpclient", httpclient);
  198. var cococloud_Response = "String cococloud_response;";
  199. Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
  200. Blockly.Python.addDeclaration("payload", "String payload;");
  201. var json_include = "#include <ArduinoJson.h>";
  202. Blockly.Python.addInclude("json_declaration", json_include);
  203. var code = "";
  204. var item_url = "http://api.cocorobo.cn/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  205. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  206. var dataGet, item_value;
  207. eventIdentifier = block.getFieldValue("event0").slice(0, 5);
  208. code += "" +
  209. "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  210. " http.begin(\"" + item_url + "\")\n" +
  211. " http.addHeader(\"Content-Type\", \"application/json\")\n" +
  212. " httpCode = http.GET()\n" +
  213. " payload = http.getString()\n" +
  214. " cococloud_response = payload\n" +
  215. " // Serial.println(httpCode)\n" +
  216. " // Serial.println(payload)\n" +
  217. " http.end()\n" +
  218. "}\n" +
  219. // "delay(500)\n" +
  220. "StaticJsonBuffer<800> jsonBuffer_easymode_" + block.getFieldValue("event0").slice(0, 5) + "\n" +
  221. "JsonObject& cloud_data_" + block.getFieldValue("event0").slice(0, 5) + " = jsonBuffer_easymode_" + block.getFieldValue("event0").slice(0, 5) + ".parseObject(cococloud_response)\n";
  222. for (var n = 0; n < this.itemCount_; n++) {
  223. dataGet = Blockly.Python.variableDB_.getName(
  224. block.getFieldValue('DATA' + n), Blockly.Variables.NAME_TYPE);
  225. item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  226. Blockly.Python.ORDER_NONE) || '""';
  227. code += "" + dataGet + "=" + item_value + "\n";
  228. }
  229. return code;
  230. }
  231. Blockly.Python['wifiServices_cococloud_get_data'] = function(block) {
  232. var code = "";
  233. var key = block.getFieldValue("KEY");
  234. var varType = block.getFieldValue('VAR_TYPE');
  235. if (varType == "Text") {
  236. varType = "char*";
  237. } else if (varType == "Number") {
  238. varType = "int";
  239. // } else if (varType == "Boolean") {
  240. // varType = "boolean";
  241. // } else if (varType == "Large Number") {
  242. // varType = "long";
  243. // } else if (varType == "Decimal") {
  244. // varType = "float";
  245. }
  246. code = "cloud_data_" + eventIdentifier + "[\"data\"][0][\"" + key + "\"].as<" + varType + ">()";
  247. return [code, Blockly.Python.ORDER_ATOMIC];
  248. }