Last active
July 9, 2018 00:47
-
-
Save nomolk/78ecc63b7a564ac0cb15f206e05647fb to your computer and use it in GitHub Desktop.
A sketch for ESP8266 to be controlled with Scratch and send infrared signal for JJRC CADY WIGI
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
#include <ESP8266WiFi.h> | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
const char* ssid = "*****"; //wifiのssid | |
const char* password = "*****"; //wifiのパスワード | |
const char* host = "xxx.xxx.xxx.xxx"; //Scratchの入っているPCのIPアドレス | |
const int Port = 42001; | |
#define IR_LED 4 | |
IRsend irsend(IR_LED); | |
WiFiClient client; | |
//Go forward | |
uint16_t 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 | |
uint16_t 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 | |
uint16_t 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 | |
uint16_t 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 | |
uint16_t 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() { | |
irsend.begin(); | |
Serial.begin(115200); | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void send_message(const char* message){ | |
char scmd[32] = {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]); | |
} //Serial.print(" "); | |
if (client.write((const uint8_t*)scmd, 4 + strlen(scmd+4))) { | |
Serial.println("send ok"); | |
} else { | |
Serial.println("send err"); | |
} | |
} | |
} | |
void loop() { | |
uint8_t buffer[128] = {0}; | |
delay(5000); | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
if (!client.connect(host, Port)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
Serial.print("create tcp ok\r\n"); | |
for(uint32_t n = 0; n <100000; n++) { | |
delay(10); | |
char scmd[32] = {0}; | |
int sw,stat; | |
// Read all from server and print them to Serial | |
client.setTimeout(100); | |
uint32_t len = client.readBytes(buffer, sizeof(buffer)); | |
if (len > 0) { | |
String message; | |
for(uint32_t i = 4; i < len; i++) { | |
message.concat(String((char)buffer[i])); | |
} | |
Serial.print("Received:["); | |
Serial.print(message); | |
Serial.print("]\r\n"); | |
if(message.startsWith("broadcast")){ | |
if(message.indexOf("\"MAE\"") > -1){ | |
Serial.println("MAE"); | |
irsend.sendRaw(signalGoForward, 35, 38); | |
} else if(message.indexOf("\"USHIRO\"") > -1){ | |
Serial.println("USHIRO"); | |
irsend.sendRaw(signalGoBack, 35, 38); | |
} else if(message.indexOf("\"MIGI\"") > -1){ | |
Serial.println("MIGI"); | |
irsend.sendRaw(signalTurnRight, 35, 38); | |
} else if(message.indexOf("\"HIDARI\"") > -1){ | |
Serial.println("HIDARI"); | |
irsend.sendRaw(signalTurnLeft, 35, 38); | |
} else if(message.indexOf("\"MABATAKI\"") > -1){ | |
Serial.println("MABATAKI"); | |
irsend.sendRaw(signalToggleEyes, 35, 38); | |
delay(3000); | |
irsend.sendRaw(signalToggleEyes, 35, 38); | |
delay(2000); | |
} | |
} | |
} | |
// send broadcast message | |
if(digitalRead(5)!=sw) { | |
sw=digitalRead(5);stat=1; | |
send_message("broadcast \"MAE\""); | |
send_message("broadcast \"USHIRO\""); | |
send_message("broadcast \"MIGI\""); | |
send_message("broadcast \"HIDARI\""); | |
send_message("broadcast \"MABATAKI\""); | |
} else { | |
if(stat==1) { | |
stat=0; | |
if(digitalRead(5)==LOW) { | |
strcpy(scmd+4,"sensor-update \"v\" 0 "); | |
} else { | |
strcpy(scmd+4,"sensor-update \"v\" 1 "); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment