当前位置:   article > 正文

ESP8266制作远程遥控车_esp8266 遥控车代码

esp8266 遥控车代码

材料:

ESP8266 12F一块(10元出头)

L298N电机驱动模块一块(10元左右)

12V电源(15左右)

杜邦线若根

小车底盘+电机+螺丝+轮子(网上套件也就20多块钱)

接线:

8266的VIN针脚用杜邦线接L298N的5v孔

8266的G针脚用杜邦线接L298N的GND孔

8266的D0,D1,D2,D4针脚分别接到L298N的IN1,IN2,IN3,IN4针脚

左边两个电机分别接到OUT1,2口

右边两个电机分别接到OUT3,4口

不会接的有详细教程,下面放链接

代码部分:

  1. #define BLINKER_PRINT Serial
  2. #include <Servo.h>
  3. #include <ESP8266WiFi.h>
  4. #include <DNSServer.h>
  5. #include <ESP8266WebServer.h>
  6. #include <WiFiManager.h>
  7. #define BLINKER_WIFI
  8. //#define BLINKER_ESP_SMARTCONFIG//
  9. #include <Blinker.h>
  10. char auth[] = "2e5a0cbf4e19";
  11. char ssid[] = "";
  12. char pswd[] = "";
  13. #define BUTTON_2 "b2"
  14. #define BUTTON_4 "b4"
  15. #define BUTTON_5 "b5"
  16. #define BUTTON_6 "b6"
  17. #define BUTTON_8 "b8"
  18. #define slider_0 "r1"
  19. #define slider_1 "r2"
  20. BlinkerButton Button2(BUTTON_2);
  21. BlinkerButton Button4(BUTTON_4);
  22. BlinkerButton Button5(BUTTON_5);
  23. BlinkerButton Button6(BUTTON_6);
  24. BlinkerButton Button8(BUTTON_8);
  25. BlinkerSlider Slider0(slider_0);
  26. BlinkerSlider Slider1(slider_1);
  27. int pwm1=60;
  28. int pwm2=60;
  29. int flag;//判断当前状态
  30. void button2_callback(const String &state)//前进
  31. {
  32. BLINKER_LOG("get button state: ", state);
  33. if (state != "pressup")
  34. {
  35. flag=1;
  36. analogWrite(16, pwm1);
  37. digitalWrite(5, LOW);
  38. analogWrite(4, pwm2);
  39. digitalWrite(2, LOW);
  40. }
  41. }
  42. void button4_callback(const String &state)//左转
  43. {
  44. digitalWrite(16, LOW);
  45. analogWrite(5, pwm1);
  46. analogWrite(4, pwm2);
  47. digitalWrite(2, LOW);
  48. flag=3;
  49. }
  50. void button5_callback(const String &state)//停止
  51. {
  52. BLINKER_LOG("get button state: ", state);
  53. if (state != "pressup")
  54. {
  55. digitalWrite(16, HIGH);
  56. digitalWrite(5, HIGH);
  57. digitalWrite(4, HIGH);
  58. digitalWrite(2, HIGH);
  59. flag=5;
  60. }
  61. }
  62. void button6_callback(const String &state)//右转
  63. {
  64. BLINKER_LOG("get button state: ", state);
  65. if (state != "pressup")
  66. {
  67. analogWrite(16, pwm1);
  68. digitalWrite(5,LOW);
  69. digitalWrite(4, LOW);
  70. analogWrite(2,pwm2);
  71. flag=4;
  72. }
  73. }
  74. void button8_callback(const String &state)//后退
  75. {
  76. BLINKER_LOG("get button state: ", state);
  77. if (state != "pressup")
  78. {
  79. digitalWrite(16, LOW);
  80. analogWrite(5,pwm1);
  81. digitalWrite(4, LOW);
  82. analogWrite(2,pwm2);
  83. flag=2;
  84. }
  85. }
  86. void slider0_callback(int32_t value)
  87. {
  88. BLINKER_LOG("slider:", value);
  89. pwm1=value;
  90. switch(flag)
  91. {
  92. case 1: analogWrite(16, pwm1); break;
  93. case 2: analogWrite(5, pwm1); break;
  94. case 3: analogWrite(5, pwm1); break;
  95. case 4: analogWrite(16, pwm1); break;
  96. case 5: break;
  97. default:break;
  98. }
  99. }
  100. void slider1_callback(int32_t value)
  101. {
  102. BLINKER_LOG("slider:", value);
  103. pwm2=value;
  104. switch(flag)
  105. {
  106. case 1: analogWrite(4, pwm2); break;
  107. case 2: analogWrite(2, pwm2); break;
  108. case 3: analogWrite(2, pwm2); break;
  109. case 4: analogWrite(4, pwm2); break;
  110. case 5: break;
  111. default:break;
  112. }
  113. }
  114. //blinker初始化
  115. void blinker_car_init()
  116. {
  117. //1-4号引脚设置为输出模式
  118. pinMode(16, OUTPUT);//D0
  119. pinMode(5, OUTPUT);//D1
  120. pinMode(4, OUTPUT);//D2
  121. pinMode(2, OUTPUT);//D4
  122. }
  123. void setup()
  124. {
  125. // 初始化串口,并开启调试信息
  126. Serial.begin(115200);
  127. WiFiManager wifiManager;//自动配网
  128. wifiManager.autoConnect("北城以北");
  129. Serial.println("connected... :)");
  130. digitalWrite(D0, HIGH);
  131. digitalWrite(D1, HIGH);
  132. digitalWrite(D2, HIGH);
  133. digitalWrite(D4, HIGH);
  134. analogWriteRange(100);
  135. analogWriteFreq(50);
  136. //analogWrite(D2, 50);//0-100 50
  137. //初始化小车参数
  138. blinker_car_init();
  139. //按钮绑定
  140. Button2.attach(button2_callback);
  141. Button4.attach(button4_callback);
  142. Button5.attach(button5_callback);
  143. Button6.attach(button6_callback);
  144. Button8.attach(button8_callback);
  145. Slider0.attach(slider0_callback);
  146. Slider1.attach(slider1_callback);
  147. Blinker.begin(auth,ssid,pswd);
  148. // Blinker.begin(auth);
  149. }
  150. void loop()
  151. {
  152. Blinker.run();
  153. }

char auth[] = "2e5a0cbf4e19";//填写你的点灯密钥
char ssid[] = "";//可以不用填,配网的时候在填写
char pswd[] = "";//可以不用填,配网的时候在填写

把代码复制到arduino,需要的库自己下载,很基础的东西,我就不说了,不会的网上搜一下

这里强调一下就是代码上传的时候,工具里的flash size后面不能选择no SPIFFS,一定要注意,很多人就是因为没有注意这个地方,导致每次断电重启后,都需要重新配网,设备无法记住WIFI的账号密码!

有什么问题评论区留言,我有时间尽量会回复大家!平时比较忙,回复慢了还望见谅!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号