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

时间:2017-12-21 06:43:03

标签: 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;
    }
  }
}

执行时,串口监视器显示:

  

OKARFaC⸮C⸮j⸮H⸮AT+ AWJAP =&#34; RPi&#34;,#raspberry&#34; WIFI断开WIFI   连接的WIFI GOT IP

     

OK AT + CIFSR

     
    

+ CIFSR:STAIP,&#34; 192.168.50.13&#34;     + CIFQR:STAMAC,&#34;图2c:图3a:吃+ CIPSTART = 4&#34; TCP&#34;&#34; 192.0n8.50.1&#34;,121升

  
     

链接类型错误

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)

我也提到this SO thread没有运气。

1 个答案:

答案 0 :(得分:1)

一些假设,因为您没有提供以下信息:
Arduino IDE> = 1.85
ESP8266社区软件包> = 2.41

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

如果是这种情况,并且这些片段(用 X X 括起来)不是错别字

  

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

这需要检查以下几点

  • 硬件连接器-arduino和esp模块之间的串行连接器
  • Eli模块的
  • 稳定电源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];
  • 代码处理
 nextPing = millis() + 10000;

末尾

 if (in.hasData) {

可能会导致交流中断
由于代码处理的原因,这可能会在不需要的地方触发