0

I am working on a project involving bluetooth and wifi communications. Both of them are wired up to the Arduino and communicate to it using two software serial port. The scenario is the one in the picture attached.

What I am doing is:

  • retrieve the sensor value
  • send it from the BT slave module to the master one (wired up to Arduino)
  • take it and send it, using AT commands, through the WIFI channel (the WIFI module is a ESP8266-01) to a web server.

The problem is that if I start listening to the BT serial port (BTSerial.listen()) I am not able to properly send the sensor values through the WIFI connection. But if I comment out the lines related to the BT sotware serial, and try to send fixed value to the web server, the sending terminates succesfully.

In my opinion there are some problems with the BTSerial.listen() and WIFISerial.listen() calls, since they are not able to switch from one the the other correctly.

I saw the TwoSerialPort example provided into the Arduino IDE and I tried to apply it into my code but it still not works to me.

Does anyone have a idea which would be the problem? Does anyone already faced with this problem or a similar one?

Hoping to be as much clear as possible, thanks in advance to anyone would help me.

Filippo

[EDIT] Added two code snippets

// This method prepares the AT commands will be send to the ESP8266-01 module in order to upload the values on the web server 
void send2server(String ip, int port, String file, int t, int h, int p) {

  String queryString = "temp=";
  queryString.concat(t);
  queryString.concat("&hum=");
  queryString.concat(h);
  queryString.concat("&press=");
  queryString.concat(p);
  Serial.println(queryString);

  // Prepare and exec the AT+CIPSTART command provideing the TCP connection init
  String CIPSTART_cmd = "AT+CIPSTART=\"TCP\",\"" + ip + "\"," + port + "";
  exec_cmd(CIPSTART_cmd, 1000);

  // Prepare the POST request
  String httpPOST_cmd = "POST /";
  httpPOST_cmd.concat(file);
  httpPOST_cmd.concat(" HTTP/1.1\r\n");
  httpPOST_cmd.concat("Host: ");
  httpPOST_cmd.concat(ip);
  httpPOST_cmd.concat("\r\n");
  //httpPOST_cmd.concat("Accept: */*\r\n");
  httpPOST_cmd.concat("Content-Length: ");
  httpPOST_cmd.concat(queryString.length());
  httpPOST_cmd.concat("\r\n\r\n");
  //httpPOST_cmd.concat("Content-Type: text/html\r\n\r\n");
  httpPOST_cmd.concat(queryString);

  // Calculate the POST request length
  unsigned int len = httpPOST_cmd.length();

  // Prepare and exec the AT+CIPSEND statement with the POST request length just calculated
  String CIPSEND_cmd = "AT+CIPSEND=";
  CIPSEND_cmd.concat(len);
  exec_cmd(CIPSEND_cmd, 1000);

  // Exec the HTTP POST request
  exec_cmd(httpPOST_cmd, 1000);

  // Close the connection
  exec_cmd("AT+CIPCLOSE", 1000);
}

// This method execute each AT command in the previuous method
void exec_cmd(String cmd, int delayTime) {
   byte i = 0;

   while(1) {
      Serial.println("invio comando");
      WIFISerial.println(cmd);
      while(WIFISerial.available()) {
         if(WIFISerial.find("OK")) {
            i = 8;
         }
      }
      delay(delayTime);

      if(i > 5)
         break;

      i++;
   }

   if(i == 8) {
      Serial.println("OK");
      //return true;
   } else {
      Serial.println("ERRORE");
      //return false;
   }
}

The problem is the code goes properly till the AT+CIPSEND command, then the POST request is sent but no good feedback arrives, ending with the "ERRORE" error message. enter image description here

[EDIT 2] Attached serial monitor output This is the serial monitor output when I execute the skecth. It seems to work properly but it does not upload any value. enter image description here

  • 4
    SoftwareSerial is poor at best. With two it's poor squared. – Majenko Jul 6 '17 at 21:39
  • You can't use two instances of the normal software serial. Probably you best bet is to drop the Arduino and instead run your sketch on the ESP8266, with that talking to the BT. Or use nRF24L01's to link the sensors instead of BT. – Chris Stratton Jul 6 '17 at 21:51
  • Arduinos now come in so many configurations and use so many different processors you now need to specify exactly what you are using. If a processor has only 1 hardware serial port you will have to implement the 2nd serial port in software. There are different libraries to use for this. Needless to say, a software implemented serial port is not as good (fast) as a hardware serial port. As you can see, there is more to programming serial ports on Arduinos then simply instantiating another instance of the standard serial port library. Goodby software. Welcome to firmware. – st2000 Jul 6 '17 at 23:50
0

This answer provides full details on choosing "good" combinations of hardware and software serial ports. To summarize:

  • Use the HardwareSerial port (i.e., Serial) for one of the devices. You can still use it for debug prints, as long as the device has some kind of command "format". For example, if commands start with "AT", just make sure that none of your debug prints start with "AT". It is the absolute best choice. You would have to disconnect RX pin 0 from the device in order to upload new sketches over USB.

  • Use AltSoftSerial for the other device. It is the most efficient software serial port, and it can be used at the same time as Serial. It only works on two specific pins.

  • NeoSWSerial is the next best choice, at limited baud rates. It can be used with the above choices, but transmitting on NeoSWSerial prevents them from receiving.

  • SoftwareSerial is the worst choice. Receiving or transmitting on SoftwareSerial prevents all of the above from receiving anything. It disables interrupts for long periods of time.


UPDATE - Here is the most reliable configuration:

enter image description here

Your code will have something like this:

#include <AltSoftSerial.h>
AltSoftSerial BT; // 8 is RX, 9 is TX

#define WIFI Serial  // 0 is RX, 1 is TX (disconnect 0 to upload new sketch)

void setup()
{
  WIFI.begin( 115200 );
  BT.begin( 9600 );
}

void loop()
{
  if (BT.available) {
    char c = BT.read();
    // do something with the character
       ... save them until '\n' arrives?
         ... then parse some values?

    if (values all received) {
      Serial.print( F("Sending values") ); // this is the WIFI port, but
          //  the ESP8266 ignores this debug print because it doesn't
          //  start with "AT".

      WIFI.println( F("AT+ ...") ); // all the AT commands
          ...
      WIFI.print( btValue1 );    // and the BT values
          ...
    }
  }
}
  • Thanks for the replies @slash-dev. If I understand correctly, using NeoSWSerial I would be able to receive data on the BT serial and transmit it towards the WIFI module over the WIFI serial simultaneously. Am I right or not? – Filippo Jul 7 '17 at 8:36
  • And if so, is there a guide or a tutorial on NeoSWSerial? Thanks again!! – Filippo Jul 7 '17 at 8:59
  • @Filippo, so you're ignoring the two best choices? <sigh> You would have to create a NeoSWSerial instance with a RX pin for the BT TX, and a TX pin for the WIFI RX. Right now, you can only transmit and receive simultaneously with the one NeoSWSerial that is "listening". You could make another instance for the WIFI TX/BT RX. Just like SoftwareSerial, NeoSWSerial can only listen to one pin at a time. Srsly, use AltSoftSerial (pins 8 & 9) with Serial (pins 0 & 1) to avoid all these issues. – slash-dev Jul 7 '17 at 14:22
  • @Filippo, there is no "tutorial" for NeoSWSerial, because it is a drop-in replacement for Serial or SoftwareSerial. Some extra info at the NeoSWSerial github page. All these libraries are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.... – slash-dev Jul 7 '17 at 14:26
  • Already found either github page or library into the Arduino IDE, thanks @slash-dev. I am trying to understand the test sketch in to the NeoSWSerial library in order to apply it into my project. But, since I am nquite new to Arduino, could you explain ti tom me a little bit, please? I have an Arduino UNO, so I got no Serial1, Serial2, ecc. ports. How can I apply your library to the scenario in the picture below? – Filippo Jul 7 '17 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.