CODE (this code allow you to drive remotely your smars with the above application):
You need to change "YOUR SSID" and "YOUR WIFI PASSWORD" with your wifi name and password
The first time you run this code, open Arduino terminal and check on the serial monitor the printed IP of your Nodemcu. Now you can add this IP in your App under settings section. In this way you are ready to drive your Smars :D
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "YOUR SSID";
const char* password = "YOUR WIFI PASSWORD";
const int port = 80;
ESP8266WebServer server(port);
static const uint8_t motorB1 = 4 ;
static const uint8_t motorA1 = 13;
static const uint8_t motorB2 = 14;
static const uint8_t motorA2 = 15;
int motor_speed = 1024;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(motorA1, OUTPUT); // PMW A
pinMode(motorA2, OUTPUT); // PMW B
pinMode(motorB1, OUTPUT); // DIR A
pinMode(motorB2, OUTPUT); // DIR B
Serial.begin(115200);
Serial.print("Connecting to : ");
Serial.println(ssid);
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
pinMode(LED_BUILTIN, LOW);
delay(250);
pinMode(LED_BUILTIN, HIGH);
delay(250);
Serial.print(".");
}
Serial.println("Connection established!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/move", HTTP_GET, handleMoveRequest);
server.on("/action", HTTP_GET, handleActionRequest);
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
server.handleClient();
}
void handleMoveRequest() {
if (!server.hasArg("dir")) {
server.send(404, "text / plain", "Move: undefined");
return;
}
String direction = server.arg("dir");
if (direction.equals("F")) {
forward();
server.send(200, "text / plain", "Move: forward");
}
else if (direction.equals("B")) {
backward();
server.send(200, "text / plain", "Move: backward");
}
else if (direction.equals("S")) {
stop_motors();
server.send(200, "text / plain", "Move: stop");
}
else if (direction.equals("L")) {
turn_left();
server.send(200, "text / plain", "Turn: Left");
}
else if (direction.equals("R")) {
turn_right();
server.send(200, "text / plain", "Turn: Right");
}
else {
server.send(404, "text / plain", "Move: undefined");
}
}
void handleActionRequest() {
if (!server.hasArg("type")) {
server.send(404, "text / plain", "Action: undefined");
return;
}
String type = server.arg("type");
if (type.equals("1")) {
// TODO : Action 1
server.send(200, "text / plain", "Action 1");
}
else if (type.equals("2")) {
// TODO : Action 2
server.send(200, "text / plain", "Action 2");
}
else if (type.equals("3")) {
// TODO : Action 3
server.send(200, "text / plain", "Action 3");
}
else if (type.equals("4")) {
// TODO : Action 4
server.send(200, "text / plain", "Action 4");
}
else if (type.equals("5")) {
// TODO : Action 5
server.send(200, "text / plain", "Action 5");
}
else if (type.equals("6")) {
// TODO : Action 6
server.send(200, "text / plain", "Action 6");
}
else if (type.equals("7")) {
// TODO : Action 7
server.send(200, "text / plain", "Action 7");
}
else if (type.equals("8")) {
// TODO : Action 8
server.send(200, "text / plain", "Action 8");
}
else {
server.send(404, "text / plain", "Action: undefined");
}
}
void handleNotFound() {
server.send(404, "text / plain", "404: Not found");
}
void stop_motors() {
digitalWrite(motorA2, LOW);
digitalWrite(motorB2, LOW);
digitalWrite(motorA1, LOW);
digitalWrite(motorB1, LOW);
}
void forward() {
digitalWrite(motorA2, HIGH);
digitalWrite(motorA1, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}
void backward() {
digitalWrite(motorA2, LOW);
digitalWrite(motorB2, LOW);
digitalWrite(motorA1, HIGH);
digitalWrite(motorB1, HIGH);
}
void turn_left() {
digitalWrite(motorA2, LOW);
digitalWrite(motorB2, HIGH);
digitalWrite(motorA1, HIGH);
digitalWrite(motorB1, LOW);
}
void turn_right() {
digitalWrite(motorA2, HIGH);
digitalWrite(motorB2, LOW);
digitalWrite(motorA1, LOW);
digitalWrite(motorB1, HIGH);
}