M6E Nano with Redboard and ESP8266 wifi shield

All things pertaining to wireless and RF links

Moderator: phalanx

Post Reply
lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Mon Dec 18, 2017 1:22 pm

Hi,

This ought to be a challenge...

Im trying to have the Redboard (arduino) work with ESP8266 wifi shield and the M6E Nano rfid reader.
The thought is the following:
1. M6E Nano reads a rfid sticker
2. The program sends a ping to a server with the EPC through the wifi shield.

I have taken the examples (provided by sparkfun) for both shields and combined them into the code below.
It works for a little while - i can see it being sent up to the server. The reading of the rfid works continuously, but it stops pinging the server unless there is a constant flow of readings. Ie. i read the tag - it works - then wait a minute and read again -> the rfid will read it but it wont ping the server.

What am I doing wrong in my code?

Code: Select all

#include <SoftwareSerial.h> //Used for transmitting to the device

SoftwareSerial softSerial(2, 3); //RX, TX

#include "SparkFun_UHF_RFID_Reader.h" //Library for controlling the M6E Nano module
RFID nano; //Create instance

#define BUZZER1 10
//#define BUZZER1 0 //For testing silently
#define BUZZER2 9

boolean tagDetected; //Keeps track of when we've beeped


#include <SparkFunESP8266WiFi.h>
const char mySSID[] = "mywifi";
const char myPSK[] = "mywifipass";
//ESP8266Server server = ESP8266Server(80);
const char destServer[] = "mydomain.se";
//const String httpRequest = "GET / HTTP/1.1\n"
  //                         "Host: mydomain.se\n"
    //                       "Connection: close\n\n";

// To use the ESP8266 as a TCP client, use the 
// ESP8266Client class. First, create an object:
ESP8266Client client;
void setup()
{
  Serial.begin(115200);
  initializeESP8266();
  connectESP8266();
  displayConnectInfo();

 
  // ESP8266Client connect([server], [port]) is used to 
  // connect to a server (const char * or IPAddress) on
  // a specified port.
  // Returns: 1 on success, 2 on already connected,
  // negative on fail (-1=TIMEOUT, -3=FAIL).
  //int retVal = client.connect(destServer, 80);
  //if (retVal <= 0)
  //{
   // Serial.println(F("Failed to connect to server."));
   // return;
  //}
int retVal;
  do {
    delay(50);
    retVal = client.connect(destServer, 80);
  } while (retVal <=0);

  while (!Serial); //Wait for the serial port to come online

  if (setupNano(38400) == false) //Configure nano to run at 38400bps
  {
    Serial.println(F("Module failed to respond. Please check wiring."));
    while (1); //Freeze!
  }

  nano.setRegion(REGION_OPEN); //Set to North America

  nano.setReadPower(500); //5.00 dBm. Higher values may caues USB port to brown out
  //Max Read TX Power is 27.00 dBm and may cause temperature-limit throttling

  Serial.println(F("Press a key to begin scanning for tags."));
  while (!Serial.available()); //Wait for user to send a character
  Serial.read(); //Throw away the user's character

  
  nano.startReading(); //Begin scanning for tags
}

void loop()
{
  if (nano.check() == true) //Check to see if any new data has come in from module
  {
    byte responseType = nano.parseResponse(); //Break response into tag ID, RSSI, frequency, and timestamp

    if (responseType == RESPONSE_IS_KEEPALIVE)
    {
      Serial.println(F("Scanning"));
      tagDetected = false;
      //lowBeep();
    }
    else if (responseType == RESPONSE_IS_TAGFOUND)
    {
      //If we have a full record we can pull out the fun bits
      int rssi = nano.getTagRSSI(); //Get the RSSI for this tag read

      long freq = nano.getTagFreq(); //Get the frequency this tag was detected at

      long timeStamp = nano.getTagTimestamp(); //Get the time this was read, (ms) since last keep-alive message

      byte tagEPCBytes = nano.getTagEPCBytes(); //Get the number of bytes of EPC from response

      Serial.print(F(" rssi["));
      Serial.print(rssi);
      Serial.print(F("]"));

      Serial.print(F(" freq["));
      Serial.print(freq);
      Serial.print(F("]"));

      Serial.print(F(" time["));
      Serial.print(timeStamp);
      Serial.print(F("]"));

      //Print EPC bytes, this is a subsection of bytes from the response/msg array
      Serial.print(F(" epc["));
      String test;
      for (byte x = 0 ; x < tagEPCBytes ; x++)
      {
        if (nano.msg[31 + x] < 0x10) Serial.print(F("0")); //Pretty print
        Serial.print(nano.msg[31 + x], HEX);
        Serial.print(F(" "));
        test = test + (nano.msg[31+x]);
      }
      Serial.print(F("]"));
      Serial.println();
      Serial.print(test);

      Serial.println();
      clientDemo(String(test));

      if (tagDetected == false) //Beep if we've detected a new tag
      {
        tagDetected = true;
        highBeep();
      }
      
    }
    else if (responseType == ERROR_CORRUPT_RESPONSE)
    {
      Serial.println("Bad CRC");
      tagDetected = false;
      //lowBeep();
    }
    else
    {
      //Unknown response
      Serial.print("Unknown error");
      tagDetected = false;
      //lowBeep();
    }
  }
}

