当前位置:   article > 正文

esp8266通过串口与stm32通信,实现远程控制pca9685_服务器下发指令通过sp8266控制stm32

服务器下发指令通过sp8266控制stm32

首先本项目通过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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/588277
推荐阅读
相关标签
  

闽ICP备14008679号