赞
踩
首先本项目通过esp8266连接服务器,实现远程通信,在和stm32串口通信实现pca9685的远程控制
1.esp8266代码
#include <Arduino.h>
#include <ESP8266WiFi.h>//ESP32WIFI
#include <PubSubClient.h>//MQTT
#include <SoftwareSerial.h>
//wifi配置
const char *ssid="Redmi K40";
const char *password="12345678";
const char *mqtt_broker = "154.8.192.235";
const char *mqtt_username = "admin";
const char *mqtt_password = "12345678";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
SoftwareSerial espSerial(1, 3);
void setup()
{
Serial.begin(115200);
espSerial.begin(9600);
WiFi.begin(ssid,password);
while(WiFi.status() !=WL_CONNECTED)
{
delay(500);
Serial.println("Connecting to WiFi");
}
client.setServer(mqtt_broker,mqtt_port);
while(!client.connected())
{
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
} else {
delay(2000);
}
}
}
//回调函数只能有一个
void callback(char* topic, byte* message, unsigned int length) {
char message_str=(char)message[0];
/*for (int i = 0; i < length; i++) {
message_str += (char)message[i];
}*/
Serial.println(message_str);
if(message_str=='1')
{espSerial.println("1");}
else if(message_str=='3')
{
espSerial.println("3");
}
}
void loop() {
client.loop();
client.subscribe("ESP8266");
client.setCallback(callback);//client.setCallback(callback)在vscode中只能放在loop函数中而不能放在setup中,而且在arduino中无论是放在哪里速度都差不多
delay(50);
}
2.stm32代码请到github下载https://github.com/fengyib/Remote-Control-of-Stepper-Motor
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。