easymode.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // 'use strict';
  2. // goog.provide('Blockly.Python.easymode');
  3. // goog.require('Blockly.Python');
  4. // Blockly.Python['wifiEasymode_network'] = function(block) {
  5. // var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
  6. // var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
  7. // Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
  8. // Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
  9. // var ssid = block.getFieldValue("wifi_ssid");
  10. // var pw = block.getFieldValue("wifi_pw");
  11. // var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti;\n";
  12. // Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
  13. // var code = " delay(10);\n" +
  14. // // Start by connecting to a WiFi network
  15. // " WiFiMulti.addAP(\"" + ssid + "\",\"" + pw + "\");\n" +
  16. // " Serial.print(\"Wait for WiFi... \");\n" +
  17. // " while(WiFiMulti.run() != WL_CONNECTED) {\n" +
  18. // " Serial.print(\".\");\n" +
  19. // " delay(500);\n" +
  20. // " }\n" +
  21. // " Serial.println(\"\");\n" +
  22. // " Serial.println(\"WiFi connected\");";
  23. // Blockly.Python.addSetup("wifinetwork_connect", code);
  24. // return '';
  25. // };
  26. // Blockly.Python['wifiEasymode_thingspeak'] = function(block) {
  27. // var key = block.getFieldValue("API_KEY");
  28. // var item_field = '',
  29. // item_value = '';
  30. // var thingspeak_url = "http://api.thingspeak.com/update?api_key=" + key;
  31. // for (var n = 0; n < this.itemCount_; n++) {
  32. // item_field = "field" + (n + 1);
  33. // item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  34. // Blockly.Python.ORDER_NONE) || '';
  35. // thingspeak_url += '&' + item_field + '=\"+String(' + item_value + ')+\"';
  36. // }
  37. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  38. // Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  39. // Blockly.Python.addDeclaration("httpclient", "HTTPClient http;");
  40. // var code = "http.begin(\"" + thingspeak_url + "\");\n" +
  41. // "http.GET();\n" +
  42. // "http.end();\n";
  43. // return code;
  44. // };
  45. // Blockly.Python['wifiEasymode_cococloud_post'] = function(block) {
  46. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  47. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  48. // var HTTPResponse = "String http_response;";
  49. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  50. // var HTTPCode = "int httpCode;";
  51. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  52. // let serial_setup = "Serial.begin(9600);";
  53. // Blockly.Python.addSetup('serial_serial', serial_setup);
  54. // var httpclient = "HTTPClient http;";
  55. // Blockly.Python.addDeclaration("httpclient", httpclient);
  56. // Blockly.Python.addDeclaration("payload", "String payload;");
  57. // var json_include = "#include <ArduinoJson.h>";
  58. // Blockly.Python.addInclude("json_declaration", json_include);
  59. // var precode = "char JSONmessageBuffer[240];\n";
  60. // Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  61. // var code = "";
  62. // var item_url = block.getFieldValue("event0");
  63. // var item_data = Blockly.Python.valueToCode(this, 'dataA',
  64. // Blockly.Python.ORDER_NONE) || '';
  65. // var code = "StaticJsonBuffer<240> JSONbuffer_easymode;\n" +
  66. // "JsonObject& Json_easymode = JSONbuffer_easymode.createObject();\n" +
  67. // "Json_easymode[\"EM_data\"]=" + item_data + ";\n" +
  68. // "Json_easymode.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n" +
  69. // "http.begin(\"" + item_url + "\");\n" +
  70. // "http.addHeader(\"Content-Type\", \"application/json\");\n" +
  71. // "httpCode = http.POST(JSONmessageBuffer);\n" +
  72. // "payload = http.getString();\n" +
  73. // "Serial.println(httpCode);\n" +
  74. // "Serial.println(payload);\n" +
  75. // "http.end();\n";
  76. // return code;
  77. // }
  78. // Blockly.Python['wifiEasymode_cococloud_get'] = function(block) {
  79. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  80. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  81. // var HTTPResponse = "String http_response;";
  82. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  83. // var HTTPCode = "int httpCode;";
  84. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  85. // let serial_setup = "Serial.begin(9600);";
  86. // Blockly.Python.addSetup('serial_serial', serial_setup);
  87. // var httpclient = "HTTPClient http;";
  88. // Blockly.Python.addDeclaration("httpclient", httpclient);
  89. // var cococloud_Response = "String cococloud_response;";
  90. // Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
  91. // Blockly.Python.addDeclaration("payload", "String payload;");
  92. // var json_include = "#include <ArduinoJson.h>";
  93. // Blockly.Python.addInclude("json_declaration", json_include);
  94. // var code = "";
  95. // var item_url = block.getFieldValue("event0");
  96. // var dataGet = Blockly.Python.variableDB_.getName(
  97. // block.getFieldValue('DATAVAR'), Blockly.Variables.NAME_TYPE);
  98. // code += "http.begin(\"" + item_url + "\");\n" +
  99. // "http.addHeader(\"Content-Type\", \"application/json\");\n" +
  100. // "httpCode = http.GET();\n" +
  101. // "payload = http.getString();\n" +
  102. // "cococloud_response = payload;\n" +
  103. // "Serial.println(httpCode);\n" +
  104. // "Serial.println(payload);\n" +
  105. // "http.end();\n" +
  106. // "StaticJsonBuffer<240> jsonBuffer_easymode;\n" +
  107. // "JsonObject& cloud_data = jsonBuffer_easymode.parseObject(cococloud_response);\n" +
  108. // "" + dataGet + " = cloud_data[\"data\"][0][\"EM_data\"].as<char*>();";
  109. // return code;
  110. // }
  111. // Blockly.Python['wifiEasymode_cococloud2_post'] = function(block) {
  112. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  113. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  114. // var HTTPResponse = "String http_response;";
  115. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  116. // var HTTPCode = "int httpCode;";
  117. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  118. // let serial_setup = "Serial.begin(9600);";
  119. // Blockly.Python.addSetup('serial_serial', serial_setup);
  120. // var httpclient = "HTTPClient http;";
  121. // Blockly.Python.addDeclaration("httpclient", httpclient);
  122. // Blockly.Python.addDeclaration("payload", "String payload;");
  123. // var json_include = "#include <ArduinoJson.h>";
  124. // Blockly.Python.addInclude("json_declaration", json_include);
  125. // var precode = "char JSONmessageBuffer[240];\n";
  126. // Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  127. // var code = "";
  128. // // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  129. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  130. // var code = "StaticJsonBuffer<240> JSONbuffer_easymode;\n" +
  131. // "JsonObject& Json_easymode = JSONbuffer_easymode.createObject();\n";
  132. // for (var n = 0; n < this.itemCount_; n++) {
  133. // var item_field = block.getFieldValue("field" + n);
  134. // var item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  135. // Blockly.Python.ORDER_NONE) || '0';
  136. // code += "Json_easymode[\"" + item_field + "\"]=" + item_value + ";\n";
  137. // }
  138. // code += "Json_easymode.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n" +
  139. // "http.begin(\"" + item_url + "\");\n" +
  140. // "http.addHeader(\"Content-Type\", \"application/json\");\n" +
  141. // "httpCode = http.POST(JSONmessageBuffer);\n" +
  142. // "payload = http.getString();\n" +
  143. // "Serial.println(httpCode);\n" +
  144. // "Serial.println(payload);\n" +
  145. // "http.end();\n";
  146. // return code;
  147. // }
  148. // Blockly.Python['wifiEasymode_cococloud2_get'] = function(block) {
  149. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  150. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  151. // var HTTPResponse = "String http_response;";
  152. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  153. // var HTTPCode = "int httpCode;";
  154. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  155. // let serial_setup = "Serial.begin(9600);";
  156. // Blockly.Python.addSetup('serial_serial', serial_setup);
  157. // var httpclient = "HTTPClient http;";
  158. // Blockly.Python.addDeclaration("httpclient", httpclient);
  159. // var cococloud_Response = "String cococloud_response;";
  160. // Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
  161. // Blockly.Python.addDeclaration("payload", "String payload;");
  162. // var json_include = "#include <ArduinoJson.h>";
  163. // Blockly.Python.addInclude("json_declaration", json_include);
  164. // var code = "";
  165. // // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  166. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  167. // var dataGet, item_value;
  168. // code += "http.begin(\"" + item_url + "\");\n" +
  169. // "http.addHeader(\"Content-Type\", \"application/json\");\n" +
  170. // "httpCode = http.GET();\n" +
  171. // "payload = http.getString();\n" +
  172. // "cococloud_response = payload;\n" +
  173. // "Serial.println(httpCode);\n" +
  174. // "Serial.println(payload);\n" +
  175. // "http.end();\n" +
  176. // "StaticJsonBuffer<240> jsonBuffer_easymode;\n" +
  177. // "JsonObject& cloud_data = jsonBuffer_easymode.parseObject(cococloud_response);\n";
  178. // for (var n = 0; n < this.itemCount_; n++) {
  179. // dataGet = Blockly.Python.variableDB_.getName(
  180. // block.getFieldValue('DATA' + n), Blockly.Variables.NAME_TYPE);
  181. // item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  182. // Blockly.Python.ORDER_NONE) || '""';
  183. // code += "" + dataGet + "=" + item_value + ";\n";
  184. // }
  185. // return code;
  186. // }
  187. // Blockly.Python['wifiEasymode_cococloud2_get_data'] = function(block) {
  188. // var code = "";
  189. // var key = block.getFieldValue("KEY");
  190. // // var varType = block.getFieldValue('VAR_TYPE');
  191. // // if (varType == "Text") {
  192. // // varType = "char*";
  193. // // } else if (varType == "Number") {
  194. // // varType = "int";
  195. // // } else if (varType == "Boolean") {
  196. // // varType = "boolean";
  197. // // } else if (varType == "Large Number") {
  198. // // varType = "long";
  199. // // } else if (varType == "Decimal") {
  200. // // varType = "float";
  201. // // }
  202. // code = "cloud_data[\"data\"][0][\"" + key + "\"].as<char*>()";
  203. // return [code, Blockly.Python.ORDER_ATOMIC];
  204. // }
  205. // Blockly.Python['wifiEasymode_IFTTT'] = function(block) {
  206. // var key = block.getFieldValue('IFTTT_KEY').replace(/\"/g, '') || "";
  207. // var event = block.getFieldValue('IFTTT_EVENT').replace(/\"/g, '') || "";
  208. // var value1 = Blockly.Python.valueToCode(block, 'dataA', Blockly.Python.ORDER_ATOMIC) || "";
  209. // // var value2 = Blockly.Python.valueToCode(block, 'VALUE2', Blockly.Python.ORDER_ATOMIC) || "";
  210. // // var value3 = Blockly.Python.valueToCode(block, 'VALUE3', Blockly.Python.ORDER_ATOMIC) || "";
  211. // var jsontool_declaration = "#include <ArduinoJson.h>";
  212. // Blockly.Python.addInclude("jsontool_declaration", jsontool_declaration);
  213. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  214. // Blockly.Python.addInclude("wifi_httpclient", wifi_httpclient);
  215. // var httpclient = "HTTPClient http;";
  216. // Blockly.Python.addDeclaration("httpclient", httpclient);
  217. // var HTTPCode = "int httpCode;";
  218. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  219. // Blockly.Python.addDeclaration("payload", "String payload;");
  220. // var precode = "char JSONmessageBuffer[240];\n";
  221. // Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  222. // var url = "http://maker.ifttt.com/trigger/" + event + "/with/key/" + key;
  223. // var code = "StaticJsonBuffer<240> JSONbuffer_ifttt;\n" +
  224. // "JsonObject& JSONencoder = JSONbuffer_ifttt.createObject();\n" +
  225. // "JSONencoder[\"value1\"] = String(" + value1 + ");\n" +
  226. // "JSONencoder[\"value2\"] = \"0\";\n" +
  227. // "JSONencoder[\"value3\"] = \"0\";\n" +
  228. // "JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n" +
  229. // "Serial.println(JSONmessageBuffer);\n" +
  230. // "if (WiFi.status() == WL_CONNECTED) {\n" +
  231. // " http.begin(\"" + url + "\");\n" +
  232. // " http.addHeader(\"Content-Type\", \"application/json\");\n" +
  233. // " httpCode = http.POST(JSONmessageBuffer);\n" +
  234. // " payload = http.getString();\n" +
  235. // " Serial.println(httpCode);\n" +
  236. // " Serial.println(payload);\n" +
  237. // " http.end();\n" +
  238. // "} else {\n" +
  239. // " Serial.println(\"Error in WiFi connection\");\n" +
  240. // "}\n";
  241. // return code;
  242. // }
  243. // Blockly.Python['wifiEasymode_transfer_send'] = function(block) {
  244. // var dataMain = Blockly.Python.variableDB_.getName(
  245. // block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
  246. // Blockly.Python.addSetup('serial', "Serial.begin(9600);");
  247. // var checkbit = "#define SOP '<'\n" +
  248. // "#define EOP '>'\n";
  249. // Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  250. // var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
  251. // var dataLength = length + "+2";
  252. // var code = "unsigned char DataOut_[" + dataLength + "];\n" +
  253. // "DataOut_[0]= SOP;\n" +
  254. // "DataOut_[" + length + "+1]= EOP;\n" +
  255. // "for (int i=0; i<" + length + ";i++) {\n" +
  256. // " DataOut_[i+1]=" + dataMain + "[i];\n" +
  257. // "}\n" +
  258. // "Serial.write(DataOut_, " + dataLength + ");\n";
  259. // return code;
  260. // }
  261. // Blockly.Python['wifiEasymode_transfer_receive'] = function(block) {
  262. // var dataMain = Blockly.Python.variableDB_.getName(
  263. // block.getFieldValue('DATAMAIN'), Blockly.Variables.NAME_TYPE);
  264. // Blockly.Python.addSetup('serial', "Serial.begin(9600);");
  265. // var checkbit = "#define SOP '<'\n" +
  266. // "#define EOP '>'\n";
  267. // Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  268. // var MsgReceiveStatus = "bool started = false;\n" +
  269. // "bool ended = false;\n" +
  270. // "int index_i;\n";
  271. // Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  272. // var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  273. // var dataIn_function = "int handleDataIn() {\n" +
  274. // " while (Serial.available() > 0) {\n" +
  275. // " int inChar = Serial.read();\n" +
  276. // " if(inChar == SOP) {\n" +
  277. // " index_i = 0;\n" +
  278. // " started = true;\n" +
  279. // " ended = false;\n" +
  280. // " } else if(inChar == EOP) {\n" +
  281. // " ended = true;\n" +
  282. // " break;\n" +
  283. // " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  284. // " " + dataMain + "[index_i] = inChar;\n" +
  285. // " index_i++;\n" +
  286. // " }\n" +
  287. // " }\n" +
  288. // " if(started && ended) {\n" +
  289. // " return 1;\n" +
  290. // " } else {\n" +
  291. // " return 0;\n" +
  292. // " }\n" +
  293. // "}\n";
  294. // Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  295. // var code = "boolean receiveMsgsuccess_ = handleDataIn();\n" +
  296. // "if (!receiveMsgsuccess) {\n" +
  297. // " Serial.println(\"Error: Fail to receive data!\")\n" +
  298. // " return;\n" +
  299. // "}\n";
  300. // return code;
  301. // }
  302. // Blockly.Python['wifiEasymode_transfer2_send'] = function(block) {
  303. // var dataMain = block.getFieldValue('DATAMAIN');
  304. // var dataMain_list = "int " + dataMain + "[" + this.itemCount_ + "] = {0";
  305. // for (var i = 1; i < this.itemCount_; i++) {
  306. // dataMain_list += ",0";
  307. // }
  308. // dataMain_list += "};\n";
  309. // Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  310. // Blockly.Python.addSetup('serial', "Serial.begin(9600);");
  311. // var checkbit = "#define SOP '<'\n" +
  312. // "#define EOP '>'\n";
  313. // Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  314. // var length = "sizeof(" + dataMain + ")/sizeof(" + dataMain + "[0])";
  315. // var dataLength = length + "+2";
  316. // var code = "";
  317. // var item_value = '';
  318. // for (var n = 0; n < this.itemCount_; n++) {
  319. // item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  320. // Blockly.Python.ORDER_NONE) || '0';
  321. // code += dataMain + "[" + n + "] = " + item_value + ";\n";
  322. // }
  323. // code += "unsigned char DataOut_[" + dataLength + "];\n" +
  324. // "DataOut_[0]= SOP;\n" +
  325. // "DataOut_[" + length + "+1]= EOP;\n" +
  326. // "for (int i=0; i<" + length + ";i++) {\n" +
  327. // " DataOut_[i+1]=" + dataMain + "[i];\n" +
  328. // "}\n" +
  329. // "Serial.write(DataOut_, " + dataLength + ");\n";
  330. // return code;
  331. // }
  332. // Blockly.Python['wifiEasymode_transfer2_receive'] = function(block) {
  333. // var dataMain = block.getFieldValue('DATAMAIN');
  334. // var list_length = block.getFieldValue("LENGTH");
  335. // var dataMain_list = "int " + dataMain + "[" + list_length + "] = {0";
  336. // for (var i = 1; i < list_length; i++) {
  337. // dataMain_list += ",0";
  338. // }
  339. // dataMain_list += "};\n";
  340. // Blockly.Python.addDeclaration("dataMain_list", dataMain_list);
  341. // Blockly.Python.addSetup('serial', "Serial.begin(9600);");
  342. // var checkbit = "#define SOP '<'\n" +
  343. // "#define EOP '>'\n";
  344. // Blockly.Python.addDeclaration("wifi_dataTransfer_checkbit", checkbit);
  345. // var MsgReceiveStatus = "bool started = false;\n" +
  346. // "bool ended = false;\n" +
  347. // "int index_i;\n";
  348. // Blockly.Python.addDeclaration("wifi_dataTransfer_MsgReceiveStatus", MsgReceiveStatus);
  349. // var length = 'sizeof(' + dataMain + ')/sizeof(' + dataMain + '[0])' || 1;
  350. // var dataIn_function = "int handleDataIn() {\n" +
  351. // " while (Serial.available() > 0) {\n" +
  352. // " int inChar = Serial.read();\n" +
  353. // " if(inChar == SOP) {\n" +
  354. // " index_i = 0;\n" +
  355. // " started = true;\n" +
  356. // " ended = false;\n" +
  357. // " } else if(inChar == EOP) {\n" +
  358. // " ended = true;\n" +
  359. // " break;\n" +
  360. // " }else if((index_i < " + length + ")&&started&&(!ended)) {\n" +
  361. // " " + dataMain + "[index_i] = inChar;\n" +
  362. // " index_i++;\n" +
  363. // " }\n" +
  364. // " }\n" +
  365. // " if(started && ended) {\n" +
  366. // " return 1;\n" +
  367. // " } else {\n" +
  368. // " return 0;\n" +
  369. // " }\n" +
  370. // "}\n";
  371. // Blockly.Python.addFunction("mainwifi_dataIn_function", dataIn_function);
  372. // var code = "boolean receiveMsgsuccess_ = handleDataIn();\n" +
  373. // "if (!receiveMsgsuccess_) {\n" +
  374. // " Serial.println(\"Error: Fail to receive data!\");\n" +
  375. // " return;\n" +
  376. // "}\n";
  377. // return code;
  378. // }
  379. // Blockly.Python['wifiEasymode_transfer2_receive_getValue'] = function(block) {
  380. // var dataMain = block.getFieldValue('DATAMAIN');
  381. // var list_length = block.getFieldValue("INDEX");
  382. // var code = "" + dataMain + "[" + list_length + "]";
  383. // return [code, Blockly.Python.ORDER_ATOMIC];
  384. // }