Go Down

Topic: serial monitor and esp8266 (Read 313 times) previous topic - next topic

metadeth2401

Hello I am doing a project I saw on instructables and I can't do it, I am using an esp 8266 8-pin wifi card which is connected to the pins 2 and 3 (who partnered to serial port) but i don't receive any data i asked in the code, already checked the connections in the serial,the wifi card, and the COM ports.

would greatly appreciate the help

here is my code
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial esp8266(3, 2);

#define DEBUG true
int state = 5;
//define motor pins
const int motor1Pin1 = 5;
const int motor1Pin2 = 6;
const int motor2Pin1 = 9;
const int motor2Pin2 = 10;
//define motor speed
int motorSpeed = 150;


void setup()
{

  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

 
  Serial.begin(9600);
  esp8266.begin(9600);
 
  sendData("AT+RST\r\n", 2000, DEBUG);
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  sendData("AT+CWJAP=\"Project1\",\"noconectar\"\r\n", 2000, DEBUG);
  delay(10000); //wait for connection

  sendData("AT+CIFSR\r\n", 1000, DEBUG);
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
  sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
}

void loop()
{
 
  if (esp8266.available())  //verify incoming data
  {
    if (esp8266.find("+IPD,")) //if there is a message
    {
      String msg;
      esp8266.find("?"); //look for the message
      msg = esp8266.readStringUntil(' '); //read whole message
      String command = msg.substring(0, 3); //first 3 characters = command
      Serial.println(command);
           
      //move forward
      if(command == "cm1") {
          state = 1;
      }

      //move backward
      if(command == "cm2") {
          state = 2;
      }

      //turn right
      if(command == "cm3") {
          state = 3;
       }
       
       //turn left
       if(command == "cm4") {
          state = 4;
       }

       //do nothing       
       if(command == "cm5") {
          state = 5;
       }

    }
  }
 
  //STATE 1: move forward
  if (state == 1) {
    analogWrite(motor1Pin1, motorSpeed);
    digitalWrite(motor1Pin2, LOW);
    analogWrite(motor2Pin1, motorSpeed);
    digitalWrite(motor2Pin2, LOW);
  }
  //STATE 2: move backward
  if (state == 2) {
    digitalWrite(motor1Pin1, LOW);
    analogWrite(motor1Pin2, motorSpeed);
    digitalWrite(motor2Pin1, LOW);
    analogWrite(motor2Pin2, motorSpeed);  }
  //STATE 3: move right
  if (state == 3) {
    analogWrite(motor1Pin1, motorSpeed);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    analogWrite(motor2Pin2, motorSpeed);
  }
  //STATE 4: move left
  if (state == 4) {
    digitalWrite(motor1Pin1, LOW);
    analogWrite(motor1Pin2, motorSpeed);
    analogWrite(motor2Pin1, motorSpeed);
    digitalWrite(motor2Pin2, LOW);
  }
  //STATE 5: do nothing
  if (state == 5) {
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
  }
 
}

//*******************
//Auxiliary functions
//*******************
String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  esp8266.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (esp8266.available())
    {
      char c = esp8266.read();
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

pert

The AT firmware on the ESP8266 is set to do serial communication at 115200 baud by default but SoftwareSerial doesn't reliably support that speed. You will need to use AT commands to change the baud rate of the AT firmware to 9600 before your code will work.

metadeth2401

The AT firmware on the ESP8266 is set to do serial communication at 115200 baud by default but SoftwareSerial doesn't reliably support that speed. You will need to use AT commands to change the baud rate of the AT firmware to 9600 before your code will work.
it still don't work, i used the AT+CIOBAUD but it does not worked :(

pert

Please explain exactly what you mean by "i used the AT+CIOBAUD but it does not worked".

metadeth2401

#4
Sep 14, 2017, 01:55 am Last Edit: Sep 14, 2017, 02:03 am by metadeth2401
Please explain exactly what you mean by "i used the AT+CIOBAUD but it does not worked".
i putted in my code and also i tried with the serial monitor
UPDATE: now the serial put some strange symbols
Code: [Select]

  Serial.begin(9600);
  esp8266.begin(9600);
  sendData("AT+CIOBAUD=9600", 5000, DEBUG);
  sendData("AT+RST\r\n", 2000, DEBUG);
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  sendData("AT+CWJAP=\"Project1\",\"noconectar\"\r\n", 2000, DEBUG);
  delay(10000); //wait for connection

  sendData("AT+CIFSR\r\n", 1000, DEBUG);
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
  sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
}

pert

Code: [Select]

  esp8266.begin(9600);
  sendData("AT+CIOBAUD=9600", 2000, DEBUG);

If it's running at 115200 this won't work because when you send the AT command at 9600 it just comes through to the ESP8266 as garbage so it will never recognize the AT+CIOBAUD command.

You could try this:
Code: [Select]

  esp8266.begin(115200);  // start at 115200 baud because that is the default configuration of the ESP8266 AT firmware.
  sendData("AT+CIOBAUD=9600", 2000, DEBUG);  // configure the ESP8266 to communicate at 9600 baud
  esp8266.begin(9600);  // now that the ESP8266 has been configured to communicate at 9600 baud we need to switch to that baud rate

The trouble with the above is that SoftwareSerial doesn't reliably work at 115200 so you have no assurance that the AT+CIOBAUD command will not be garbled and thus fail to configure the ESP8266 to 9600 baud.

Also, note that AT+CIOBAUD is not a valid command on some versions of the AT firmware. If you look at the documentation for the recent versions of the Espressif AT firmware:
http://espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
you'll notice that the commands are AT+UART_CUR to temporarily configure the UART settings (will be lost after power down) or AT+UART_DEF to permanently configure the UART settings (written to non-volatile flash memory). You can get information on the AT firmware by running AT+GMR but of course that means you need to be able to successfully do serial communication with the ESP8266. I believe the AT commands were different with older versions of the AT firmware (at one point it was AT_UART). Also there are two different types of the AT firmware. One is from Espressif, the manufacturer of the ESP8266 chip, the other is from AI-Thinker, the manufacturer of the ESP8266 modules. They are similar but not the same and the commands may also vary depending on which version of either you are using. I believe that the ESP8266 modules typically ship with the AI-Thinker firmware. I always flash them with the Espressif firmware immediately because it actually has proper documentation. Maybe there is official and up to date documentation for the AI-Thinker firmware somewhere but their website is all in Chinese so I can't find it.

also i tried with the serial monitor
And what happened?

metadeth2401

#6
Sep 14, 2017, 02:43 am Last Edit: Sep 14, 2017, 03:52 am by metadeth2401
And what happened?
nothing, but i think the baud rate is working properly because i got weird simbols like inverted question marks and squares(obiouslly not printable characters) but any info

pert

Weird symbols can be caused by baud rate mismatch.

Which Arduino board is the ESP8266 connected to?

How exactly do you have the ESP8266 connected to the Arduino board?

Robin2

I was trying to get an ESP8266-12 working last night and eventually discovered that it works at 115200 baud. I was not able to find how to change the baud rate. All of AT+CIOBAUD, AT+UART_CUR or AT+UART_DEF  just returned ERROR

The AT+GMR returned the exact same result as in Kolban's book.

I was able to get it to work when directly connected to the Serial Monitor via the USB-TTL adapter and without any Arduino board.

It occurred to me, before falling asleep, that the answer is to use the Uno's HardwareSerial to communicate with the ESP8266 at 115200 baud and use SoftwareSerial at 9600 baud via the USB-TTL adapter to talk to the PC. So not tested yet.

I came across one website that suggested AT+IPR is use to change the baud rate and another website that said that would BRICK the ESP8266 - so I have not tried it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pert

Yeah, it's really a nightmare how there are two different similar but not identical AT firmwares and also that they have changed the AT commands over time. It's a bit difficult to track down the documentation for the older firmware versions and the AI-Thinker firmware and to know which version the documentation is actually referring to. That's why I just flash the latest Espressif firmware so I know for sure which version I have and also that I can trust the latest AT command reference published by Espressif. Unfortunately the flashing procedure is also a bit complicated (even more so due to the official documentation not being very good and many of the unofficial tutorials also not great), and even when you do it right it sometimes still causes problems.

It's definitely a good idea to wire the thing up to a USB-TTL adapter if you have one and just use Serial Monitor or whatever to verify it's working and get the baud rate changed if necessary before adding the extra complexity of the Arduino into the mix. A lot of beginners try to use the Arduino as a USB-TTL adapter for this purpose, which is doable, but it just increases the chance for error of an already confusing process.

Robin2

I bought a few of them so I can afford to brick one of them - though I would prefer not to. My plan is to program it directly from the Arduino IDE and (AFAIK) that overwrites the standard firmware. I might try re-flashing the Espressif firmware first.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pert

That's correct, if you're programming the ESP8266 directly then you don't need to worry about the AT firmware because the first time you upload a sketch to the ESP8266 it will overwrite it. The AT firmware is mainly for people who want to program a standard Arduino board and just use the ESP8266 as a WiFi module connected to the board. There are definitely advantages to each but I think you'll have more fun playing with programming the ESP8266 directly. Coming from only having used AVRs previously it's certainly different to have such a high clock speed and so much memory, even if some of it gets hogged by the WiFi processes happening in the background.

You also have the option of programming the ESP8266 with MicroPython by flashing it with that firmware. I know you like Python. I prefer the more low-level approach but I'm better in C++ than Python anyway.

Robin2

I managed to reflash the firmware using esptool.py and this Electrodragon firmware (v 1.3.0.2) and now I can change the baud rate with AT+CIOBAUD=9600

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up