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.

asked Mar 13, 2017 at 16:31
2
  • You forgot to print the return value of wifi.testAT();...? Commented Mar 13, 2017 at 16:33
  • it my mistake when post question I have updated it Commented Mar 13, 2017 at 16:36

1 Answer 1

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)

answered Mar 13, 2017 at 22:24
4
  • Hi baud 115200 worked with secondary code. Commented Mar 14, 2017 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. Commented Mar 14, 2017 at 3:02
  • I think that means your Wifi module use baud 9600. Commented Mar 14, 2017 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? Commented Mar 14, 2017 at 3:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.