Created
December 31, 2017 11:20
-
-
Save YushengLi/7239284a39080e7bffd1f0b2dde3587e to your computer and use it in GitHub Desktop.
Remote Control Arduino 4060 Car Over WiFi with self hosted Blynk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************* | |
Download latest Blynk library here: | |
https://github.com/blynkkk/blynk-library/releases/latest | |
Blynk is a platform with iOS and Android apps to control | |
Arduino, Raspberry Pi and the likes over the Internet. | |
You can easily build graphic interfaces for all your | |
projects by simply dragging and dropping widgets. | |
Downloads, docs, tutorials: http://www.blynk.cc | |
Sketch generator: http://examples.blynk.cc | |
Blynk community: http://community.blynk.cc | |
Follow us: http://www.fb.com/blynkapp | |
http://twitter.com/blynk_app | |
Blynk library is licensed under MIT license | |
This example code is in public domain. | |
************************************************************* | |
This example shows how to use ESP8266 Shield (with AT commands) | |
to connect your project to Blynk. | |
WARNING! | |
It's very tricky to get it working. Please read this article: | |
http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware | |
Change WiFi ssid, pass, and Blynk auth token to run :) | |
Feel free to apply it to any other example. It's simple! | |
*************************************************************/ | |
/* Comment this out to disable prints and save space */ | |
#define BLYNK_PRINT Serial | |
#include <ESP8266_Lib.h> | |
#include <BlynkSimpleShieldEsp8266.h> | |
#include <Servo.h> | |
Servo servo; | |
// You should get Auth Token in the Blynk App. | |
// Go to the Project Settings (nut icon). | |
char auth[] = "token"; | |
// Your WiFi credentials. | |
// Set password to "" for open networks. | |
char ssid[] = "ssid"; | |
char pass[] = "password"; | |
// Hardware Serial on Mega, Leonardo, Micro... | |
//#define EspSerial Serial | |
// or Software Serial on Uno, Nano... | |
#include <SoftwareSerial.h> | |
SoftwareSerial EspSerial(2, 3); // RX, TX | |
// Your ESP8266 baud rate: | |
#define ESP8266_BAUD 9600 | |
ESP8266 wifi(&EspSerial); | |
void setup() | |
{ | |
// Debug console | |
Serial.begin(9600); | |
servo.attach(9); | |
pinMode( 5, OUTPUT); | |
pinMode( 6, OUTPUT); | |
pinMode(11, OUTPUT); | |
delay(1000); | |
// Set ESP8266 baud rate | |
EspSerial.begin(ESP8266_BAUD); | |
delay(1000); | |
Blynk.begin(auth, wifi, ssid, pass, "192.168.1.1"); | |
} | |
void loop() | |
{ | |
Blynk.run(); | |
} | |
BLYNK_CONNECTED() { | |
Blynk.syncAll(); | |
} | |
// Move Forward | |
BLYNK_WRITE(V0) { | |
int motorSpeed = param.asInt(); | |
forward(motorSpeed); | |
} | |
BLYNK_WRITE(V5) { | |
int motorSpeed = param.asInt(); | |
forward(motorSpeed); | |
} | |
void forward(int speed){ | |
analogWrite(A0, speed); | |
analogWrite(A2, speed); | |
} | |
// Move Backward | |
BLYNK_WRITE(V1) { | |
int motorSpeed = param.asInt(); | |
backward(motorSpeed); | |
} | |
BLYNK_WRITE(V6) { | |
int motorSpeed = param.asInt(); | |
backward(motorSpeed); | |
} | |
void backward(int speed) { | |
analogWrite(A1, speed); | |
analogWrite(A3, speed); | |
} | |
// Left Turn | |
BLYNK_WRITE(V2) { | |
int motorSpeed = param.asInt(); | |
leftTurn(motorSpeed); | |
} | |
BLYNK_WRITE(V7) { | |
int motorSpeed = param.asInt(); | |
leftTurn(motorSpeed); | |
} | |
void leftTurn(int speed) { | |
analogWrite(A1, speed); | |
analogWrite(A2, speed); | |
} | |
// Right Turn | |
BLYNK_WRITE(V3) { | |
int motorSpeed = param.asInt(); | |
rightTurn(motorSpeed); | |
} | |
BLYNK_WRITE(V8) { | |
int motorSpeed = param.asInt(); | |
rightTurn(motorSpeed); | |
} | |
void rightTurn(int speed) { | |
analogWrite(A0, speed); | |
analogWrite(A3, speed); | |
} | |
// RGB Control | |
BLYNK_WRITE(V9) { | |
analogWrite(5, param.asInt()); | |
} | |
BLYNK_WRITE(V10) { | |
analogWrite(6, param.asInt()); | |
} | |
BLYNK_WRITE(V11) { | |
analogWrite(11, param.asInt()); | |
} | |
// Servo Angle Control | |
BLYNK_WRITE(V4) { | |
setServoAngle(param.asInt()); | |
} | |
BLYNK_WRITE(V12) { | |
setServoAngle(param.asInt()); | |
} | |
void setServoAngle(int angle) { | |
servo.write(angle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment