nbiot.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. 'use strict';
  2. goog.provide('Blockly.Python.nbiot');
  3. goog.require('Blockly.Python');
  4. /* NBIoT
  5. * Setup
  6. */
  7. Blockly.Python['NBIoT_setup'] = function(block) {
  8. Blockly.Python.addDeclaration("NBIoT_Dec", "#define nbiotSerial Serial1\n" +
  9. "int nbiotperiodicallyExecutionState = 0;\n" +
  10. "int nbiotperiodicallyExecutionGap = 8;\n" +
  11. "int serialSendState = 0;\n")
  12. Blockly.Python.addSetup("NBIoT_Setup",
  13. "nbiotSerial.begin(115200);\n" +
  14. "nbiotSerial.write(\"AT+CSQ\\r\\n\");\n"
  15. /* "delay(3000);\n"+
  16. "nbiotSerial.write(\"AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n\");\n"*/
  17. );
  18. return '';
  19. }
  20. /* NBIoT
  21. * Http request
  22. */
  23. Blockly.Python['NBIoT_http'] = function(block) {
  24. var apikey = block.getFieldValue("APIKEY");
  25. Blockly.Python.addFunction("nbiotHttprequestPOST", 'void nbiotHttprequestPOST() {\n' +
  26. ' int nbiotDataLength = nbiotHTTPJSONDataSet().length() + 1;\n' +
  27. ' char nbiotDataBuf[nbiotDataLength];\n' +
  28. ' nbiotHTTPJSONDataSet().toCharArray(nbiotDataBuf, nbiotDataLength);\n' +
  29. /*' if ( (millis() / 100) % (nbiotperiodicallyExecutionGap * 10) == ((nbiotperiodicallyExecutionGap * 10) - 1) && nbiotperiodicallyExecutionState == 0) {\n'+
  30. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://api.cocorobo.cn/\\"\\r\\n");\n'+
  31. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"Content-Type: application/json\\\\r\\\\n\\"\\r\\n");\n'+
  32. ' nbiotSerial.write(nbiotDataBuf);\n'+
  33. ' nbiotSerial.write("AT+HTTPSEND=0,1,\\"/iot/data/eventAPIKey/'+apikey+'\\"\\r\\n");\n'+
  34. ' } else if ((millis() / 100) % (nbiotperiodicallyExecutionGap * 10) == ((nbiotperiodicallyExecutionGap * 10) - 10)) {\n'+
  35. ' digitalWrite(13, LOW);\n'+
  36. // ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n'+
  37. ' }\n'+ */
  38. ' if ((millis() / 1000) % 10 == 1 && serialSendState == 0) {\n' +
  39. ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n' +
  40. ' serialSendState= 1;\n;' +
  41. ' } else if ((millis() / 1000) % 10 == 2) {\n' +
  42. ' serialSendState = 0;\n }' +
  43. ' if ((millis() / 1000) % 10 == 3 && serialSendState == 0) {\n' +
  44. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://api.cocorobo.cn/\\"\\r\\n");\n' +
  45. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"Content-Type: application/json\\\\r\\\\n\\"\\r\\n");\n' +
  46. ' serialSendState= 1;\n' +
  47. ' } else if ((millis() / 1000) % 10 == 4) {\n' +
  48. ' serialSendState = 0;\n }' +
  49. ' if ((millis() / 1000) % 10 == 5 && serialSendState == 0) {\n' +
  50. ' nbiotSerial.write(nbiotDataBuf);\n' +
  51. ' serialSendState= 1;\n' +
  52. ' } else if ((millis() / 1000) % 10 == 6) {\n' +
  53. ' serialSendState = 0;\n }' +
  54. ' if ((millis() / 1000) % 10 == 7 && serialSendState == 0) {\n' +
  55. ' nbiotSerial.write("AT+HTTPSEND=0,1,\\"/iot/data/eventAPIKey/' + apikey + '\\"\\r\\n");\n' +
  56. ' serialSendState= 1;\n' +
  57. ' } else if ((millis() / 1000) % 10 == 8) {\n' +
  58. ' serialSendState = 0;\n }' +
  59. ' }\n')
  60. var nbiotHTTPJSONDataSet = 'String nbiotHTTPJSONDataSet() {\n' +
  61. ' String data = "AT+HTTPCONTENT=0,\\"{";\n';
  62. for (var i = 0; i < block.itemCount_; i++) {
  63. var data_default = block.getFieldValue("DATA_DEFAULT" + i) || "default";
  64. var data_value = Blockly.Python.valueToCode(block, 'ADD' + i, Blockly.Python.ORDER_ATOMIC) || 0;
  65. if (i > 0) nbiotHTTPJSONDataSet += ' data += ",";\n';
  66. nbiotHTTPJSONDataSet += ' data += "\\"";\n' +
  67. ' data += "' + data_default + '";\n' +
  68. ' data += "\\":";\n' +
  69. // ' data += "\'";\n'+
  70. ' data += ' + data_value + ';\n'
  71. // ' data += "\'";\n'
  72. }
  73. nbiotHTTPJSONDataSet += ' data += "}\\"\\r\\n";\n' +
  74. ' return data;\n' +
  75. '}\n'
  76. Blockly.Python.addFunction("nbiotHTTPJSONDataSet", nbiotHTTPJSONDataSet);
  77. var code = "nbiotHttprequestPOST();\n";
  78. return code;
  79. }
  80. Blockly.Python['NBIoT_enable'] = function(block) {
  81. Blockly.Python.addDeclaration("nbiotEnable_var", 'const char EOPmarker = \'\\r\\n\'; //This is the end of packet marker\n' +
  82. 'char serialbuf[200]; //This gives the incoming serial some room. Change it if you want a longer incoming.\n' +
  83. 'char c = " ";\n' +
  84. '\n' +
  85. 'String nbiot_btstring;\n' +
  86. 'String nbiot_var;\n' +
  87. 'String nbiot_str;\n' +
  88. '\n' +
  89. 'int _stateADDR = 0;\n' +
  90. 'int _stateNbiotIsOn = 0;\n' +
  91. 'String _IPADDR = "";\n');
  92. Blockly.Python.addFunction("interactiveNBIoTSerial", 'void interactiveNBIoTSerial(){\n' +
  93. ' while (nbiotSerial.available() > 0) { //makes sure something is ready to be read\n' +
  94. '\n' +
  95. ' static int bufpos = 0; //starts the buffer back at the first position in the incoming serial.read\n' +
  96. ' char inchar = nbiotSerial.read(); //assigns one byte (as serial.read()\'s only input one byte at a time\n' +
  97. ' if (inchar != EOPmarker) { //if the incoming character is not the byte that is the incoming package ender\n' +
  98. ' serialbuf[bufpos] = inchar; //the buffer position in the array get assigned to the current read\n' +
  99. ' bufpos++; //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.\n' +
  100. ' }\n' +
  101. ' else { //once the end of package marker has been read\n' +
  102. ' serialbuf[bufpos] = 0; //restart the buff\n' +
  103. ' bufpos = 0; //restart the position of the buff\n' +
  104. '\n' +
  105. ' Serial.println(serialbuf);\n' +
  106. ' nbiot_str = serialbuf;\n' +
  107. '\n' +
  108. ' if (nbiot_str.charAt(1) == \'I\' && nbiot_str.charAt(2) == \'P\') {\n' +
  109. ' nbiot_var = midString(nbiot_str, "+IP:", "\\r");\n' +
  110. ' _IPADDR = nbiot_var;\n' +
  111. ' int i = 0;\n' +
  112. ' while (i < nbiot_var.length()) {\n' +
  113. ' if (nbiot_var.charAt(i) == \'.\') \n' +
  114. ' nbiot_var.setCharAt(i, \',\');\n' +
  115. ' i++;\n' +
  116. ' }\n' +
  117. ' Serial.println(nbiot_var);\n' +
  118. ' _stateADDR = 1;\n' +
  119. ' }\n' +
  120. 'if (nbiot_str.charAt(1) == \'C\' && nbiot_str.charAt(2) == \'P\' && nbiot_str.charAt(3) == \'I\' && nbiot_str.charAt(4) == \'N\' && nbiot_str.charAt(7) == \'R\' && nbiot_str.charAt(8) == \'E\' && nbiot_str.charAt(9) == \'A\' && nbiot_str.charAt(10) == \'D\' && nbiot_str.charAt(11) == \'Y\') {\n' +
  121. '_stateNbiotIsOn = 1;\n' +
  122. 'Serial.println("NBIoT initiated.");\n' +
  123. 'delay(100);\n' +
  124. 'nbiotSerial.write("AT+CMSYSCTRL=0,2,50,300,100,800\\r\\n");\n' +
  125. '}\n' +
  126. '\n' +
  127. ' }\n' +
  128. ' }\n' +
  129. ' while (Serial.available() > 0) {\n' +
  130. ' char cmd = Serial.read();\n' +
  131. ' nbiotSerial.write(cmd);\n' +
  132. ' }\n' +
  133. '}\n');
  134. Blockly.Python.addFunction("NBIoT_midString", "String midString(String str, String start, String finish) {\n" +
  135. " int locStart = str.indexOf(start);\n" +
  136. " if (locStart == -1) return \"\";\n" +
  137. " locStart += start.length();\n" +
  138. " int locFinish = str.indexOf(finish, locStart);\n" +
  139. " if (locFinish == -1) return \"\";\n" +
  140. " return str.substring(locStart, locFinish);\n" +
  141. "}\n");
  142. var code = "interactiveNBIoTSerial();\n";
  143. return code;
  144. }
  145. /* NBIoT
  146. * Http request
  147. */
  148. Blockly.Python['NBIoT_http_thingspeak'] = function(block) {
  149. var apikey = block.getFieldValue("APIKEY");
  150. var get_str = 'update?api_key=' + apikey + '&';
  151. for (let i = 0; i < block.itemCount_; i++) {
  152. var data_item = Blockly.Python.valueToCode(block, "ADD" + i, Blockly.Python.ORDER_ATOMIC);
  153. get_str += 'field' + (i + 1) + '="+String(' + data_item + ')+"'
  154. if (i != block.itemCount_ - 1) get_str += '&';
  155. // else get_str += '"';
  156. }
  157. Blockly.Python.addFunction("nbiotThingspeakRequest", 'void nbiotThingspeakRequest() {\n' +
  158. ' if ((millis() / 1000) % 10 == 1 && serialSendState == 0) {\n' +
  159. ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n' +
  160. ' serialSendState= 1;\n;' +
  161. ' } else if ((millis() / 1000) % 10 == 2) {\n' +
  162. ' serialSendState = 0;\n' +
  163. ' }\n' +
  164. ' if ((millis() / 1000) % 10 == 3 && serialSendState == 0) {\n' +
  165. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://api.thingspeak.com/\\"\\r\\n");\n' +
  166. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"User-Agent: Unknown\\\\r\\\\n\\"\\r\\n");\n' +
  167. ' serialSendState= 1;\n' +
  168. ' } else if ((millis() / 1000) % 10 == 4) {\n' +
  169. ' serialSendState = 0;\n' +
  170. ' }\n' +
  171. ' if ((millis() / 1000) % 10 == 5 && serialSendState == 0 ){\n' +
  172. ' String myString = "AT+HTTPSEND=0,0,\\"' + get_str + '\\"\\r\\n";\n' +
  173. ' char* buf = (char*) malloc(sizeof(char)*myString.length()+1);\n' +
  174. ' myString.toCharArray(buf, myString.length()+1);\n' +
  175. ' nbiotSerial.write(buf);\n' +
  176. ' serialSendState= 1;\n' +
  177. ' } else if ((millis() / 1000) % 10 == 6) {\n' +
  178. ' serialSendState = 0;\n' +
  179. ' }\n' +
  180. '}\n')
  181. var code = "nbiotThingspeakRequest();\n";
  182. return code;
  183. }
  184. /* NBIoT
  185. * Http request
  186. */
  187. Blockly.Python['NBIoT_http_IFTTT'] = function(block) {
  188. var apikey = block.getFieldValue("APIKEY");
  189. var event = block.getFieldValue("EVENT");
  190. var get_str = 'update?api_key=' + apikey + '&';
  191. for (let i = 0; i < block.itemCount_; i++) {
  192. var data_item = Blockly.Python.valueToCode(block, "ADD" + i, Blockly.Python.ORDER_ATOMIC);
  193. get_str += 'field' + (i + 1) + '="+String(' + data_item + ')+"'
  194. if (i != block.itemCount_ - 1) get_str += '&';
  195. // else get_str += '"';
  196. }
  197. Blockly.Python.addFunction("nbiotIFTTTRequest", 'void nbiotIFTTTRequest() {\n' +
  198. ' int nbiotDataLength = nbiotHTTPJSONDataSet().length() + 1;\n' +
  199. ' char nbiotDataBuf[nbiotDataLength];\n' +
  200. ' nbiotHTTPJSONDataSet().toCharArray(nbiotDataBuf, nbiotDataLength);\n' +
  201. /*' if ( (millis() / 100) % (nbiotperiodicallyExecutionGap * 10) == ((nbiotperiodicallyExecutionGap * 10) - 1) && nbiotperiodicallyExecutionState == 0) {\n'+
  202. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://api.cocorobo.cn/\\"\\r\\n");\n'+
  203. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"Content-Type: application/json\\\\r\\\\n\\"\\r\\n");\n'+
  204. ' nbiotSerial.write(nbiotDataBuf);\n'+
  205. ' nbiotSerial.write("AT+HTTPSEND=0,1,\\"/iot/data/eventAPIKey/'+apikey+'\\"\\r\\n");\n'+
  206. ' } else if ((millis() / 100) % (nbiotperiodicallyExecutionGap * 10) == ((nbiotperiodicallyExecutionGap * 10) - 10)) {\n'+
  207. ' digitalWrite(13, LOW);\n'+
  208. // ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n'+
  209. ' }\n'+ */
  210. 'if ((millis() / 1000) % 10 == 1 && serialSendState == 0) {\n' +
  211. ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n' +
  212. ' serialSendState= 1;\n;' +
  213. '} else if ((millis() / 1000) % 10 == 2) {\n' +
  214. ' serialSendState = 0;\n }' +
  215. 'if ((millis() / 1000) % 10 == 3 && serialSendState == 0) {\n' +
  216. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://maker.ifttt.com/\\"\\r\\n");\n' +
  217. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"Content-Type: application/json\\\\r\\\\n\\"\\r\\n");\n' +
  218. ' serialSendState= 1;\n' +
  219. '} else if ((millis() / 1000) % 10 == 4) {\n' +
  220. ' serialSendState = 0;\n }' +
  221. 'if ((millis() / 1000) % 10 == 5 && serialSendState == 0) {\n' +
  222. ' nbiotSerial.write(nbiotDataBuf);\n' +
  223. ' serialSendState= 1;\n' +
  224. '} else if ((millis() / 1000) % 10 == 6) {\n' +
  225. ' serialSendState = 0;\n }' +
  226. 'if ((millis() / 1000) % 10 == 7 && serialSendState == 0) {\n' +
  227. ' nbiotSerial.write("AT+HTTPSEND=0,1,\\"/trigger/' + event + '/with/key/' + apikey + '\\"\\r\\n");\n' +
  228. ' serialSendState= 1;\n' +
  229. '} else if ((millis() / 1000) % 10 == 8) {\n' +
  230. ' serialSendState = 0;\n }' +
  231. '}\n')
  232. var nbiotHTTPJSONDataSet = 'String nbiotHTTPJSONDataSet() {\n' +
  233. ' String data = "AT+HTTPCONTENT=0,\\"{";\n';
  234. for (var i = 0; i < block.itemCount_ && i < 3; i++) {
  235. var data_default = "value" + (i + 1)
  236. var data_value = Blockly.Python.valueToCode(block, 'ADD' + i, Blockly.Python.ORDER_ATOMIC) || 0;
  237. if (i > 0) nbiotHTTPJSONDataSet += ' data += ",";\n';
  238. nbiotHTTPJSONDataSet += ' data += "\\"";\n' +
  239. ' data += "' + data_default + '";\n' +
  240. ' data += "\\":";\n' +
  241. // ' data += "\'";\n'+
  242. ' data += ' + data_value + ';\n'
  243. // ' data += "\'";\n'
  244. }
  245. nbiotHTTPJSONDataSet += ' data += "}\\"\\r\\n";\n' +
  246. ' return data;\n' +
  247. '}\n'
  248. Blockly.Python.addFunction("nbiotHTTPJSONDataSet", nbiotHTTPJSONDataSet);
  249. var code = "nbiotIFTTTRequest();\n";
  250. return code;
  251. }
  252. Blockly.Python['NBIoT_network_connected'] = function(block) {
  253. var code = "_stateADDR";
  254. return [code, Blockly.Python.ORDER_ATOMIC]
  255. }
  256. Blockly.Python['NBIoT_powered_on'] = function(block) {
  257. var code = "_stateNbiotIsOn";
  258. return [code, Blockly.Python.ORDER_ATOMIC]
  259. }
  260. Blockly.Python['NBIoT_network_IP'] = function(block) {
  261. var code = "_IPADDR";
  262. return [code, Blockly.Python.ORDER_ATOMIC]
  263. }
  264. Blockly.Python['NBIoT_http_cococloud_get'] = function(block) {
  265. var apikey = block.getFieldValue("APIKEY") || '';
  266. Blockly.Python.addFunction("nbiotHttprequestGet", 'void nbiotHttprequestGet() {\n' +
  267. ' char nbiotDataBuf[nbiotDataLength];\n' +
  268. ' if ((millis() / 1000) % 10 == 1 && serialSendState == 0) {\n' +
  269. ' nbiotSerial.write("AT+HTTPCLOSE=0\\r\\n");\n' +
  270. ' serialSendState= 1;\n;' +
  271. ' } else if ((millis() / 1000) % 10 == 2) {\n' +
  272. ' serialSendState = 0;\n }' +
  273. ' if ((millis() / 1000) % 10 == 3 && serialSendState == 0) {\n' +
  274. ' nbiotSerial.write("AT+HTTPCREATE=\\"http://api.cocorobo.cn/\\"\\r\\n");\n' +
  275. ' nbiotSerial.write("AT+HTTPHEADER=0,\\"User-Agent: Unknown\\r\\n\\"\\r\\n");\n' +
  276. ' serialSendState= 1;\n' +
  277. ' } else if ((millis() / 1000) % 10 == 4) {\n' +
  278. ' serialSendState = 0;\n }' +
  279. ' if ((millis() / 1000) % 10 == 5 && serialSendState == 0) {\n' +
  280. ' nbiotSerial.write("AT+HTTPSEND=0,0,\\"/iot/data/eventAPIKey/' + apikey + '\\"\\r\\n");\n' +
  281. ' serialSendState= 1;\n' +
  282. ' } else if ((millis() / 1000) % 10 == 6) {\n' +
  283. ' serialSendState = 0;\n }' +
  284. ' }\n')
  285. var code = "";
  286. return code;
  287. }