电路如上图
实物图如下
(电压表用来监视12V输入电压
效果:
WIFI 遥控 基于ESP8266
注意:
连着ESP8266_01的时候可能无法从电脑向arduino上载程序,此时可以拔掉arduino上的TX 和RX(原理不明,但是管用
操控软件:
在网页上打开:
Remote control Arduino - RemoteXYOnline designer of graphical interface to control Arduino via smartphone or tablethttps://remotexy.com/en/editor/添加组件:
Config如下:
Module interface 如下:
最下面的view可以配置外观,组件名(viriable name)(记住,编程时要用):
配置完后,点击Get source code:
就可以看到代码模板:
记得点击download library, 并以zip格式导入编译器:
网站提供的代码只是一个stump,这几部分对我们有用:
每次配置完UI后可能需要在arduino IDE里更改,其余部分基本不用动。
我的代码如下:
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT
#include <RemoteXY.h>
#include <Servo.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 115200
#define REMOTEXY_WIFI_SSID "WiFi Test"
#define REMOTEXY_WIFI_PASSWORD "123456789"
#define REMOTEXY_SERVER_PORT 6377
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,3,0,22,0,97,0,13,16,0,
5,49,49,15,36,36,2,26,31,4,
48,12,7,11,51,65,26,67,4,36,
7,20,5,2,26,11,67,4,77,7,
20,5,2,26,11,129,0,35,4,22,
3,17,76,101,102,116,77,111,116,111,
114,80,111,119,101,114,32,32,32,32,
32,32,32,32,32,32,32,32,32,32,
32,32,32,32,32,32,32,32,82,105,
103,104,116,77,111,116,111,114,80,111,
119,101,114,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
int8_t joystick_1_x; // =-100..100 x-coordinate joystick position
int8_t joystick_1_y; // =-100..100 y-coordinate joystick position
int8_t slider_1; // =-100..100 slider position
// output variables
char left_motor_val[11]; // string UTF8 end zero
char right_motor_val[11]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/
// END RemoteXY include //
/
//define MOTOR Controlling pins
#define robot_LEFT_motor1 4
#define robot_LEFT_motor2 5
#define LEFT_motor_speed 10
//define RIGHT Motor pins
#define robot_RIGHT_motor1 6
#define robot_RIGHT_motor2 7
#define RIGHT_motor_speed 11
//define two arrays with list of pins each other
uint8_t Right_Motor[3] = {robot_RIGHT_motor1,robot_RIGHT_motor2,RIGHT_motor_speed};
uint8_t LEFT_Motor [3]= {robot_LEFT_motor1,robot_LEFT_motor2,LEFT_motor_speed };
//Speed control of motors
void Wheel (uint8_t * motor,int v)// v=motor speed control=pointer to an array of pins
{
if (v > 100) v=100;
if (v <-100) v=100;
if (v > 0)
{
digitalWrite (motor [0],HIGH);
digitalWrite (motor [1],LOW);
analogWrite (motor [2],v * 2.55);
}
else if(v<0)
{
digitalWrite (motor [0],LOW);
digitalWrite (motor [1],HIGH);
analogWrite (motor [2], (-v) * 2.55);
}
else
{
digitalWrite (motor [0],LOW);
digitalWrite (motor [1],LOW);
analogWrite (motor [2],0);
}
}
void setup()
{
RemoteXY_Init ();
//Serial.begin(9600); // set data transmission rate to communicate with computer
//initialization pins
pinMode (robot_LEFT_motor1,OUTPUT);
pinMode (robot_LEFT_motor2,OUTPUT);
pinMode (robot_RIGHT_motor1,OUTPUT);
pinMode (robot_RIGHT_motor2,OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// TODO you setup code
}
void loop()
{
RemoteXY_Handler ();
//manage the motor
double right_motor = RemoteXY.joystick_1_y - RemoteXY.joystick_1_x;
double left_motor = RemoteXY.joystick_1_y + RemoteXY.joystick_1_x;
Wheel (Right_Motor, right_motor);
Wheel (LEFT_Motor, left_motor );
//servo nutural position is 90deg
//tell servo to go to 90deg +/- slider value
myservo.write( RemoteXY.slider_1 +90);
//show number on phone screen
dtostrf(left_motor/2, 0, 1, RemoteXY.left_motor_val);
dtostrf(right_motor/2, 0, 1, RemoteXY.right_motor_val);
//print joystick x y coord on the screen
/* Serial.print("joystic y: ") ; // Prints the distance on the Serial Monitor
Serial.println(RemoteXY.joystick_1_y);
Serial.println("");
Serial.print("joystic x: ") ; // Prints the distance on the Serial Monitor
Serial.println(RemoteXY.joystick_1_x);
Serial.println("");
//print slider value on the screen
Serial.print("slider: ") ; // Prints the distance on the Serial Monitor
Serial.println(RemoteXY.slider_1);
Serial.println("");*/
}