wificlient.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. goog.provide('Blockly.Python.wificlient');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['wifiClient_http_setup'] = function(block) {
  5. var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  6. Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  7. var HTTPResponse = "String http_response;";
  8. Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  9. var HTTPCode = "int httpCode;";
  10. Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  11. let serial_setup = "Serial.begin(9600);";
  12. Blockly.Python.addSetup('serial_9600', serial_setup);
  13. var code = "HTTPClient http;\n";
  14. Blockly.Python.addDeclaration("wifi_httpclient_setup", code);
  15. code = ''
  16. return code;
  17. };
  18. Blockly.Python['wifiClient_http_get'] = function(block) {
  19. var json_include = "#include <ArduinoJson.h>";
  20. Blockly.Python.addInclude("json_declaration", json_include);
  21. // var parse_data_type = "int ParseData_type = ";
  22. var varName = block.getFieldValue("VAR");
  23. var url = "http://192.168.4.1" + (block.getFieldValue('URL') || "");
  24. var code = "" +
  25. "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  26. " http.begin(\"" + url + "\");\n" +
  27. " httpCode = http.GET();\n" +
  28. " // Serial.print(\"HTTP code: \");\n" +
  29. " // Serial.println(httpCode);\n" +
  30. " http_response = http.getString();\n" +
  31. " http.end();\n" +
  32. "}\n"
  33. // "delay(500);\n"
  34. code += "StaticJsonBuffer<800> jsonBuffer_" + varName + ";\n" +
  35. "JsonObject& parse_" + varName + " = jsonBuffer_" + varName + ".parseObject(http_response);\n";
  36. return code;
  37. };
  38. Blockly.Python['wifiClient_http_get_getValue'] = function(block) {
  39. var varName = block.getFieldValue("VAR") || '';
  40. var index = block.getFieldValue("INDEX") || '';
  41. var type = block.getFieldValue("TYPE") || '';
  42. var code = "parse_" + varName + "[\"data\"][" + index + "]"
  43. if (type == "Int") code += ".as<int>()"
  44. else code += ".as<String>()"
  45. return [code, Blockly.Python.ORDER_ATOMIC];
  46. }
  47. Blockly.Python['wifiClient_http_post'] = function(block) {
  48. var json_include = "#include <ArduinoJson.h>";
  49. Blockly.Python.addInclude("json_include", json_include);
  50. var precode = "char JSONmessageBuffer[240];\n";
  51. Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  52. var VarName = block.getFieldValue('VAR') || "json";
  53. var code = "StaticJsonBuffer<800> JSONbuffer_" + VarName + ";\n" +
  54. "JsonObject& " + VarName + "_json_ = JSONbuffer_" + VarName + ".createObject();\n" +
  55. "JsonArray& data_json_C = " + VarName + "_json_.createNestedArray(\"data\");\n" +
  56. // "JsonArray& data_type_C = " + VarName + "_json_.createNestedArray(\"type\");\n"+
  57. "";
  58. var content = "";
  59. var data_;
  60. var url = "http://192.168.4.1" + (block.getFieldValue('URL') || "");
  61. for (let i = 0; i < block.itemCount_; i++) {
  62. data_ = Blockly.Python.valueToCode(block, "ADD" + i);
  63. code += "data_json_C.add(" + data_ + ");\n";
  64. // if (block.childBlocks_[i].type == "text") {
  65. // code += "data_type_C.add(0);\n";
  66. // } else if (block.childBlocks_[i].type == "math_number") {
  67. // code += "data_type_C.add(1);\n";
  68. // }
  69. }
  70. code += "String jsonString_" + VarName + ";\n" + VarName + "_json_.printTo(jsonString_" + VarName + ");\n";
  71. code += "" +
  72. "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
  73. " http.begin(\"" + url + "\");\n";
  74. code += " http.addHeader(\"Content-Type\",\"application/json\");\n";
  75. code += " httpCode = http.POST(jsonString_" + VarName + ");\n" +
  76. " Serial.print(\"HTTP code: \");\n" +
  77. " Serial.println(httpCode);\n" +
  78. " http_response = http.getString();\n" +
  79. " http.end();\n" +
  80. "}\n"
  81. // "delay(500);\n"
  82. return code;
  83. };
  84. // Blockly.Python['wificlient_http_put'] = function(block) {
  85. // var content = Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
  86. // var code = "http.POST("+content+");";
  87. // return code;
  88. // };
  89. Blockly.Python['wifiClient_http_put'] = function(block) {
  90. var content = Blockly.Python.valueToCode(block, 'DATA', Blockly.Python.ORDER_ATOMIC) || "";
  91. var url = Blockly.Python.valueToCode(block, 'URL', Blockly.Python.ORDER_ATOMIC) || "";
  92. var type = block.getFieldValue("TYPE");
  93. var code = "http.begin(" + url + ");\n";
  94. if (type == "Cloud Data") code += "http.addHeader(\"Content-Type\",\"application/json\");\n" +
  95. "httpCode = http.sendRequest(\"PUT\",\"" + content + "\")\n";
  96. else if (type == "String") code += "httpCode = http.sendRequest(\"PUT\"," + content + ")\n";
  97. code += "Serial.print(\"HTTP code: \");\n" +
  98. "Serial.println(httpCode);\n" +
  99. "http_response = http.getString();\n" +
  100. "http.end();\n"
  101. // "delay(500);\n"
  102. return code;
  103. };
  104. Blockly.Python['wifiClient_http_response'] = function(block) {
  105. var code = "http_response";
  106. return [code, Blockly.Python.ORDER_ATOMIC];
  107. };