//Gracefully handles a reader that is already configured and already reading continuously
//Because Stream does not have a .begin() we have to do this outside the library
boolean setupNano(long baudRate)
{
  nano.begin(softSerial); //Tell the library to communicate over software serial port

  //Test to see if we are already connected to a module
  //This would be the case if the Arduino has been reprogrammed and the module has stayed powered
  softSerial.begin(baudRate); //For this test, assume module is already at our desired baud rate
  while(!softSerial); //Wait for port to open

  //About 200ms from power on the module will send its firmware version at 115200. We need to ignore this.
  while(softSerial.available()) softSerial.read();
  
  nano.getVersion();

  if (nano.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
  {
    //This happens if the baud rate is correct but the module is doing a ccontinuous read
    nano.stopReading();

    Serial.println(F("Module continuously reading. Asking it to stop..."));

    delay(1500);
  }
  else
  {
    //The module did not respond so assume it's just been powered on and communicating at 115200bps
    softSerial.begin(115200); //Start software serial at 115200

    nano.setBaud(baudRate); //Tell the module to go to the chosen baud rate. Ignore the response msg

    softSerial.begin(baudRate); //Start the software serial port, this time at user's chosen baud rate
  }

  //Test the connection
  nano.getVersion();
  if (nano.msg[0] != ALL_GOOD) return (false); //Something is not right

  //The M6E has these settings no matter what
  nano.setTagProtocol(); //Set protocol to GEN2

  nano.setAntennaPort(); //Set TX/RX antenna ports to 1

  return (true); //We are ready to rock
}

void initializeESP8266()
{
  // esp8266.begin() verifies that the ESP8266 is operational
  // and sets it up for the rest of the sketch.
  // It returns either true or false -- indicating whether
  // communication was successul or not.
  // true
  int test = esp8266.begin();
  if (test != true)
  {
    Serial.println(F("Error talking to ESP8266."));
    errorLoop(test);
  }
  Serial.println(F("ESP8266 Shield Present"));
}

void connectESP8266()
{
  // The ESP8266 can be set to one of three modes:
  //  1 - ESP8266_MODE_STA - Station only
  //  2 - ESP8266_MODE_AP - Access point only
  //  3 - ESP8266_MODE_STAAP - Station/AP combo
  // Use esp8266.getMode() to check which mode it's in:
  int retVal = esp8266.getMode();
  if (retVal != ESP8266_MODE_STA)
  { // If it's not in station mode.
    // Use esp8266.setMode([mode]) to set it to a specified
    // mode.
    retVal = esp8266.setMode(ESP8266_MODE_STA);
    if (retVal < 0)
    {
      Serial.println(F("Error setting mode."));
      errorLoop(retVal);
    }
  }
  Serial.println(F("Mode set to station"));

  // esp8266.status() indicates the ESP8266's WiFi connect
  // status.
  // A return value of 1 indicates the device is already
  // connected. 0 indicates disconnected. (Negative values
  // equate to communication errors.)
  retVal = esp8266.status();
  if (retVal <= 0)
  {
    Serial.print(F("Connecting to "));
    Serial.println(mySSID);
    // esp8266.connect([ssid], [psk]) connects the ESP8266
    // to a network.
    // On success the connect function returns a value >0
    // On fail, the function will either return:
    //  -1: TIMEOUT - The library has a set 30s timeout
    //  -3: FAIL - Couldn't connect to network.
    retVal = esp8266.connect(mySSID, myPSK);
    if (retVal < 0)
    {
      Serial.println(F("Error connecting"));
      errorLoop(retVal);
    }
  }
}

void displayConnectInfo()
{
  char connectedSSID[24];
  memset(connectedSSID, 0, 24);
  // esp8266.getAP() can be used to check which AP the
  // ESP8266 is connected to. It returns an error code.
  // The connected AP is returned by reference as a parameter.
  int retVal = esp8266.getAP(connectedSSID);
  if (retVal > 0)
  {
    Serial.print(F("Connected to: "));
    Serial.println(connectedSSID);
  }

  // esp8266.localIP returns an IPAddress variable with the
  // ESP8266's current local IP address.
  IPAddress myIP = esp8266.localIP();
  Serial.print(F("My IP: ")); Serial.println(myIP);
}
// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
  Serial.print(F("Error: ")); Serial.println(error);
  Serial.println(F("Looping forever."));
  for (;;)
    ;
}

void clientDemo(String id)
{
 /* if (client.connected() == 0){
    client.connect(destServer, 80);
    Serial.write("Reconnected");
  }*/

  // print and write can be used to send data to a connected
  // client connection.
  String httpRequest = "GET /rfid.php/?id="+id+" HTTP/1.1\n"
                           "Host: mydomain.se\n"
                           "Connection: closed\n\n";
  client.print(httpRequest);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available()){
    Serial.write(client.read()); // read() gets the FIFO char
  }

  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
}

// serialTrigger prints a message, then waits for something
// to come in from the serial port.
void serialTrigger(String message)
{
  Serial.println();
  Serial.println(message);
  Serial.println();
  while (!Serial.available())
    ;
  while (Serial.available())
    Serial.read();
}

void lowBeep()
{
  tone(BUZZER1, 130, 150); //Low C
  //delay(150);
}

void highBeep()
{
  tone(BUZZER1, 2093, 150); //High C
  //delay(150);
}

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Tue Dec 19, 2017 6:07 am

Is the check at the end of ClientDemo correct? It checks that you are connected and if so you close the connection. Shouldn't that read in case NOT connected with an ! before : if (!client.connected()).....

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Tue Dec 19, 2017 11:48 am

paulvha wrote:
Tue Dec 19, 2017 6:07 am
Is the check at the end of ClientDemo correct? It checks that you are connected and if so you close the connection. Shouldn't that read in case NOT connected with an ! before : if (!client.connected()).....
I tried what you said with if (!client.connected()), but it didn't work.
Thing is that the code says right above it that:

Code: Select all

  
  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
...and in C -1 is also true...
Either way, i manage to get a few pings in the beginning, but then the connection either closes, or something else happens at it stops working. I mean, it could work many times, until i take a 30 second break, and then after that it wont work again.

Anybody has any clue?

Code: Select all

void clientDemo(String id)
{
 /* if (client.connected() == 0){
    client.connect(destServer, 80);
    Serial.write("Reconnected");
  }*/

  // print and write can be used to send data to a connected
  // client connection.
  String httpRequest = "GET /rfid.php/?id="+id+" HTTP/1.1\n"
                           "Host: mydomain.se\n"
                           "Connection: closed\n\n";
  client.print(httpRequest);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available()){
    Serial.write(client.read()); // read() gets the FIFO char
  }

  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
 [b] if (client.connected())[/b]
    client.stop(); // stop() closes a TCP connection.
}

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Tue Dec 19, 2017 12:40 pm

The definition for client.connected return is : Returns true if the client is connected, false if not. So if you are connected, the next step is to disconnect (client.stop()). IF you are not connected, you are already disconnected. BUT once you disconnect however, you do not connect again within the loop. (only in setup the connection activity is happening ) Why do you check on client.connected as the next step is to close the connection without the chance to reconnect again?? (the connection is commented out at the beginning of the Clientdemo() routine.)

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Tue Dec 19, 2017 12:52 pm

paulvha wrote:
Tue Dec 19, 2017 12:40 pm
The definition for client.connected return is : Returns true if the client is connected, false if not. So if you are connected, the next step is to disconnect (client.stop()). IF you are not connected, you are already disconnected. BUT once you disconnect however, you do not connect again within the loop. (only in setup the connection activity is happening ) Why do you check on client.connected as the next step is to close the connection without the chance to reconnect again?? (the connection is commented out at the beginning of the Clientdemo() routine.)
I know, i added it there, but it didnt help. Just slowed everything down...
i tried Serial.writing every time i am connected and the thing is that it actually works the few times in the beginning also when it is not connected. It never outputted connected for me - but maybe that is because it is in the setup().
What do you suggest that i do?
I tried outting the whole client instance (the one above the setup) inside the clientdemo() but that didnt work at all...

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Tue Dec 19, 2017 1:48 pm

I am not sure what you try to do in this ClientDemo(). The connection as as a station/client. I would expect you try to write the found EPC number to a server. After sending the GET command, you are reading/printing the response. Not sure what you are getting and what is printed as that is depending on the server code, BUT after sending the response by the server the instruction as part of GET : Connection: close(d) will close the connection once all has been sent (if anything). The next step is to check whether it is closed..and then close the connection on the client. However that connection is not opened again for later communication as part of the loop() and it's subroutines. The connection is only established as part of setup. So you should not include the instruction Connection: close(d) and do not check on close, OR re-establish the connection each time. I would opt for the first option...do NOT close...

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Wed Dec 20, 2017 7:52 am

interestingly,not stopping did not help it.
As long as i keep scanning a tag with 3-6 seconds in between it works and pings the server (sends the requests), but as soon as I wait around 20 seconds it looses itself and stops working...

Here is what i did:

Code: Select all

void clientDemo(String id)
{
  if (client.connected() == 0){
    //client.connect(destServer, 80);
    Serial.write("disconnected");
  }

  
  // print and write can be used to send data to a connected
  // client connection.
  String httpRequest = "GET /rfid.php/?id="+id+" HTTP/1.1\n"
                           "Host: smspower.se\n"
                           "User-Agent: Lorkanss/Arduino\n"
                           "Connection: closed\n\n";
  client.print(httpRequest);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available()){
    Serial.write(client.read()); // read() gets the FIFO char
  }

  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
 /* if (client.connected()){
    client.stop(); // stop() closes a TCP connection.
    Serial.write("Client stopped");
    }*/
}
Also note that it outputs disconnected ALL THE TIME. even on first scan...

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Wed Dec 20, 2017 9:35 am

Hi all,

I restructured the code to the following. The function that pings the server should be standalone and not global like before.
I now think that the problem has to do with the Softwareserial library, and how the M6E Nano changes the baudrate to 38400 in its setup, while the wifi shield prefers 9600 or 115200...
Is there anyway to divide this up? so i have one instance of software serial for each?

My setup is that i have male headers between the shields and just stacked the wifi on top of the arduino (redboard) and the M6E nano on top of the wifi.

Code: Select all

/************************************************************
The code is a mashup between the two sparkfun examples:
Shield Demo (ESP8266 Wifi shield)
Constant read (M6E Nano)
************************************************************/

//////////////////////
// Library Includes //
//////////////////////
// SoftwareSerial is required (even you don't intend on
// using it).
//#include <SoftwareSerial.h> 
#include <SparkFunESP8266WiFi.h>

/////////////////////////////////////
//Nano business:
#include <SoftwareSerial.h> //Used for transmitting to the device
SoftwareSerial softSerial(2, 3); //RX, TX
#include "SparkFun_UHF_RFID_Reader.h" //Library for controlling the M6E Nano module
RFID nano; //Create instance
//////////////////////////////

//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "sisisi7";
const char myPSK[] = "0543948110";


//////////////////
// HTTP Strings //
//////////////////
const char destServer[] = "smspower.se";

const String httpRequest = "GET / HTTP/1.1\n"
                           "Host: smspower.se\n"
                           "Connection: close\n\n";

// All functions called from setup() are defined below the
// loop() function. They modularized to make it easier to
// copy/paste into sketches of your own.
void setup() 
{
  // Serial Monitor is used to control the demo and view
  // debug information.
  Serial.begin(115200);
  serialTrigger(F("Press any key to begin."));

  // initializeESP8266() verifies communication with the WiFi
  // shield, and sets it up.
  initializeESP8266();

  // connectESP8266() connects to the defined WiFi network.
  connectESP8266();

  // displayConnectInfo prints the Shield's local IP
  // and the network it's connected to.
  displayConnectInfo();

  ////////////////////////////////////
  //Nano business:
  while (!Serial); //Wait for the serial port to come online

if (setupNano(38400) == false) //Configure nano to run at 38400bps
  {
    Serial.println(F("Module failed to respond. Please check wiring."));
    while (1); //Freeze!
  }

  nano.setRegion(REGION_OPEN); //Set to North America

  nano.setReadPower(500); //5.00 dBm. Higher values may caues USB port to brown out
  //Max Read TX Power is 27.00 dBm and may cause temperature-limit throttling

 Serial.println(F("Press a key to begin scanning for tags."));
  while (!Serial.available()); //Wait for user to send a character
  Serial.read(); //Throw away the user's character

  nano.startReading(); //Begin scanning for tags
  //////////////////////////////////
}

void loop() 
{
  if (nano.check() == true) //Check to see if any new data has come in from module
  {
    byte responseType = nano.parseResponse(); //Break response into tag ID, RSSI, frequency, and timestamp

    if (responseType == RESPONSE_IS_KEEPALIVE)
    {
      Serial.println(F("Scanning"));
    }
    else if (responseType == RESPONSE_IS_TAGFOUND)
    {
      //If we have a full record we can pull out the fun bits
      int rssi = nano.getTagRSSI(); //Get the RSSI for this tag read

      long freq = nano.getTagFreq(); //Get the frequency this tag was detected at

      long timeStamp = nano.getTagTimestamp(); //Get the time this was read, (ms) since last keep-alive message

      byte tagEPCBytes = nano.getTagEPCBytes(); //Get the number of bytes of EPC from response

      Serial.print(F(" rssi["));
      Serial.print(rssi);
      Serial.print(F("]"));

      Serial.print(F(" freq["));
      Serial.print(freq);
      Serial.print(F("]"));

      Serial.print(F(" time["));
      Serial.print(timeStamp);
      Serial.print(F("]"));

      //Print EPC bytes, this is a subsection of bytes from the response/msg array
      Serial.print(F(" epc["));
      for (byte x = 0 ; x < tagEPCBytes ; x++)
      {
        if (nano.msg[31 + x] < 0x10) Serial.print(F("0")); //Pretty print
        Serial.print(nano.msg[31 + x], HEX);
        Serial.print(F(" "));
      }
      Serial.print(F("]"));

      Serial.println();

      clientDemo();
    }
    else if (responseType == ERROR_CORRUPT_RESPONSE)
    {
      Serial.println("Bad CRC");
    }
    else
    {
      //Unknown response
      Serial.print("Unknown error");
    }
  }

  //serialTrigger(F("Press any key to connect client."));
  //clientDemo();
  
}

//Gracefully handles a reader that is already configured and already reading continuously
//Because Stream does not have a .begin() we have to do this outside the library
boolean setupNano(long baudRate)
{
  nano.begin(softSerial); //Tell the library to communicate over software serial port

  //Test to see if we are already connected to a module
  //This would be the case if the Arduino has been reprogrammed and the module has stayed powered
  softSerial.begin(baudRate); //For this test, assume module is already at our desired baud rate
  while(!softSerial); //Wait for port to open

  //About 200ms from power on the module will send its firmware version at 115200. We need to ignore this.
  while(softSerial.available()) softSerial.read();
  
  nano.getVersion();

  if (nano.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
  {
    //This happens if the baud rate is correct but the module is doing a ccontinuous read
    nano.stopReading();

    Serial.println(F("Module continuously reading. Asking it to stop..."));

    delay(1500);
  }
  else
  {
    //The module did not respond so assume it's just been powered on and communicating at 115200bps
    softSerial.begin(115200); //Start software serial at 115200

    nano.setBaud(baudRate); //Tell the module to go to the chosen baud rate. Ignore the response msg

    softSerial.begin(baudRate); //Start the software serial port, this time at user's chosen baud rate
  }

  //Test the connection
  nano.getVersion();
  if (nano.msg[0] != ALL_GOOD) return (false); //Something is not right

  //The M6E has these settings no matter what
  nano.setTagProtocol(); //Set protocol to GEN2

  nano.setAntennaPort(); //Set TX/RX antenna ports to 1

  return (true); //We are ready to rock
}


void initializeESP8266()
{
  // esp8266.begin() verifies that the ESP8266 is operational
  // and sets it up for the rest of the sketch.
  // It returns either true or false -- indicating whether
  // communication was successul or not.
  // true
  int test = esp8266.begin();
  if (test != true)
  {
    Serial.println(F("Error talking to ESP8266."));
    errorLoop(test);
  }
  Serial.println(F("ESP8266 Shield Present"));
}

void connectESP8266()
{
  // The ESP8266 can be set to one of three modes:
  //  1 - ESP8266_MODE_STA - Station only
  //  2 - ESP8266_MODE_AP - Access point only
  //  3 - ESP8266_MODE_STAAP - Station/AP combo
  // Use esp8266.getMode() to check which mode it's in:
  int retVal = esp8266.getMode();
  if (retVal != ESP8266_MODE_STA)
  { // If it's not in station mode.
    // Use esp8266.setMode([mode]) to set it to a specified
    // mode.
    retVal = esp8266.setMode(ESP8266_MODE_STA);
    if (retVal < 0)
    {
      Serial.println(F("Error setting mode."));
      errorLoop(retVal);
    }
  }
  Serial.println(F("Mode set to station"));

  // esp8266.status() indicates the ESP8266's WiFi connect
  // status.
  // A return value of 1 indicates the device is already
  // connected. 0 indicates disconnected. (Negative values
  // equate to communication errors.)
  retVal = esp8266.status();
  if (retVal <= 0)
  {
    Serial.print(F("Connecting to "));
    Serial.println(mySSID);
    // esp8266.connect([ssid], [psk]) connects the ESP8266
    // to a network.
    // On success the connect function returns a value >0
    // On fail, the function will either return:
    //  -1: TIMEOUT - The library has a set 30s timeout
    //  -3: FAIL - Couldn't connect to network.
    retVal = esp8266.connect(mySSID, myPSK);
    if (retVal < 0)
    {
      Serial.println(F("Error connecting"));
      errorLoop(retVal);
    }
  }
}

void displayConnectInfo()
{
  char connectedSSID[24];
  memset(connectedSSID, 0, 24);
  // esp8266.getAP() can be used to check which AP the
  // ESP8266 is connected to. It returns an error code.
  // The connected AP is returned by reference as a parameter.
  int retVal = esp8266.getAP(connectedSSID);
  if (retVal > 0)
  {
    Serial.print(F("Connected to: "));
    Serial.println(connectedSSID);
  }

  // esp8266.localIP returns an IPAddress variable with the
  // ESP8266's current local IP address.
  IPAddress myIP = esp8266.localIP();
  Serial.print(F("My IP: ")); Serial.println(myIP);
}

void clientDemo()
{
  Serial.begin(115200);
  // To use the ESP8266 as a TCP client, use the 
  // ESP8266Client class. First, create an object:
  ESP8266Client client;

  // ESP8266Client connect([server], [port]) is used to 
  // connect to a server (const char * or IPAddress) on
  // a specified port.
  // Returns: 1 on success, 2 on already connected,
  // negative on fail (-1=TIMEOUT, -3=FAIL).
  int retVal = client.connect(destServer, 80);
  if (retVal <= 0)
  {
    Serial.println(F("Failed to connect to server. "));
    Serial.print(retVal);
    return;
  }

  // print and write can be used to send data to a connected
  // client connection.
  client.print(httpRequest);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available())
    Serial.write(client.read()); // read() gets the FIFO char
  
  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.

}


// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
  Serial.print(F("Error: ")); Serial.println(error);
  Serial.println(F("Looping forever."));
  for (;;)
    ;
}

// serialTrigger prints a message, then waits for something
// to come in from the serial port.
void serialTrigger(String message)
{
  Serial.println();
  Serial.println(message);
  Serial.println();
  while (!Serial.available())
    ;
  while (Serial.available())
    Serial.read();
}

To try my hypothesis about the baud rate, I commented out all of the code pertaining to the M6E nano. then already when adding the following the wifi didnt manage to connect to the server:

Code: Select all

if (setupNano(38400) == false) //Configure nano to run at 38400bps
  {
    Serial.println(F("Module failed to respond. Please check wiring."));
    while (1); //Freeze!
  }

I receive the "failed to connect to server" error and -1 (timeout) as error code...

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Wed Dec 20, 2017 9:46 am

weird indeed.. some ideas

Question where I do not know the answer. Are BOTH the ESP8266 shield and the M6E Nano redboard using softserial and could they be in each others way ?

Have you tried remove the "connection: closed" from the Get string ?

Another thought...: Could it be the server side closing the socket due to a time-out ?

My shield got lost ESP8266 some how, (can't find it, maybe lend it to someone). So I have ordered a new one that should arrive tomorrow. I will give a try as well,

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Wed Dec 20, 2017 10:14 am

Yes, both are using the softserial. the M6E nano seems to be using it more seriously, and the shield as more of a dependency. but not entirely sure. Both of them load this in as an example.

Tried this for fun, but didnt know how to proceed afterwards:

Code: Select all

#include <SoftwareSerial.h> //Used for transmitting to the device
SoftwareSerial softSerial(2, 3); //RX, TX
SoftwareSerial wifiSerial(8, 9); //RX, TX
Didnt help. I do think this issue is where the cookie crumbles. If we manage to solve this, then these two will be able to work well together. I dont mind that the rfid scanning pauses for the 1-3 seconds it takes to ping the server each time in case they cant work together...

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Wed Dec 20, 2017 10:28 am

paulvha wrote:
Wed Dec 20, 2017 9:46 am

Another thought...: Could it be the server side closing the socket due to a time-out ?
I checked in php.ini on the server...
default_socket_timeout = 60 ... but then i realized it has nothing to do with that kind of socked so i went to httpd.conf insted and found this:

Code: Select all

#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 60

#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 150

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 15
I think the KeepAliveTimeout is the key to why it timed out before. But it still doesnt solve the problem that it only lets me open one connection to the server. So it could be a workaround to add more seconds to that - but cant add endless of seconds...

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Wed Dec 20, 2017 10:59 am

Woohooh Good progress.... :-) what happens if you (next to extending this timeout) also remove the "connection: close" from the GET string ?

Softserial has it's challenges, especially if the processor gets loaded. It might loose/drop bytes if the baudrate is high.(which it is on the M6E/Nano by default) If you run into trouble and loose wireless/RFID information: 1) Try to switch the M6E to a lower baudrate (also sending that to the M6E) OR ...2) it seems that if nano.begin() is called without an interface defined it will try to use the (buffered) SERIAL/ UART connection by default. Then the challenge is that this is actually also used for the USB connection. You would NOT being able to use the USB connection (and power through the 5V separately), remove one line from the setup (waiting on Serial). At least you still get the RFID to the server..

lorkanss
Posts: 8
Joined: Mon Dec 18, 2017 1:11 pm

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by lorkanss » Fri Dec 22, 2017 5:00 am

Connection: Keep-Alive works, and it lets me use the 15s between each request to keep the connection alive.
but what it a ping is to come say 2 hours, or a day later? its not going to work in the long run... - also, that is with the old script further up. not with the never version of code im trying to make work...

i tried to have the M6E work without the serial software library. it didnt... not sure what you meant, but tried doing nano.begin().
then i tried setting the baudrate to 115200, but couldnt read what it said on serial monitor - and in general, it didnt work i think...

Did you manage to try things out on your end? thanks for the help so far btw!

paulvha
Posts: 289
Joined: Sat Nov 11, 2017 2:39 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by paulvha » Fri Dec 22, 2017 6:40 am

my shield has not arrived yet.. Christmas posts take longer.. I hope/expect today.

What you could do instead of is as part of the loop send a regular (empty) message. Also if you plan to wait (say 5 min) in between readings, you could make a loop that run 5 x 6 = 30 times {a ping & a sleep for 10 seconds}. if you want to wait an hour : 5 x 36 = 180 times that loop {a ping & a sleep for 10 seconds}.

You need the M6E library indeed. What I meant to say is that in the library in the file SparkFun_UHF_RFID_Reader.h, (line 78) it defines that IF the user has not a serial Serial will be used.: begin(Stream &serialPort = Serial); //If user doesn't specify then Serial will be used. You then have to make further changes in setupNano() to set baudrate (to 115200) on the Serial (instead of softserial). As Serial is also used for USB you will indeed see some stuff.

joshmo93
Posts: 1
Joined: Sun Sep 09, 2018 1:22 am

Re: M6E Nano with Redboard and ESP8266 wifi shield

Post by joshmo93 » Sun Sep 09, 2018 1:25 am

Reviving this thread, i am using the m6E nano with esp8266 wifi shield and the redboard,
i was completely unable to get the m6e nano and reboard to run any of the sparkfun example scripts and just added on the wifi shield then
do you have your complete code and any lessons learned i can gain from you?
we wanted to demo it this tuesday so would be awesome if you could let me/everyone visiting this thread in the future how you were able to get it to work

Post Reply