使用ESP8266 / Arduino Uno从服务器接收数据

Sac*_*ith 5 automation arduino-uno esp8266 arduino-esp8266

我有一个Raspberry Pi作为WiFi热点,一个Arduino Uno试图使用ESP8266模块从中获取数据。

这是我的Arduino接收器代码:

#include <SoftwareSerial.h>
#include <SerialESP8266wifi.h>

#define sw_serial_rx_pin 4 //  Connect this pin to TX on the esp8266
#define sw_serial_tx_pin 6 //  Connect this pin to RX on the esp8266
#define esp8266_reset_pin 5 // Connect this pin to CH_PD on the esp8266, not reset. (let reset be unconnected)

SoftwareSerial swSerial(sw_serial_rx_pin, sw_serial_tx_pin);

// the last parameter sets the local echo option for the ESP8266 module..
SerialESP8266wifi wifi(swSerial, swSerial, esp8266_reset_pin, Serial);//adding Serial enabled local echo and wifi debug

String inputString;
boolean stringComplete = false;
unsigned long nextPing = 0;

void setup() {
  inputString.reserve(20);
  swSerial.begin(9600);
  Serial.begin(9600);
  while (!Serial)
    ;
  Serial.println("Starting wifi");

  wifi.setTransportToTCP();// this is also default
  // wifi.setTransportToUDP();//Will use UDP when connecting to server, default is TCP

  wifi.endSendWithNewline(true); // Will end all transmissions with a newline and carriage return ie println.. default is true
  wifi.begin();
  wifi.connectToAP("RPi", "raspberry");
  wifi.connectToServer("192.168.50.1", "1234");
  wifi.send(SERVER, "ESP8266 test app started");
}

void loop() {
  //Make sure the esp8266 is started..
  if (!wifi.isStarted())
    wifi.begin();

  //Send what you typed in the arduino console to the server
  static char buf[20];
  if (stringComplete) {
    inputString.toCharArray(buf, sizeof buf);
    wifi.send(SERVER, buf);
    inputString = "";
    stringComplete = false;
  }

  //Send a ping once in a while..
  if (millis() > nextPing) {
    wifi.send(SERVER, "Ping ping..");
    nextPing = millis() + 10000;
  }

  //Listen for incoming messages and echo back, will wait until a message is received, or max 6000ms..
  WifiMessage in = wifi.listenForIncomingMessage(6000);
  if (in.hasData) {
    if (in.channel == SERVER)
      Serial.println("Message from the server:");
    else
      Serial.println("Message a local client:");
    Serial.println(in.message);
    //Echo back;
    wifi.send(in.channel, "Echo:", false);
    wifi.send(in.channel, in.message);
    nextPing = millis() + 10000;
  }

  //If you want do disconnect from the server use:
  // wifi.disconnectFromServer();
}

//Listen for serial input from the console
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我执行时,串行监视器显示:

OK ARFa C?C?j?H?AT + AWJAP =“ RPi”,#raspberry“ WIFI断开WIFI连接WIFI GOT IP

OK AT + CIFSR

+ CIFSR:STAIP,“ 192.168.50.13” + CIFQR:STAMAC,“ 2c:3a:eAT + CIPSTART = 4,” TCP“,” 192.0n8.50.1“,121l

链接类型错误

Raspberry Pi的ISC DHCP服务器:

wlan0:STA 2c:3a:e8:4e:bf:70 RADIUS:开始计费会话5A3B2C85-000000E9 wlan0:STA 2c:3a:e8:4e:bf:70 WPA:双向密钥握手已完成(RSN)

我也提到这个SO线程没有运气。

Cod*_*007 1

一些假设,因为您没有给出信息:
Arduino IDE >=1.85
ESP8266 Community package >=2.41

ESP 模块 ESP8266-12E 具有最新的 AT 固件

如果是这种情况并且这些片段(由XX括起来)没有拼写错误

+CIFQR:STAMAC,"2c:3a:e X AT+CIPSTART=4,"TCP","192.0 X n X 8.50.1",121l

这留下了以下几点需要检查

  • 硬件连接器 - arduino 和 esp 模块之间的串行连接器
  • ESP 模块的稳定电源 3.3V
    当然可以 - 但以防万一并作为其他读者的参考
  • 串行速度 - 尝试从 9600 增加到 57600 甚至 115200 波特
  • 这些代码应该在setup()中而不是在loop()中

//Make sure the esp8266 is started..
if (!wifi.isStarted())
wifi.begin();

//Send what you typed in the arduino console to the server
static char buf[20];
Run Code Online (Sandbox Code Playgroud)
  • 代码处理
 nextPing = millis() + 10000;
Run Code Online (Sandbox Code Playgroud)

在......的最后

 if (in.hasData) {
Run Code Online (Sandbox Code Playgroud)

可能会导致通信中断
原因是代码处理可能会在不需要的点触发