5
votes

I have a Raspberry Pi working as a WiFi hotspot and an Arduino Uno trying to get data from it using an ESP8266 module.

This is my receiver code for 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;
    }
  }
}

When I execute, the serial monitor shows:

OK ARFa C⸮C⸮j⸮H⸮AT+AWJAP="RPi",#raspberry" WIFI DISCONNECT WIFI CONNECVED 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

Link type ERROR

Raspberry Pi's ISC DHCP server:

wlan0: STA 2c:3a:e8:4e:bf:70 RADIUS: starting accounting session 5A3B2C85-000000E9 wlan0: STA 2c:3a:e8:4e:bf:70 WPA: pairwise key handshake completed (RSN)

I also referred to this SO thread with no luck.

1

1 Answers

1
votes

Some assumptions because you have not given the info:
Arduino IDE >=1.85
ESP8266 Community package >=2.41

ESP Module ESP8266-12E with latest AT firmware

if that is the case and these fragments (enclosed by X X) are no typos

+CIFQR:STAMAC,"2c:3a:eXAT+CIPSTART=4,"TCP","192.0XnX8.50.1",121l

this leaves the following points to check

  • hardware connectors - serial connectors between arduino and esp module
  • stable power source 3.3V for the ESP module
    Sure this is ok - but just in case and as reference for other readers
  • serial speed - try to increase from 9600 to 57600 or even to 115200 baud
  • These pieces of code should be in the setup() and not in the 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];
  • Code processing
 nextPing = millis() + 10000;

at the end of

 if (in.hasData) {

might lead to interuptions in the communication
Reason due to the processing of code this might trigger at an unwanted point