Last active
March 18, 2019 12:08
-
-
Save yoggy/0120fd07a52baa73b218e894a999ff7b to your computer and use it in GitHub Desktop.
ESPr IR(https://www.switch-science.com/catalog/2740/)で動作を確認したminirum.ino (2019/3/15)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// minirum.ino - ESPr IR(https://www.switch-science.com/catalog/2740/)で動作を確認したminirum.ino (2019/3/15) | |
// | |
// オリジナルはこちら | |
// https://github.com/9SQ/minIRum/blob/master/minirum/minirum.ino | |
// | |
// オリジナルからの修正点 | |
// 1. POST時のContent-Typeをapplication/jsonに変更 (最近のライブラリだとこれを指定しないとreq.length()==0になるっぽい?) | |
// 2. 長いコードを受信するためにタイムアウトを設定 | |
// | |
// require | |
// https://github.com/esp8266/Arduino (ver.2.5.0) | |
// https://github.com/markszabo/IRremoteESP8266 (ver.2.5.6) | |
// https://github.com/interactive-matter/aJson (要手動ダウンロード。1.8.5だとaJsonStream::flush()の宣言を修正する必要あり) | |
// | |
// how to use... | |
// http://ipアドレス/ にアクセスして、学習したリモコンデータを取得 | |
// (??????の部分はMACアドレス) | |
// | |
// {"format":"raw","freq":38,"data":[9050,4400,700,500,700,500,700,500,700,500,700,500,700,500,700,1600,700,500,700,1600,700,1600,700,1600,700,1600,700,1600,700,1600,700,500,700,1600,700,500,700,1600,700,500,700,500,700,1600,700,500,700,500,700,500,700,1600,700,500,700,1600,700,1600,700,500,700,1600,700,1600,700,1600,700]} | |
// {"format":"raw","freq":38,"data":[9000,2250,650]} | |
// | |
// POSTすると送信できる | |
// $ curl -i "http://ipアドレス/messages" -d '{"format":"raw","freq":38,"data":[9050,4400,700,500,700,500,700,500,700,500,700,500,700,500,700,1600,700,500,700,1600,700,1600,700,1600,700,1600,700,1600,700,1600,700,500,700,1600,700,500,700,1600,700,500,700,500,700,1600,700,500,700,500,700,500,700,1600,700,500,700,1600,700,1600,700,500,700,1600,700,1600,700,1600,700]}' | |
// | |
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <ESP8266WebServer.h> | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
#include <IRrecv.h> | |
#include <aJSON.h> | |
#include <WiFiClient.h> | |
const int send_pin = 14; | |
const int recv_pin = 5; | |
const char *ssid = "ssid"; | |
const char *password = "password"; | |
char *localName = "minirum-"; | |
String histIRcode; | |
const uint16_t kCaptureBufferSize = 1024; | |
const uint8_t kTimeout = 100; | |
ESP8266WebServer webServer(80); | |
IRsend irsend(send_pin); | |
IRrecv irrecv(recv_pin, kCaptureBufferSize, kTimeout, true); | |
void handleMessages() | |
{ | |
if (webServer.method() == HTTP_POST) | |
{ | |
irrecv.disableIRIn(); | |
String req = webServer.arg(0); | |
char json[req.length() + 1]; | |
req.toCharArray(json, req.length() + 1); | |
Serial.println("handleMessages()"); | |
Serial.print("req.length() = "); | |
Serial.println(req.length()); | |
Serial.println(json); | |
aJsonObject *root = aJson.parse(json); | |
if (root != NULL) | |
{ | |
aJsonObject *freq = aJson.getObjectItem(root, "freq"); | |
aJsonObject *data = aJson.getObjectItem(root, "data"); | |
if (freq != NULL && data != NULL) | |
{ | |
const int d_size = aJson.getArraySize(data); | |
uint16_t rawData[d_size]; | |
for (int i = 0; i < d_size; i++) | |
{ | |
aJsonObject *d_int = aJson.getArrayItem(data, i); | |
rawData[i] = d_int->valueint; | |
} | |
irsend.sendRaw(rawData, (uint16_t)d_size, (uint16_t)(freq->valueint)); | |
irrecv.enableIRIn(); | |
req = ""; | |
aJson.deleteItem(root); | |
webServer.send(200, "text/plain", "ok"); | |
} | |
else | |
{ | |
webServer.send(400, "text/plain", "Invalid JSON format"); | |
} | |
} | |
else | |
{ | |
webServer.send(400, "text/plain", "Request body is empty"); | |
} | |
} | |
else if (webServer.method() == HTTP_GET) | |
{ | |
String s = "{\"format\":\"raw\",\"freq\":38,\"data\":["; | |
s += histIRcode; | |
s += "]}"; | |
histIRcode = ""; | |
webServer.send(200, "text/plain", s); | |
} | |
} | |
void handleIndex() | |
{ | |
String s = "<html lang=\"en\"><head><meta charset=\"utf-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>minIRum</title><link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css\" /><script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script><script src=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js\"></script></head><body><div class=\"container\"><div class=\"row\"><div class=\"col-md-12\"><h1>minIRum console <small>"; | |
s += localName; | |
s += ".local</small></h1><p>IP address: "; | |
s += String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]); | |
s += "</p><hr><div class=\"form-group\"><textarea class=\"form-control\" id=\"message\" rows=\"10\"></textarea></div><button class=\"btn btn-primary\" id=\"get\">GET</button> <button class=\"btn btn-success\" id=\"post\">POST</button></div></div><script>var xhr = new XMLHttpRequest();var textarea = document.getElementById(\"message\");document.getElementById(\"get\").addEventListener(\"click\", function () {xhr.open('GET', '/messages', true);xhr.onreadystatechange = function() {if(xhr.readyState == 4) {textarea.value =xhr.responseText;}};xhr.send(null);});document.getElementById(\"post\").addEventListener(\"click\", function () {data = textarea.value;xhr.open('POST', '/messages', true);xhr.onreadystatechange = function() {if(xhr.readyState == 4) {alert(xhr.responseText);}};xhr.setRequestHeader('Content-Type', 'application/json');xhr.send(data);});</script></body></html>"; | |
webServer.send(200, "text/html", s); | |
} | |
void handleNotFound() | |
{ | |
webServer.send(404, "text/plain", "Not found."); | |
} | |
String dumpIRcode(decode_results *results) | |
{ | |
String s = ""; | |
for (int i = 1; i < results->rawlen; i++) | |
{ | |
s += results->rawbuf[i] * RAWTICK; | |
if (i < results->rawlen - 1) | |
s += ","; | |
} | |
return s; | |
} | |
void setup() | |
{ | |
byte mac[6]; | |
char buf[6]; | |
irsend.begin(); | |
irrecv.enableIRIn(); | |
Serial.begin(115200); | |
Serial.println(); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
WiFi.macAddress(mac); | |
sprintf(buf, "%02x%02x%02x", mac[3], mac[4], mac[5]); | |
strcat(localName, buf); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(200); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("http://"); | |
Serial.print(WiFi.localIP()); | |
Serial.println("/"); | |
Serial.print("http://"); | |
Serial.print(localName); | |
Serial.print(".local"); | |
Serial.println("/"); | |
webServer.on("/messages", handleMessages); | |
webServer.on("/", handleIndex); | |
webServer.onNotFound(handleNotFound); | |
webServer.begin(); | |
Serial.println("Web server started."); | |
MDNS.begin(localName); | |
MDNS.addService("http", "tcp", 80); | |
} | |
void loop() | |
{ | |
MDNS.update(); | |
webServer.handleClient(); | |
decode_results results; | |
if (irrecv.decode(&results)) | |
{ | |
if (results.overflow) | |
Serial.printf( | |
"WARNING: IR code is too big for buffer (>= %d). " | |
"This result shouldn't be trusted until this is resolved. " | |
"Edit & increase kCaptureBufferSize.\n", | |
kCaptureBufferSize); | |
histIRcode = dumpIRcode(&results); | |
irrecv.resume(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment