-
Notifications
You must be signed in to change notification settings - Fork 3
/
NodeCar-6wire.ino
148 lines (115 loc) · 5.22 KB
/
NodeCar-6wire.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>
#define WifiSSID "<YOUR-WIFI-SSID>"
#define WifiPASS "<YOUR-WIFI-KEY>"
#define RIGHT D0
#define R0 D5
#define R1 D6
#define LEFT D1
#define L0 D7
#define L1 D8
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
ESP8266WebServer server = ESP8266WebServer(80);
WebSocketsServer webSocket = WebSocketsServer(81);
void Set_Speed(String MOTOR, int SPEED) {
USE_SERIAL.printf("[%u] Speed!\n", SPEED);
if(MOTOR == "left") {
if(SPEED >= 0) {
USE_SERIAL.printf("Left Forward!\n");
digitalWrite(L0, LOW);
digitalWrite(L1, HIGH);
} else {
USE_SERIAL.printf("Left Reverse!\n");
digitalWrite(L0, HIGH);
digitalWrite(L1, LOW);
SPEED = SPEED * -1;
}
analogWrite(LEFT, SPEED);
} else if(MOTOR == "right") {
if(SPEED >= 0) {
USE_SERIAL.printf("Right Forward!\n");
digitalWrite(R0, LOW);
digitalWrite(R1, HIGH);
} else {
USE_SERIAL.printf("Right Reverse!\n");
digitalWrite(R0, HIGH);
digitalWrite(R1, LOW);
SPEED = SPEED * -1;
}
analogWrite(RIGHT, SPEED);
}
}
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);
String strpayload = (char*)payload;
if(int location = strpayload.indexOf(':')) {
String MOTOR = strpayload.substring(0, location);
String SPEED = strpayload.substring(location+1);
int intSPEED = SPEED.toInt();
Set_Speed(MOTOR, intSPEED);
}
break;
}
}
void setup() {
//USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
pinMode(LEFT, OUTPUT);
pinMode(L0, OUTPUT);
pinMode(L1, OUTPUT);
pinMode(RIGHT, OUTPUT);
pinMode(R0, OUTPUT);
pinMode(R1, OUTPUT);
analogWrite(LEFT, 0);
analogWrite(RIGHT, 0);
WiFiMulti.addAP(WifiSSID, WifiPASS);
while(WiFiMulti.run() != WL_CONNECTED && millis() < 5000) {
delay(100);
}
// start webSocket server
webSocket.begin();
webSocket.onEvent(webSocketEvent);
if(MDNS.begin("robot")) {
USE_SERIAL.println("MDNS responder started");
}
// handle index
server.on("/", []() {
// send index.html
server.send(200, "text/html", "<html><head><title>Controller</title><script type=\"text/javascript\">function send(n){var o=parseInt(document.getElementById(n).value);console.log(n+\": \"+o.toString()),connection.send(n+\":\"+o.toString())}var connection=new WebSocket(\"ws://\"+location.hostname+\":81/\",[\"arduino\"]);connection.onopen=function(){connection.send(\"#Connect \"+new Date)},connection.onerror=function(n){console.log(\"WebSocket Error \",n)},connection.onmessage=function(n){console.log(\"WebSocket Message \",n.data)},document.addEventListener(\"DOMContentLoaded\",function(n){var o=document.getElementById(\"left\"),e=document.getElementById(\"right\");o.onmousedown=function(n){o.onmousemove=function(){send(\"left\")}},o.onmouseup=function(n){o.value=0,send(\"left\"),o.onmousemove=null},e.onmousedown=function(n){e.onmousemove=function(){send(\"right\")}},e.onmouseup=function(n){e.value=0,send(\"right\"),e.onmousemove=null},o.ontouchstart=function(n){o.ontouchmove=function(){send(\"left\")}},o.ontouchend=function(n){o.value=0,send(\"left\"),o.ontouchmove=null},e.ontouchstart=function(n){e.ontouchmove=function(){send(\"right\")}},e.ontouchend=function(n){e.value=0,send(\"right\"),e.ontouchmove=null}});</script></head><body><input id=\"left\" type=\"range\" min=\"-1023\" max=\"1023\" step=\"1\" value=\"0\" orient=\"vertical\" style=\"writing-mode: bt-lr;-webkit-appearance: slider-vertical; display: inline; float: left; height: 18vh; width: 25%; zoom: 5\" /><input id=\"right\" type=\"range\" min=\"-1023\" max=\"1023\" step=\"1\" value=\"0\" orient=\"vertical\" style=\"writing-mode: bt-lr;-webkit-appearance: slider-vertical; display: inline; float: right; height: 18vh; width: 25%; zoom: 5\" /></body></html>");
});
server.begin();
// Add service to MDNS
MDNS.addService("http", "tcp", 80);
MDNS.addService("ws", "tcp", 81);
}
void loop() {
webSocket.loop();
server.handleClient();
}