Skip to content

Instantly share code, notes, and snippets.

@jianingy
Last active January 6, 2018 14:35
Show Gist options
  • Save jianingy/b2ca34008de3b36d86abf3a40e861f99 to your computer and use it in GitHub Desktop.
Save jianingy/b2ca34008de3b36d86abf3a40e861f99 to your computer and use it in GitHub Desktop.
#include <M5Stack.h>
const int PIN_EA = 1;
const int PIN_I1 = 16;
const int PIN_I2 = 17;
const int CHANNEL_EA = 3;
const int PIN_EB = 3;
const int PIN_I3 = 21;
const int PIN_I4 = 22;
const int CHANNEL_EB = 4;
bool direction = true;
const char *wifi_ssid = "nby-family";
const char *wifi_secret = "******";
WiFiServer server(80);
void setup() {
M5.begin();
M5.Lcd.clear();
M5.Lcd.println("staring car32 ...");
Serial.begin(9600);
motor_init();
connect_wifi();
M5.Lcd.println("starting control server ...");
server.begin();
}
void connect_wifi() {
M5.Lcd.printf("Connecting to WiFi %s", wifi_ssid);
WiFi.begin(wifi_ssid, wifi_secret);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
M5.Lcd.println("connected.");
M5.Lcd.print("IP address is ");
M5.Lcd.println(WiFi.localIP());
}
void motor_init() {
M5.Lcd.println("initializing motors ...");
ledcSetup(CHANNEL_EA, 5000, 8);
ledcAttachPin(PIN_EA, CHANNEL_EA);
ledcSetup(CHANNEL_EB, 5000, 8);
ledcAttachPin(PIN_EB, CHANNEL_EB);
pinMode(PIN_EA, OUTPUT);
pinMode(PIN_EB, OUTPUT);
pinMode(PIN_I1, OUTPUT);
pinMode(PIN_I2, OUTPUT);
pinMode(PIN_I3, OUTPUT);
pinMode(PIN_I4, OUTPUT);
digitalWrite(PIN_I1, HIGH);
digitalWrite(PIN_I2, HIGH);
digitalWrite(PIN_I3, HIGH);
digitalWrite(PIN_I4, HIGH);
ledcWrite(CHANNEL_EA, 0);
ledcWrite(CHANNEL_EB, 0);
}
void activate_right(bool forward, int speed) {
if (forward) {
digitalWrite(PIN_I1, LOW);
digitalWrite(PIN_I2, HIGH);
} else {
digitalWrite(PIN_I1, HIGH);
digitalWrite(PIN_I2, LOW);
}
ledcWrite(CHANNEL_EA, speed);
}
void activate_left(bool forward, int speed) {
if (forward) {
digitalWrite(PIN_I3, LOW);
digitalWrite(PIN_I4, HIGH);
} else {
digitalWrite(PIN_I3, HIGH);
digitalWrite(PIN_I4, LOW);
}
ledcWrite(CHANNEL_EB, speed);
}
void deactivate_right() {
ledcWrite(CHANNEL_EA, 0);
digitalWrite(PIN_I1, HIGH);
digitalWrite(PIN_I2, HIGH);
}
void deactivate_left() {
ledcWrite(CHANNEL_EB, 0);
digitalWrite(PIN_I3, HIGH);
digitalWrite(PIN_I4, HIGH);
}
void loop() {
/* if(M5.BtnA.wasPressed()) { */
/* activate_left(direction, 250); */
/* } else if (M5.BtnA.wasReleased()) { */
/* deactivate_left(); */
/* } */
/* if(M5.BtnB.wasPressed()) { */
/* activate_right(direction, 250); */
/* } else if (M5.BtnB.wasReleased()) { */
/* deactivate_right(); */
/* } */
/* if(M5.BtnC.wasPressed()) { */
/* direction = !direction; */
/* } */
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
line.trim();
Serial.printf("command: [%s]\n", line);
if (line == "FD") {
activate_left(true, 250);
activate_right(true, 250);
} else if (line == "RD") {
activate_left(false, 250);
activate_right(false, 250);
} else if (line == "LT") {
activate_left(false, 250);
activate_right(true, 250);
} else if (line == "RT") {
activate_left(true, 250);
activate_right(false, 250);
}
delay(1000);
deactivate_left();
deactivate_right();
}
}
delay(1);
client.stop();
}
M5.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment