1

I have a problem when I try to use SoftwareSerial in a class..

Here my detail My class

class MyEsp8266
{
 protected: 
     SoftwareSerial *esp8266;
 public: 
     MyEsp8266(SoftwareSerial *_esp8266){
           esp8266 = _esp8266;
     }
     void begin(int baud){
        esp8266->begin(baud);
     }
     String testAT(){
          esp8266->println("AT");
          return esp8266->readString();
     }
}

My Arduino code

#include <SoftwareSerial.h>
#include "MyEsp8266.h"
// the setup function runs once when you press reset or power the board

SoftwareSerial _esp(10, 11);
MyEsp8266 wifi(&_esp);

void setup() {
    Serial.begin(9600);
    wifi.begin(115200);
    Serial.println(wifi.testAT());
}

It doesn't log anything But when I have used all code in Arduino code it worked.

#include <SoftwareSerial.h>
// the setup function runs once when you press reset or power the board

SoftwareSerial _esp(10, 11);

void setup() {
    Serial.begin(9600);
    _esp.begin(115200);
    _esp.println("AT");
    Serial.println(_esp.readString());
}

Thank any tips. Regards.

  • You forgot to print the return value of wifi.testAT();...? – Majenko Mar 13 '17 at 16:33
  • it my mistake when post question I have updated it – HoangHieu Mar 13 '17 at 16:36
1

This is more of a work-around than a solution, but the code you're trying to get to work, worked for me once I changed

wifi.begin(115200);

to

wifi.begin(9600);

(sorry I don't have enough karma to just comment)

  • Hi baud 115200 worked with secondary code. – HoangHieu Mar 14 '17 at 2:55
  • I agree! and when testing your desired block of code, I got it to output the test line by changing the baud from 115200 to 9600. – pizzaisdavid Mar 14 '17 at 3:02
  • I think that means your Wifi module use baud 9600. – HoangHieu Mar 14 '17 at 3:08
  • To clarify, I was using the bluetooth HC-05 which a quick Google search tells me It can go up to 1382400. But besides, I tried both code samples, the second worked without modification (meaning my hardware can handle 115200) and then when I tried the first, and once I changed the baud to 9600 it worked. Did adjusting the baud rate on the first code sample not fix the problem for you? – pizzaisdavid Mar 14 '17 at 3:13

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.