ESP8266 connection to a Arduino Nano

烈酒焚心 提交于 2020-02-03 05:48:05

问题


I am trying to connect a WiFi module (ESP8266) to a "funduino" development board (Arduino Nano) but I have no success. Since I tried so much schematics I've found on the internet about the connection between them two, I kindly ask here if is anyone who succeed in "pairing" this two devices. I am asking for the schematic and a functional source code.

Regards


回答1:


So you have multiple ways to pair those two devices. The best and most reliable is a serial connection which consists of 2 wires, RX and TX which are flipped(RX of one board to TX of other, and TX ->RX). The reason for this is very simple, you have two unidirectional communication channel. On first channel, the first board is listening while the other one is speaking. On the second one, the first board is speaking while the other one is listening.

The circuit:

  • RX is digital pin 10 (connect to digital pin 11 TX of other device)

  • TX is digital pin 11 (connect to digital pin 10 RX of other device)

  • GND to GND of the other board

After uploading the code on both boards and opening serial monitor on both devices, you should be able to communicate between boards via serial monitor.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}



回答2:


The ESP-01 by default comes with nonOS SDK bootloader that communicated via AT commands, you can find the complete command set from Expressif here. This is designed for an MCU (like Arduino Nano) to use it purely as an WiFi module rather than using it as a stand-alone MCU (for which it will require NodeMCU SDK).

If you ever upload an Arduino sketch up to the ESP-01, it will erase the AT Command firmware.

Assuming your ESP-01 is still having the AT Command firmware. What @Ben provided is a sketch that allows you to type AT commands via the Serial Monitor to internact with the ESP-01, it is manual, and good for testing if ESP-01 is working (you type AT and press return on Serial Monitor, the ESP-01 will ack with Ok) but not practical as a real application. The minimum commands required to established an WiFi connection with ESP-01 is listed below.

AT+CIPMUX=1 - Enable single (0) or multiple connection (1) to the web server.
              Multiple connection is a good option if you are repeatedly sending 
              out or reading data from the Internet.

AT+CWMODE=3 - Set WiFi mode: 1 is station mode (ESP8266 is client), 2 is AP mode 
              (ESP8266 acts like a WiFi router where your phone or PC can connect), 
              3 is AP+station mode (make the ESP8266 do both)

AT+CWJAP=“<your-ssid>”,”<your-pw>” - Connect to your WiFi. Provide your SSID name 
                                     and password inside the double qoutes.

AT+CIFSR - This returns the IP address of the module, indicating that it has 
           successfully connected to your WiFi router.

Once the WiFi connection is established, you can further communicate with the ESP-01 via the connection, like accessing a website for example:

AT+CIPSTART=0,"TCP", "www.example.com","80” - Start TCP or UDP connection. The 
                                              0 is the id of the connection.

AT+CIPSEND=0,16 -   Command to tell the module data is ready to be sent. 0 is the 
                    connection id, and 16 is the length of the data to be sent.
                    After this command, the ESP8266 will reply with the “>” 
                    character to tell us that it will be waiting for the data to be 
                    sent. If successful, the module will reply with “SEND OK”

GET / HTTP/1.1 - Send the http header, and other data, etc...

You can write your own sketch to automate those AT commands for interacting with with ESP-01 once you understand the AT commands required for establish a WiFi connection.

Here are two resources that I personally found extremely useful for doing more than connecting to WiFi.

STM32-ESP-01 Web Server - although this is for interfacing with STM32, the main difference is the pin assignment, so you should be able to port to Arduino easily.

MQTT via ESP-01

As for hardware interface, please noted that what @Ben provided is correct in principle, but you need to be aware that the ESP-01(ESP8266 to be precise) is a 3V3 MCU, so the connection is depended on what kind of host board you are using. If you are using Arduino Uno/Nano, both are having a 5V MCU, you will need a voltage divider (two resistors to drop the voltage to 3v3 before connecting to ESP-01) or a level shifter chip at least for the ESP-01 Rx pin to avoid the potential damage to the ESP-01.



来源:https://stackoverflow.com/questions/59738680/esp8266-connection-to-a-arduino-nano

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!