ESP小车控制 --物联网

  1. 系统需求分析

    目前很多信息技术业界大佬都把物联网视作继个人计算机、互联网之后,当今世界最具发展潜力的产业之一。物联网作为继互联网之后新一代的连接方式,正在有力带动传统产业转型升级,引领战略性新兴产业发展,推动社会生产和经济发展方式的深度变革。MQTT是教学中常见的物联网协议之一,MQTT协议是近几年才出现的吗?其实不然,MQTT诞生于1999年互联网初期。Ashton提出IoT概念也是在1999年,MQTT协议生逢其时。当时MIT Auto-ID Labs的Kevin Ashton为了把宝洁的供应链上的RFID标签和互联网连接起来,在1999年第一个提出了IoT这个概念。 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议)是一个基于客户端-服务器的消息发布/订阅传输协议。

  2. 系统初步设计,包括框架设计,功能设计

     手机app 和 esp32 连接到同一个  物联网服务器平台 ,相互订阅和发布话题达到数据互传,来控制esp32 对小车进行驱动。来达到控制小车前进的目地。
    

手机端.jpgimage.png
客户端监控数据用的是开源的第三方物联网服务器平台 无法进入后台只能接入后订阅话题

  1. 系统详细设计:
    (1)感知层:传感器,组网
    (2)网络层:传输网的选择、说明
    (3)应用层:数据处理,结果呈现

应用.png
代码构架

// 电位器连接到GPIO 34(模拟ADC1_CH6)
const int potPin = 34;

// 用于存储电位器值的变量
int potValue = 0;

void setup() {
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  // 读取电位计值
  potValue = analogRead(potPin);
  Serial.println(potValue);
  delay(500);
}

image.png

#include <AsyncTimer.h>
int pmPin  = 34;
int ledPin = 2;
int ch0 = 0;


AsyncTimer timer;
int taskId = 0;
void ChangeLedLightness()
{
    int  val = analogRead(pmPin);
    //Serial.println(val);


    auto vol = analogReadMilliVolts(pmPin);
    //Serial.println(vol-142);
    //读取换算毫伏


    int duty = val/4095.0*1024;
    ledcWrite(ch0,duty);
}


void setup()
{
  Serial.begin(115200) ;
  analogReadResolution(12);
  analogSetAttenuation(ADC_11db);
 
  ledcSetup(ch0,1000,10);
  ledcAttachPin(ledPin,ch0);
 
  taskId = timer.setInterval(ChangeLedLightness,20);//20 ms 一次
}


void loop() {
     timer.handle();
     
}

wifi 接入物联网控制系统

//WIFI 连接库和MQTT客户端
#include <WiFi.h>
#include <PubSubClient.h>


//***************** WiFi 账号密码************************
const char *ssid = "2580"; // Wifi 账号
const char *password = "1234567890";  // wifi 密码
// const char *ssid = "HUAWEI-5VBLX2"; // Wifi 账号
// const char *password = "d13545386916";  // wifi 密码
 
//**************** MQTT Broker 服务端连接****************
const char *mqtt_broker = "broker-cn.emqx.io";


const char *OutTopic = "ESP_Car/SELF";
const char *InTopic = "ESP_Car/control";
const char *mqtt_username = "ESP32_car";
const char *mqtt_password = "14789632";
const int mqtt_port = 1883;
//车灯
const int LED    = 2;//到时候改到别的引脚
long lastReconnectAttempt = 0;


// ***********************车驱动*************************


const int pin26=26; //设置驱动脚
const int pin25=25;
const int pin33=33;
const int pin32=32;


int speed=255;//车速度
String state="s";//车的状态
//控制单元
void Go(){


  state="t";
  ledcWrite(1,0);
  ledcWrite(2,speed);


  ledcWrite(3,0);
  ledcWrite(4,speed);


}//前进
void back(){
  state="b";
  ledcWrite(1,speed);
  ledcWrite(2,0);


  ledcWrite(3,speed);
  ledcWrite(4,0);
}//后退
void left(){
  state="l";
  ledcWrite(1,0);
  ledcWrite(2,speed);


  ledcWrite(3,0);
  ledcWrite(4,0);


}//左
void right(){
  state="r";
  //发布一个向右
  ledcWrite(1,0);
  ledcWrite(2,0);


  ledcWrite(3,0);
  ledcWrite(4,speed);


}//右
void stop(){
  state="s";
  ledcWrite(1,0);
  ledcWrite(2,0);


  ledcWrite(3,0);
  ledcWrite(4,0);


}//停


