1

I've been working on this small project, not that small for me though, for a while. Because I also had classes and exams I didn't really have time for it. But almost everything was done except that I couldn't figure out the proper way of communicating with the ESP8266-01 and I need your help or guidance or any idea that may help.

Here is what I am trying to do:

I have an Arduino Uno, an L298N motor driver, an ESP8266-01 and a small DC motor, all connected properly to my Arduino. Besides that, I also have a gamepad for PC, which I would like to control the DC motor with over the ESP8266-01.

Here is what I've done so far:

I've used the SDL library in C to get the analog stick input from my gamepad and I am able to increase and decrease the speed with my gamepad.

I've also connected the ESP8266-01 to my WiFi and I can change the motor speed from terminal using telnet (via port 80). But not accurately.

What is left is to find a proper way to communicate with the ESP8266-01 so that it responds to every request and to connect these two together so that my project is done and I can control the DC motor with my gamepad.

I was searching for a proper way to communicate with the ESP8266-01 through the terminal. I've tried using telnet (via port 80) but sometimes for some reason the ESP8266-01 won't respond my request or sometimes I lose the connection and I get a respond like below:

telnet: Unable to connect to remote host: Connection refused

Below is the code that I am using for my Arduino to control the motor speed and also the direction of it.

#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(2, 3); //Rx, Tx
void espsend(String);
String sendData(String command, const int timeout, boolean debug);

void setup()
{
  Serial.begin(9600);    //For Serial monitor
  esp8266.begin(9600);   //ESP Baud rate
  pinMode(13, OUTPUT);  
  pinMode(12, OUTPUT);
  pinMode(9, OUTPUT);    //for speed of the DC motor
  analogWrite(9, LOW);
  digitalWrite(12,HIGH); //for direction of the DC motor
  digitalWrite(13, LOW); //for direction of the DC motor

  sendData("AT+RST\r\n", 5000, DEBUG); // reset module
  sendData("AT+CWMODE=1\r\n", 5000, DEBUG); // configure as STA

  sendData("AT+CIPMUX=1\r\n", 3000, DEBUG); // configure for multiple connections
  sendData("AT+CWJAP=\"wifiID\",\"wifiPW\"\r\n", 3000, DEBUG);
  sendData("AT+CIFSR\r\n", 5000, DEBUG); // get ip address
  sendData("AT+CIPSERVER=1,80\r\n", 5000, DEBUG); // turn on server on port 80

}

int connectionId;
void loop()
{
  int velocity;
  char direction[]="direction=";
  char speed[]="speed=";
  char ipd[]="+IPD,";
  if (esp8266.available())
  {
    if(esp8266.find(ipd)){

    delay(1000);
    connectionId = esp8266.read() - 48;
    esp8266.find(speed);
    if(velocity = esp8266.parseInt()){
      analogWrite(9, velocity);
    }
    if(esp8266.find(direction)){
        delay(300);  
        int direction2= esp8266.parseInt();
        if(direction2 == 1){
          digitalWrite(12,HIGH);
          digitalWrite(13,LOW);
    }else{
      digitalWrite(12,LOW);
      digitalWrite(13,HIGH);
    }
}
  String closeCommand = "AT+CIPCLOSE=";  //close the socket connection
  closeCommand += connectionId; // append connection id
  closeCommand += "\r\n";
  sendData(closeCommand, 2000, DEBUG);
    }
  }
}

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(); // read the next character.
      response += c;
    }
  }

  if (debug)
  {
    Serial.print(response); //displays the esp response messages in arduino Serial monitor
  }
  return response;
}

After uploading this code to my Arduino Uno, from the terminal I connect to the ESP8266-01 through telnet 192.168.1.40 80, then I send something like +IPD,1,speed=40. This works but not always and sometimes I lose connection.

What is the best way to communicate with the ESP8266-01 in this situation? I've searched on the Internet but I didn't really get anything useful. Using telnet is not always working and I don't know how fast my ESP8266-01 could respond to those requests, as I will be sending them like in miliseconds.

  • 1
    Program the ESP to receive values over TCP or WebSockets, and send it over Serial to the Arduino. Don't use AT commands. – tttapa Jun 5 '18 at 14:35
  • @tttapa Do you mean to program the ESP using a USB to serial converter and using the ESP8266WiFi libraries on GitHub? Do you have links related to this or a generic topic name that I can search for? Thank you. :) – André Yuhai Jun 5 '18 at 15:00
  • @tttapa Thank you sooo much! :) I couldn't program the ESP directly because I don't have a USB to UART converter but I will get it as soon as possible and try as you suggested. – André Yuhai Jun 5 '18 at 15:45
  • 1
    Did you read the second article? You don't need a USB-to-Serial converter. Although, it'll be easier if you have one. But then again: why spend $2.50 on a USB-to-Serial adapter if you can get a Wemos D1 mini for $3.50 that has a USB-to-Serial converter built-in, and gives you access to all of the ESP8266's IO pins? – tttapa Jun 5 '18 at 15:48

Your Answer

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

Browse other questions tagged or ask your own question.