|
/* |
|
* WifiCar.ino |
|
* |
|
*/ |
|
|
|
#include <Arduino.h> |
|
|
|
#include <ESP8266WiFi.h> |
|
#include <ESP8266WiFiMulti.h> |
|
#include <WebSocketsServer.h> |
|
#include <ESP8266WebServer.h> |
|
#include <ESP8266mDNS.h> |
|
#include <Hash.h> |
|
|
|
#define MOTORA_E D1 // Motor A Forward pin |
|
#define MOTORA_P D2 // Motor A PWM |
|
#define MOTORB_E D3 // Motor B Forward pin |
|
#define MOTORB_P D4 // Motor B PWM |
|
|
|
#define USE_SERIAL Serial |
|
|
|
void stopIt() { |
|
delay(500); |
|
digitalWrite(MOTORA_E, LOW); |
|
digitalWrite(MOTORA_P, LOW); |
|
digitalWrite(MOTORB_E, LOW); |
|
digitalWrite(MOTORB_P, LOW); |
|
} |
|
|
|
ESP8266WiFiMulti WiFiMulti; |
|
|
|
ESP8266WebServer server = ESP8266WebServer(80); |
|
WebSocketsServer webSocket = WebSocketsServer(81); |
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { |
|
|
|
switch(type) { |
|
case WStype_DISCONNECTED: |
|
USE_SERIAL.printf("[%u] Disconnected!\n", num); |
|
break; |
|
case WStype_CONNECTED: { |
|
IPAddress ip = webSocket.remoteIP(num); |
|
USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); |
|
|
|
// send message to client |
|
webSocket.sendTXT(num, "Connected"); |
|
} |
|
break; |
|
case WStype_TEXT: |
|
USE_SERIAL.printf("[%u] get Text: %s\n", num, payload); |
|
|
|
if(payload[0] == 'f') { |
|
digitalWrite(MOTORA_E, LOW); |
|
digitalWrite(MOTORA_P, HIGH); |
|
digitalWrite(MOTORB_E, LOW); |
|
digitalWrite(MOTORB_P, HIGH); |
|
stopIt(); |
|
} |
|
|
|
if(payload[0] == 'b') { |
|
digitalWrite(MOTORA_E, HIGH); |
|
digitalWrite(MOTORA_P, LOW); |
|
digitalWrite(MOTORB_E, HIGH); |
|
digitalWrite(MOTORB_P, LOW); |
|
stopIt(); |
|
} |
|
|
|
if(payload[0] == 'l') { |
|
digitalWrite(MOTORA_E, LOW); |
|
digitalWrite(MOTORA_P, LOW); |
|
digitalWrite(MOTORB_E, LOW); |
|
digitalWrite(MOTORB_P, HIGH); |
|
stopIt(); |
|
} |
|
|
|
if(payload[0] == 'r') { |
|
digitalWrite(MOTORA_E, LOW); |
|
digitalWrite(MOTORA_P, HIGH); |
|
digitalWrite(MOTORB_E, LOW); |
|
digitalWrite(MOTORB_P, LOW); |
|
stopIt(); |
|
} |
|
break; |
|
} |
|
} |
|
|
|
void setup() { |
|
|
|
USE_SERIAL.begin(115200); |
|
USE_SERIAL.println(); |
|
USE_SERIAL.println(); |
|
USE_SERIAL.println(); |
|
|
|
pinMode(MOTORA_E, OUTPUT); |
|
pinMode(MOTORA_P, OUTPUT); |
|
pinMode(MOTORB_E, OUTPUT); |
|
pinMode(MOTORB_P, OUTPUT); |
|
stopIt(); |
|
|
|
for(uint8_t t = 4; t > 0; t--) { |
|
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); |
|
USE_SERIAL.flush(); |
|
delay(1000); |
|
} |
|
|
|
WiFi.softAP("holan", "12345678"); |
|
IPAddress myIP = WiFi.softAPIP(); |
|
USE_SERIAL.print("AP IP address: "); |
|
USE_SERIAL.println(myIP); |
|
|
|
// start webSocket server |
|
webSocket.begin(); |
|
webSocket.onEvent(webSocketEvent); |
|
|
|
if(MDNS.begin("esp8266")) { |
|
USE_SERIAL.println("MDNS responder started"); |
|
} |
|
|
|
// handle index |
|
server.on("/", []() { |
|
// send index.html |
|
server.send(200, "text/html", |
|
"<html><head><script>" |
|
"var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);" |
|
"connection.onopen = function(){connection.send('Connect ' + new Date());};" |
|
"connection.onerror = function(error){console.log('WebSocket Error ', error);};" |
|
"connection.onmessage = function(e){console.log('Server: ', e.data);};" |
|
"function forward(){connection.send('f');}" |
|
"function backward(){connection.send('b');}" |
|
"function right(){connection.send('r');}" |
|
"function left(){connection.send('l');}" |
|
"</script></head><body>" |
|
"<b>Wifi Car</b><br/><br/>" |
|
"<button type=\"button\" onclick=\"forward()\">Forward</button>" |
|
"<button type=\"button\" onclick=\"backward()\">Backward</button>" |
|
"<button type=\"button\" onclick=\"right()\">Right</button>" |
|
"<button type=\"button\" onclick=\"left()\">Left</button>" |
|
"<br/></body></html>"); |
|
}); |
|
|
|
server.begin(); |
|
|
|
// Add service to MDNS |
|
MDNS.addService("http", "tcp", 80); |
|
MDNS.addService("ws", "tcp", 81); |
|
} |
|
|
|
void loop() { |
|
webSocket.loop(); |
|
server.handleClient(); |
|
} |