Skip to content

Instantly share code, notes, and snippets.

@nomolk
Last active July 9, 2018 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomolk/77a958544652474be339ea2833d36ebe to your computer and use it in GitHub Desktop.
Save nomolk/77a958544652474be339ea2833d36ebe to your computer and use it in GitHub Desktop.
A sketch for M5Stack to be controlled with Scratch and send infrared signal for JJRC CADY WIGI
#include <M5Stack.h>
#include <WiFi.h>
#include <IRremote.h>
#include <WiFiClient.h>
String wifi_ssid = "*****"; //wifiのssid
String wifi_password = "*****"; //wifiのパスワード
int sendPin = 26;
boolean settingMode;
const char* host = "xxx.xxx.xxx.xxx"; //Scratchの入っているPCのIPアドレス
const int Port = 42001;
IRsend irsend(sendPin);
WiFiClient client;
//Go forward
unsigned int signalGoForward[35] = {2050,1750, 2050,400, 900,400, 900,400, 900,950, 400,400, 900,400, 900,450, 850,450, 900,400, 900,900, 400,450, 850,450, 900,400, 900,900, 400,450, 850,950, 400}; // UNKNOWN 8C4D61EC
//Go backward
unsigned int signalGoBack[35] = {2000,1750, 2050,400, 900,450, 850,450, 850,950, 400,400, 900,450, 850,950, 400,900, 400,400, 900,950, 400,400, 850,450, 900,900, 400,450, 850,450, 850,450, 900}; // UNKNOWN 3F10CB0
//Turn right
unsigned int signalTurnRight[35] = {2050,1750, 2050,400, 850,450, 900,400, 900,950, 400,400, 850,450, 900,900, 400,450, 850,450, 850,950, 400,400, 900,450, 850,450, 850,950, 400,900, 400,950, 400}; // UNKNOWN 52291ED4
//Turn left
unsigned int signalTurnLeft[35] = {2050,1750, 2050,350, 900,450, 900,400, 900,900, 400,450, 850,950, 400,400, 900,400, 900,450, 850,950, 350,450, 900,400, 900,900, 400,450, 900,400, 900,900, 400}; // UNKNOWN 5F1187B5
//Eye
unsigned int signalToggleEyes[35] = {2050,1750, 2050,400, 900,400, 900,400, 900,400, 900,950, 400,900, 400,900, 400,400, 900,450, 900,900, 350,450, 900,400, 900,450, 900,400, 900,900, 400,900, 400}; // UNKNOWN 2FE676C8
void setup() {
Serial.begin(115200);
m5.begin();
m5.Lcd.setTextSize(5);
delay(10);
WiFi.begin(wifi_ssid.c_str(), wifi_password.c_str());
if (!checkConnection()) {
M5.Lcd.println("Wifi connection failed");
Serial.println("Wifi connection failed");
}
settingMode = false;
int count = 0;
while ( count < 10 ) {
if (client.connect(host, Port)) {
Serial.print("create tcp ok\r\n");
break;
}
M5.Lcd.println("connection failed");
Serial.println("connection failed");
count++;
}
//「を送る」のプルダウンに出すために1回メッセージを送る
sendMessage("broadcast \"MAE\"");
sendMessage("broadcast \"USHIRO\"");
sendMessage("broadcast \"MIGI\"");
sendMessage("broadcast \"HIDARI\"");
sendMessage("broadcast \"MABATAKI\"");
//初回なぜか失敗するので1回ダミーシグナルを送っておく
irsend.sendRaw({0}, 0, 38);
}
boolean checkConnection() {
int count = 0;
Serial.print("Waiting for Wi-Fi connection");
M5.Lcd.print("Waiting for Wi-Fi connection");
while ( count < 30 ) {
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
M5.Lcd.println();
Serial.println("Connected!");
M5.Lcd.println("Connected!");
delay(500);
return (true);
}
delay(500);
Serial.print(".");
M5.Lcd.print(".");
count++;
}
Serial.println("Timed out.");
M5.Lcd.println("Timed out.");
return false;
}
void sendMessage(const char* message){
char scmd[128] = {0};
strcpy(scmd+4, message);
scmd[3]=(uint8_t)strlen(scmd+4);
if(0!=strlen(scmd+4)) {
for(uint32_t i = 0; i < 4 + strlen(scmd + 4); i++) {
Serial.print(scmd[i]);
}
if (client.write((const uint8_t*)scmd, 4 + strlen(scmd+4))) {
Serial.println("send ok");
} else {
Serial.println("send err");
}
}
}
void showText(String str){
M5.Lcd.setCursor(50, 100);
M5.Lcd.clear();
Serial.println(str);
M5.Lcd.println(str);
}
void loop() {
uint8_t bufferC[128] = {0};
char bufferS[128] = {0};
client.setTimeout(100);
uint32_t len = client.readBytes(bufferC, sizeof(bufferC));
if (len > 0) {
String messageC;
for(uint32_t i = 4; i < len; i++) {
messageC.concat(String((char)bufferC[i]));
}
Serial.print("Received:[");
Serial.print(messageC);
Serial.print("]\r\n");
if(messageC.startsWith("broadcast")){
if(messageC.indexOf("\"MAE\"") > -1){
Serial.println("MAE");
showText("MAE");
irsend.sendRaw(signalGoForward, 35, 38);
} else if(messageC.indexOf("\"USHIRO\"") > -1){
Serial.println("USHIRO");
showText("USHIRO");
irsend.sendRaw(signalGoBack, 35, 38);
} else if(messageC.indexOf("\"MIGI\"") > -1){
Serial.println("MIGI");
showText("MIGI");
irsend.sendRaw(signalTurnRight, 35, 38);
} else if(messageC.indexOf("\"HIDARI\"") > -1){
Serial.println("HIDARI");
showText("HIDARI");
irsend.sendRaw(signalTurnLeft, 35, 38);
} else if(messageC.indexOf("\"MABATAKI\"") > -1){
Serial.println("MABATAKI");
showText("MABATAKI");
irsend.sendRaw(signalToggleEyes, 35, 38);
delay(3000);
irsend.sendRaw(signalToggleEyes, 35, 38);
delay(2000);
}
}
}
delay(10);
int sensorValueR = analogRead(35);
int sensorValueL = analogRead(36);
Serial.println(sensorValueR);
Serial.println(sensorValueL);
String messageS = String("sensor-update \"MIGI SENSOR\" ") + String(sensorValueR) + String(" \"HIDARI SENSOR\" ") + String(sensorValueL);
Serial.println(messageS);
messageS.toCharArray(bufferS, sizeof(bufferS));
sendMessage(bufferS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment