Controlling From a Phone

Using Blynk.cc 

The Blynk app provides a user-friendly way to build user interfaces for IoT projects. The following sketch can be used with the Blynk app (avaialble from Blynk.cc).

Firmware setup (Arduino) 

Download file Copy to clipboard
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <NewPing.h>

#define TRIGGER_PIN  10
#define ECHO_PIN     7
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Pin assignments
#define AIN1 3
#define AIN2 4
#define APWM 5
#define BIN1 12
#define BIN2 13
#define BPWM 11
#define STBY 6

int x_speed = 0;
int y_speed = 0;
int distance = 0;

enum motor_state_t {
    STOP,
    FORWARD,
    BACKWARD
};

void setLeftMotor(motor_state_t direction, uint8_t speed){
    if(direction == STOP){
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, LOW);
    }else if(direction == BACKWARD){
        digitalWrite(AIN1, HIGH);
        digitalWrite(AIN2, LOW);
    }else if(direction == FORWARD){
        digitalWrite(AIN1, LOW);
        digitalWrite(AIN2, HIGH);
    }
    analogWrite (APWM, speed);
}

void setRightMotor(int8_t direction, uint8_t speed){
    if(direction == STOP){
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, LOW);
    }else if(direction == BACKWARD){
        digitalWrite(BIN1, HIGH);
        digitalWrite(BIN2, LOW);
    }else if(direction == FORWARD){
        digitalWrite(BIN1, LOW);
        digitalWrite(BIN2, HIGH);
    }
    analogWrite (BPWM, speed);
}

void stopRobot(){
    digitalWrite(STBY, LOW);
}

void goRobot(){
    digitalWrite(STBY, HIGH);
}


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "authtoken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "password";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8, 9); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{
    // Initialize pins as outputs
    pinMode (STBY, OUTPUT);
    pinMode (AIN1, OUTPUT);
    pinMode (AIN2, OUTPUT);
    pinMode (APWM, OUTPUT);
    pinMode (BIN1, OUTPUT);
    pinMode (BIN2, OUTPUT);
    pinMode (BPWM, OUTPUT);
    pinMode (TRIGGER_PIN, OUTPUT);
    pinMode (ECHO_PIN, INPUT);
    // Set console baud rate
    Serial.begin(9600);
    delay(10);
    // Set ESP8266 baud rate
    EspSerial.begin(ESP8266_BAUD);
    delay(10);

    Blynk.begin(auth, wifi, ssid, pass);
}

BLYNK_READ(V5) {
    int uS = sonar.ping();
    distance = uS / US_ROUNDTRIP_CM;
    Serial.print("Ping: ");
    Serial.print(distance);
    Serial.println("cm");
    // Write to Blynk Virtual Pin 5
    Blynk.virtualWrite(V5, distance);
}

BLYNK_READ(V0){
    Blynk.virtualWrite(V0, 100);
}

BLYNK_WRITE(V2) {
    x_speed = param.asInt();
    Serial.print("x_speed: ");
    Serial.println(x_speed);
}

BLYNK_WRITE(V3) {
    y_speed = param.asInt();
    Serial.print("y_speed: ");
    Serial.println(y_speed);
}