//客户端变量
WiFiClient espClient;
PubSubClient client(espClient);
//闪灯次数
void re_signal(int fq){
  int i = 0;
  for(i;i<fq;i++){
        digitalWrite(LED, HIGH);  
        delay(20);                      
        digitalWrite(LED, LOW);  
        delay(20);    
        digitalWrite(LED, HIGH);  
        delay(20);                      
        digitalWrite(LED, LOW);  
        delay(20);          
  }
                     
}
void setup() {
    //车驱动
    ledcSetup(1,50,8);
    ledcAttachPin(pin25,1);
    ledcSetup(2,50,8);
    ledcAttachPin(pin26,2);
   
    ledcSetup(3,50,8);
    ledcAttachPin(pin32,3);
    ledcSetup(4,50,8);
    ledcAttachPin(pin33,4);
    //车灯      
    pinMode(LED, OUTPUT);
    //软串口波特率      
    Serial.begin(115200);
    lastReconnectAttempt = 0;
    // connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    re_signal(1);
    Serial.println("Connected to the WiFi network");
    //connecting to a mqtt broker 连接服务端
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);//回调
    while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id += String(WiFi.macAddress());
        Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public emqx mqtt broker connected");
        } else {
            Serial.println("failed with state ");
            Serial.println(client.state());//返回连接状态
            delay(2000);
        }
    }
    re_signal(3);
    // publish and subscribe
    client.publish(OutTopic, "小车车来喽!");
    re_signal(3);      
    client.subscribe(InTopic);
   
}
boolean reconnect() {
  if (client.connect("YSL", mqtt_username, mqtt_password)) {
    // Once connected, publish an announcement...
    client.publish("outTopic","小车车来喽!");
    // ... and resubscribe
    client.subscribe("inTopic");
  }
  return client.connected();
}


//回调函数(主题,内容,长度)
void callback(char *InTopic, byte *payload, unsigned int length) {
 Serial.print("Message arrived in topic: ");
 Serial.println(InTopic);
 Serial.print("Message:");
 for (int i = 0; i < length; i++) {
     Serial.print((char) payload[i]);        
 }  
 if (payload[0] == 'C'&payload[1]=='#') {  
      //Serial.print(payload[2]-48);出来的都是字符流        
    switch (payload[2]) {
        case  '1':
          digitalWrite(LED,1);  // 则点亮LED。
          client.publish(OutTopic, "亮");
          break;
        case  '0':
          digitalWrite(LED,0);  // 则点亮LED。
          client.publish(OutTopic, "咩");
          break;
        case  'L':
          left();  
          re_signal(1);                  
          client.publish(OutTopic, "左转");
          break;
        case  'R':
          right();
          re_signal(1);
          client.publish(OutTopic, "右转");
          break;
        case  'S':
          stop();
          re_signal(1);        
          client.publish(OutTopic, "停车");
          break;
        case  'B':
          back();
          re_signal(1);
          client.publish(OutTopic, "倒车");
          break;  
        case  'G':
          Go();
          re_signal(1);
          client.publish(OutTopic, "前进");
          break;    
        case  'V':
          client.publish(OutTopic, "速度这块容我开发!!!!");
          break;          
                 
        default:
          client.publish(OutTopic, "好家伙,新东西?");  
          break;                        
 }
}
 Serial.println();
 Serial.println("-----------------------");
}
 
void loop()
{
  if (!client.connected()) {
    long now = millis();
    if (now - lastReconnectAttempt > 5000) {
      lastReconnectAttempt = now;
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
      }
    }
  } else {
    // Client connected


    client.loop();
  }


}

esp32 wifi 只是 其中一个部分
air780e 插入手机卡 做第二阶段控制

posted @ 2023-02-07 18:46  Yang-blackSun  阅读(20)  评论(0编辑  收藏  举报  来源
1 3