むとうたけし(@610t)
Scratch Remote Sensors Protocol (遠隔センサープロトコル) は、ネットワーク経由でScratchと情報をやり取りするためのプロトコルです。
Scratch側では、TCP42001で遠隔センサーからの接続を待ち受けします。
<size: 4 bytes><msg: size bytes>
メッセージを送ります。
→broadcast "btn"
broadcast "btn"→
すべてのスプライト用変数の値の変更をおこなう。
sensor-update "h" 29 "t" 27 "btn" 0 →
→sensor-update "r" 10 "g" 20 "b" 30
ScratchからWifiで遠隔操作してみる: Scratch 遠隔センサープロトコルの実装例
#include <ESP8266WiFi.h> const char* ssid = "PUT SSID HERE"; const char* password = "PUT PWD HERE"; const char* host = "PUT PC IP ADDRESS HERE"; const int Port = 42001; void setup() { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void loop() { WiFiClient client; if (!client.connect(host, Port)) {return;} // fail (snip) if (client.write((const uint8_t*)scmd, 4 + strlen(scmd+4))) { (snip) }
void loop() { (snip) if(digitalRead(4)!=sw) { sw=digitalRead(4);stat=1; strcpy(scmd+4,"broadcast \"m\""); } else { if(stat==1) { stat=0; if(digitalRead(4)==LOW) { strcpy(scmd+4,"sensor-update \"v\" 0 "); } else { (snip) scmd[3]=(uint8_t)strlen(scmd+4); // scmd[3]はコマンド長 if(0!=strlen(scmd+4)) { for(uint32_t i = 0; i < 4 + strlen(broadcastcmd); i++) { if (client.write((const uint8_t*)scmd, 4 + strlen(scmd+4))) { } else { (snip)
Scratch 1.4 is still alive!!!