BLYNK_WRITE(V1) {
    // Joystick range is 0 to 1024. Resting zero position of the joystick is 512
    const int ZERO_POSITION = 512;

    // Use a threshold to account for near zero position
    const int THRESHOLD_OFFSET = 90;

    // speed for a hard clouckwise/counterclockwise turn
    const int TURN_SPEED = 120;

    // Parameters received from Blynk app
    int x = param[0].asInt();
    int y = param[1].asInt();

    // Direction variables indicating direction of joystick movement
    // Possible values: -1, 0, 1
    int x_direction;
    int y_direction;

    // Print x and y values for debugging
    Serial.print("X = ");
    Serial.print(x);
    Serial.print("; Y = ");
    Serial.println(y);

    // Determine direction of the joystick
    x_direction = 0;
    y_direction = 0;

    if(x > (ZERO_POSITION + THRESHOLD_OFFSET)){
        x_direction = 1;
    }else if(x < (ZERO_POSITION - THRESHOLD_OFFSET)){
        x_direction = -1;
    }

    if(y > (ZERO_POSITION + THRESHOLD_OFFSET)){
        y_direction = 1;
    }else if(y < (ZERO_POSITION - THRESHOLD_OFFSET)){
        y_direction = -1;
    }

    // Move the WiFiBot according to the x and y direction:
    // STOP:       x= 0, y= 0
    // FORWARD:    x= 0, y= 1
    // BACKWARD:   x= 0, y=-1
    // RIGHT UP:   x= 1, y= 1
    // RIGHT:      x= 1, y= 0
    // RIGHT DOWN: x= 1, y=-1
    // LEFT UP:    x=-1, y=-1
    // LEFT:       x=-1, y= 0
    // LEFT DOWN:  x=-1, y=-1

    if (x_direction == 0 && y_direction ==0){
        // STOP: x= 0, y= 0
        stopRobot();
        setLeftMotor(STOP, 0);
        setRightMotor(STOP, 0);
        Serial.println("STOP");
    } else {
        // Robot is moving, set STBY pin accordingly using the goRobot function
        goRobot();
        Serial.print("Go ");

        // Set the direction and speed of the motors accordingly
        if(x_direction == 0){

            if(y_direction == 1){
                Serial.println("Forward");
                // FORWARD: x= 0, y= 1
                setLeftMotor(FORWARD, x_speed);
                setRightMotor(FORWARD, y_speed);
            }else if(y_direction == -1){
                // BACKWARD:   x= 0, y=-1
                Serial.println("Backward");
                setLeftMotor(BACKWARD, x_speed);
                setRightMotor(BACKWARD, y_speed);
            }
            //
        }else if(x_direction == 1){
            if(y_direction == 1){
                // RIGHT UP:   x= 1, y= 1
                Serial.println("Right up");
                setLeftMotor(FORWARD, x_speed);
                setRightMotor(STOP, y_speed);
            }else if(y_direction == 0){
                // RIGHT:      x= 1, y= 0
                Serial.println("Right");
                setLeftMotor(FORWARD, TURN_SPEED);
                setRightMotor(BACKWARD, TURN_SPEED);
            }else if(y_direction == -1){
                // RIGHT DOWN: x= 1, y=-1
                Serial.println("Right down");
                setLeftMotor(BACKWARD, x_speed);
                setRightMotor(STOP, y_speed);
            }
        }else if(x_direction == -1){
            if(y_direction == 1){
                // LEFT UP:    x=-1, y=-1
                Serial.println("Left up");
                setLeftMotor(STOP, x_speed);
                setRightMotor(FORWARD, y_speed);
            }else if(y_direction == 0){
                // LEFT:       x=-1, y= 0
                Serial.println("Left");
                setLeftMotor(BACKWARD, TURN_SPEED);
                setRightMotor(FORWARD, TURN_SPEED);
            }else if(y_direction == -1){
                // LEFT DOWN:  x=-1, y=-1
                Serial.println("Left down");
                setLeftMotor(STOP, x_speed);
                setRightMotor(BACKWARD, y_speed);
            }
        }
    }
}

void loop()
{
    Blynk.run();
}

Configuring Blynk app 

Download the Blynk app from the app store and follow the steps below to create a new project to control your robot. Each step matches the corresponding screenshot.

Click create a new project in the Blynk app

Give your project a name, select ESP8266, choose a theme, and click Create

Blynk will send you an email with your auth token. Copy and paste this into the Arduino code

A new blank canvas for your project. Hit the + icon in the upper right corner to add a new widget.

Add 2 Slider widgets, a Joystick widget, and a Value Display widget.

Your Blynk canvas should look similar to this.

Edit the slider widget by tapping on it. Then set the name to "right motor speed."

Set the output to Virtual pin V2

Change the upper limit from 1024 to 255

Edit the second slider widget similarly. Set its name to "left motor speed," the pin to Virtual pin V3, and the upper limit to 255

Edit the Joystick widget. Switch output from "split" to "merge."

Edit the Value Display widget. Set the name as "ping cm" and set the input to Virtual Pin V5.

Tap on the "Pin" box and set it to Virtual pin V1

Upload your code to the UNO board. Then, hit the play button on the blink app highlighted in the upper right corner of this screenshot.

Your Blynk sketch is now live. Tap the chip-shaped button highlighted to ensure your device is connected.

If everything is working, you should see your device is online. Make sure there are batteries in your robot, and you're almost ready to start driving it around.

Set the right and left motor speed to 255 and you can use the joystick to drive it around. If the robot does not drive straight, you can tweak the motor speeds.

Click create a new project in the Blynk app
Click create a new project in the Blynk app
Give your project a name, select ESP8266, choose a theme, and click Create
Blynk will send you an email with your auth token. Copy and paste this into the Arduino code
A new blank canvas for your project. Hit the + icon in the upper right corner to add a new widget.
Add 2 Slider widgets, a Joystick widget, and a Value Display widget.
Your Blynk canvas should look similar to this.
Edit the slider widget by tapping on it. Then set the name to "right motor speed."
Set the output to Virtual pin V2
Change the upper limit from 1024 to 255
Edit the second slider widget similarly. Set its name to "left motor speed," the pin to Virtual pin V3, and the upper limit to 255
Edit the Joystick widget. Switch output from "split" to "merge."
Edit the Value Display widget. Set the name as "ping cm" and set the input to Virtual Pin V5.
Tap on the "Pin" box and set it to Virtual pin V1
Upload your code to the UNO board. Then, hit the play button on the blink app highlighted in the upper right corner of this screenshot.
Your Blynk sketch is now live. Tap the chip-shaped button highlighted to ensure your device is connected.
If everything is working, you should see your device is online. Make sure there are batteries in your robot, and you're almost ready to start driving it around.
Set the right and left motor speed to 255 and you can use the joystick to drive it around. If the robot does not drive straight, you can tweak the motor speeds.