blocklyでobjectを動かす。 その9
概要
blocklyでobjectを動かして見る。
wemosで、Lチカ、やってみた。
写真
ブロックを書く。
Blockly.Blocks.send = {
init: function() {
this.jsonInit({
message0: "send %1",
args0: [{
type: "input_value",
name: "TEXT"
}],
previousStatement: null,
nextStatement: null,
style: "text_blocks",
tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
})
}};
フローを書く。
Blockly.Blocks.send = {
init: function() {
this.jsonInit({
message0: "send %1",
args0: [{
type: "input_value",
name: "TEXT"
}],
previousStatement: null,
nextStatement: null,
style: "text_blocks",
tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
})
}};
objectを書く。
Blockly.JavaScript.send = function(a) {
return "send_(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "''") + ");\n"
};
function send_(data) {
var str = "http://192.168.10.8/off";
if (data == 'on')
{
str= "http://192.168.10.8/on";
}
var xhr = new XMLHttpRequest();
xhr.open("GET", str, false);
xhr.send(null);
if (xhr.status != 200)
{
alert("ng");
}
else
{
var res = xhr.responseText;
alert(res);
}
}
サンプルコード
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char * ssid = "";
const char * password = "";
ESP8266WebServer server(80);
const int led = 2;
void handleRoot()
{
server.send(200, "text/plain", "hello from esp8266!");
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void)
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266"))
{
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/on", []() {
digitalWrite(led, 1);
Serial.println("on");
server.send(200, "text/json", "{text: \"on\"}");
});
server.on("/off", []() {
digitalWrite(led, 0);
Serial.println("off");
server.send(200, "text/json", "{text: \"off\"}");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}
成果物
Blockly.JavaScript.send = function(a) {
return "send_(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "''") + ");\n"
};
function send_(data) {
var str = "http://192.168.10.8/off";
if (data == 'on')
{
str= "http://192.168.10.8/on";
}
var xhr = new XMLHttpRequest();
xhr.open("GET", str, false);
xhr.send(null);
if (xhr.status != 200)
{
alert("ng");
}
else
{
var res = xhr.responseText;
alert(res);
}
}
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char * ssid = "";
const char * password = "";
ESP8266WebServer server(80);
const int led = 2;
void handleRoot()
{
server.send(200, "text/plain", "hello from esp8266!");
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void)
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266"))
{
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/on", []() {
digitalWrite(led, 1);
Serial.println("on");
server.send(200, "text/json", "{text: \"on\"}");
});
server.on("/off", []() {
digitalWrite(led, 0);
Serial.println("off");
server.send(200, "text/json", "{text: \"off\"}");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}
成果物
以上。
Author And Source
この問題について(blocklyでobjectを動かす。 その9), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/90c58e8686bdb4dc467d著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .