123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 'use strict';
- goog.provide('Blockly.Python.wificlient');
- goog.require('Blockly.Python');
- Blockly.Python['wifiClient_http_setup'] = function(block) {
- var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
- Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
- var HTTPResponse = "String http_response;";
- Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
- var HTTPCode = "int httpCode;";
- Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
- let serial_setup = "Serial.begin(9600);";
- Blockly.Python.addSetup('serial_9600', serial_setup);
- var code = "HTTPClient http\n";
- Blockly.Python.addDeclaration("wifi_httpclient_setup", code);
- code = ''
- return code;
- };
- Blockly.Python['wifiClient_http_get'] = function(block) {
- var json_include = "#include <ArduinoJson.h>";
- Blockly.Python.addInclude("json_declaration", json_include);
- // var parse_data_type = "int ParseData_type = ";
- var varName = block.getFieldValue("VAR");
- var url = "http://192.168.4.1" + (block.getFieldValue('URL') || "");
- var code = "" +
- "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
- " http.begin(\"" + url + "\")\n" +
- " httpCode = http.GET()\n" +
- " // Serial.print(\"HTTP code: \")\n" +
- " // Serial.println(httpCode)\n" +
- " http_response = http.getString()\n" +
- " http.end()\n" +
- "}\n"
- // "delay(500)\n"
- code += "StaticJsonBuffer<800> jsonBuffer_" + varName + "\n" +
- "JsonObject& parse_" + varName + " = jsonBuffer_" + varName + ".parseObject(http_response)\n";
- return code;
- };
- Blockly.Python['wifiClient_http_get_getValue'] = function(block) {
- var varName = block.getFieldValue("VAR") || '';
- var index = block.getFieldValue("INDEX") || '';
- var type = block.getFieldValue("TYPE") || '';
- var code = "parse_" + varName + "[\"data\"][" + index + "]"
- if (type == "Int") code += ".as<int>()"
- else code += ".as<String>()"
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['wifiClient_http_post'] = function(block) {
- var json_include = "#include <ArduinoJson.h>";
- Blockly.Python.addInclude("json_include", json_include);
- var precode = "char JSONmessageBuffer[240]\n";
- Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
- var VarName = block.getFieldValue('VAR') || "json";
- var code = "StaticJsonBuffer<800> JSONbuffer_" + VarName + "\n" +
- "JsonObject& " + VarName + "_json_ = JSONbuffer_" + VarName + ".createObject()\n" +
- "JsonArray& data_json_C = " + VarName + "_json_.createNestedArray(\"data\")\n" +
- // "JsonArray& data_type_C = " + VarName + "_json_.createNestedArray(\"type\")\n"+
- "";
- var content = "";
- var data_;
- var url = "http://192.168.4.1" + (block.getFieldValue('URL') || "");
- for (let i = 0; i < block.itemCount_; i++) {
- data_ = Blockly.Python.valueToCode(block, "ADD" + i);
- code += "data_json_C.add(" + data_ + ")\n";
- // if (block.childBlocks_[i].type == "text") {
- // code += "data_type_C.add(0)\n";
- // } else if (block.childBlocks_[i].type == "math_number") {
- // code += "data_type_C.add(1)\n";
- // }
- }
- code += "String jsonString_" + VarName + "\n" + VarName + "_json_.printTo(jsonString_" + VarName + ")\n";
- code += "" +
- "if ((millis() % 1000) /100 % 5 == 0 ) {\n" +
- " http.begin(\"" + url + "\")\n";
- code += " http.addHeader(\"Content-Type\",\"application/json\")\n";
- code += " httpCode = http.POST(jsonString_" + VarName + ")\n" +
- " Serial.print(\"HTTP code: \")\n" +
- " Serial.println(httpCode)\n" +
- " http_response = http.getString()\n" +
- " http.end()\n" +
- "}\n"
- // "delay(500)\n"
- return code;
- };
- // Blockly.Python['wificlient_http_put'] = function(block) {
- // var content = Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
- // var code = "http.POST("+content+");";
- // return code;
- // };
- Blockly.Python['wifiClient_http_put'] = function(block) {
- var content = Blockly.Python.valueToCode(block, 'DATA', Blockly.Python.ORDER_ATOMIC) || "";
- var url = Blockly.Python.valueToCode(block, 'URL', Blockly.Python.ORDER_ATOMIC) || "";
- var type = block.getFieldValue("TYPE");
- var code = "http.begin(" + url + ")\n";
- if (type == "Cloud Data") code += "http.addHeader(\"Content-Type\",\"application/json\")\n" +
- "httpCode = http.sendRequest(\"PUT\",\"" + content + "\")\n";
- else if (type == "String") code += "httpCode = http.sendRequest(\"PUT\"," + content + ")\n";
- code += "Serial.print(\"HTTP code: \")\n" +
- "Serial.println(httpCode)\n" +
- "http_response = http.getString()\n" +
- "http.end()\n"
- // "delay(500)\n"
- return code;
- };
- Blockly.Python['wifiClient_http_response'] = function(block) {
- var code = "http_response";
- return [code, Blockly.Python.ORDER_ATOMIC];
- };
|