NucleoL476RG+ESP8266 AT 指令互连入门

首先感谢:https://circuitdigest.com/microcontroller-projects/interfacing-esp8266-with-stm32f103c8-stm32-to-create-a-webserver,这个功能过了之后,离自主简单iot,比如什么发发温度啊,什么人脸识别现成模块啊,指纹打开现成模块啊,都会近很多。

然而照抄是不行的,尤其你买的ESP8266 还是一些淘宝的开发板。我买的模块地址:https://detail.tmall.com/item.htm?id=598864159632&spm=a1z09.2.0.0.441b2e8dVpudrP&_u=j2mjstr31f7e,有个坑是,这货的RX,TX管脚容差是5V,而非3.3V。坑都说完了,接下来过一下炮制流程。

首先ESP8266 刷espressif 的 non-os sdk,其实最新的RTOS SDK那套应该也是没问题的,无非就是有个串口传AT指令,AT指令都是大路货,至少本案例中。

接下来想说,用Arduino IDE作开发,真香。请加一个stm32 arduino的管脚mapping包,即可。https://github.com/stm32duino/Arduino_Core_STM32/blob/42efbeb48728748965ce50df1a71e0db259d37da/variants/NUCLEO_L476RG/PeripheralPins.c

这里有个找usart serial的过程,其实花了挺久理解的,此处只给wiring答案。真的在解决问题的过程中,其实需要好好看一下nucleoL476RG的pinout图,这里就不贴了。

nucleo 5v + 地线,这里不多说。主要是Serial3的位置,分别是PC_4和PC_5,管脚图+上面的github mapping是说的清清楚楚的。

通篇代码也贴一下,


HardwareSerial Serial3(PC5, PC4);

String webpage="";                                   //String variable to store characters
int i=0,k=0,x=0;                                         //integer variables
String readString;                                   //using readString feature to read characters                       

boolean No_IP=false;                                 //boolean variables 
String IP="";                                         //String variable to store data
char temp1='0';                                      //character variable

String name="<p>Circuit Digest</p><p>A community of electrical and electronics students, engineers and makers</p>";   //String with html notations
String data="<p>Data Received Successfully.....</p>";     //String with html 


void check4IP(int t1)                                     //A function to check ip of ESP8266 
{
  int t2=millis();
  while(t2+t1>millis())
  {
    while(Serial3.available()>0)
    {
      if(Serial3.find("WIFI GOT IP"))
      {
        No_IP=true;
      }
    }
  }
}

void get_ip()                                           //After cheacking ip ,this is a function to get IP address
{
  IP="";
  char ch=0;
  while(1)
  {
    Serial3.println("AT+CIFSR");                   //GET IP AT COMMAND
    while(Serial3.available()>0)        
    {
      if(Serial3.find("STAIP,"))                   //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266
      {
        delay(1000);
        Serial.print("IP Address:");
        while(Serial3.available()>0)
        {
          ch=Serial3.read();                      //Serial2 reads from ESP8266
          if(ch=='+')
          break;
          IP+=ch;
        }
      }
      if(ch=='+')
      break;
    }
    if(ch=='+')
    break;
    delay(1000);
  }
  Serial.print(IP);                                //prints IP address in Serial monitor
  Serial.print("Port:");
  Serial.println(80);
}

void connect_wifi(String cmd, int t)                  //This function is for connecting ESP8266 with wifi network by using AT commands
{
  int temp=0,i=0;
  while(1)
  {
    Serial.println(cmd);                  //Sends to serial monitor
    Serial3.println(cmd);                 //sends to ESP8266 via serial communication
    while(Serial3.available())
    {
      if(Serial3.find("OK"))
      i=8;
    }
    delay(t);
    if(i>5)
    break;
    i++;
  }
  if(i==8)
  Serial.println("OK");
  else
  Serial.println("Error");
}

void wifi_init()                                //This function contains AT commands that passes to connect_wifi()
{
      connect_wifi("AT",3000);                   //Sends AT command with time(Command for Achknowledgement)
      connect_wifi("AT+CWMODE=3",1500);          //Sends AT command with time (For setting mode of Wifi)
      connect_wifi("AT+CWQAP",1000);            //Sends AT command with time (for Quit AP)
      connect_wifi("AT+RST",5000);             //Sends AT command with time (For RESETTING WIFI)
      check4IP(5000);
      if(!No_IP)
      {
        
        Serial.println("Connecting Wifi....");
        connect_wifi("AT+CWJAP=\"open_wrt\",\"bbb\"",7000);         //provide your WiFi username and password here
   
      }
      else
        {
        }
      Serial.println("Wifi Connected"); 
      get_ip();
      
      connect_wifi("AT+CIPMUX=1",100);                          //Sends AT command with time (For creating multiple connections)
      connect_wifi("AT+CIPSERVER=1,80",100);                    //Sends AT command with time (For setting up server with port 80)
}

void sendwebdata(String webPage)                          //This function is used to send webpage datas to the localserver
{
    int ii=0;
     while(1)
     {
      unsigned int l=webPage.length();
      Serial.print("AT+CIPSEND=0,");
      Serial3.print("AT+CIPSEND=0,");
      Serial.println(l+2);
      Serial3.println(l+2);
      delay(100);
      Serial.println(webPage);                        //sends webpage data to serial monitor
      Serial3.println(webPage);                       //sends webpage data to Serial2 ESP8266
      while(Serial3.available())
      {
       
        if(Serial3.find("OK"))
        {
          ii=11;
          break;
        }
      }
      if(ii==11)
      break;
      delay(100);
     }
}

void setup() 
{
   Serial.begin(115200);                //begins serial monitor with baud rate 9600
   Serial3.begin(115200);               //begins serial communication with esp8266 with baud rate 9600 (Change according to your esp8266 module)
   wifi_init();
   Serial.println("System Ready..");
}

void loop() 
{
  k=0;
  Serial.println("Please Refresh your Page");
  while(k<1000)
  {
    k++;
   while(Serial3.available())
   {
    if(Serial3.find("0,CONNECT"))
    {
      Serial.println("Start Printing");
      Send();
      Serial.println("Done Printing");
      delay(1000);
    }
  }
  delay(1);
 }
}

void Send()                                        //This function contains data to be sent to local server
{
      webpage = "<h1>Welcome to Circuit Digest</h1><body bgcolor=f0f0f0>";
      sendwebdata(webpage);
      webpage=name;
      sendwebdata(webpage);
      delay(1000);
      webpage = "<a href=\"http://circuitdigest.com/\"";
      webpage+="\">Click Here to get into circuitdigest.com</a>";
      webpage+=data;
      sendwebdata(webpage);
      Serial3.println("AT+CIPCLOSE=0");                  //Closes the server connection
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值