-->
Page 1 of 1

Arduino Due + ESP8266 for stupids (connect to TCP server)

PostPosted: Wed Apr 22, 2015 6:46 am
by skaarj
Hello

I'm trying to understand how to hook the ESP8266 to an arduino and get it to send some text to a telnet server.
Just for test.
I have to send my username "skaarj", wait for response, then send "stupid", get the prompt, wait for 30 seconds then send "exit".
Telnet server is unencrypted so this is the simplest possible example.

Arduino due is necessary because of its higher processing power, because this system will be further developed in some lab applications.

This is how I did the electric connections:
pin VTD goes to pin 19 (Arduino Due Serial1 Rx);
pin CH_PD + RST + VCC goes to +3.3V;
pin GND goes to GND;
pin Vrxd goes to pin 18 (Arduino Due Serial1 Tx).

I downloaded the Arduino-1.6.1-p1.zip for windows which is presented here in this site and it is described that has "great examples".

Well, there are great examples indeed but I can't find any sketch example for ESP8266. So far the evaluation gets -1 points.

Allright, check https://nurdspace.nl/ESP8266 for AT commands.

Code: Select allvoid setup()
{

SerialUSB.begin(9600);   // dump data to USB serial, Arduino due has an issue when connect serial monitor on Serial0 which resets the program.
Serial1.begin(9600);   // Start hardware serial 1 port,  pin 19 and pin 18 for data

 Serial1.print("AT+RST\r\n");   // reset module
 Serial1.print("AT+CWMODE=1\r\n");  // Configuration as Access Point
 Serial1.print("AT+CWSAP=\"MyAP\",\"MyPass\",2,0\r\n");// SSID , password, channel 2, no encryption
 Serial1.print("AT+CIPMIX=1\r\n"); // Accept multiple connections
}

void loop() {



}


Compiling, uploading, I wish to see if the ESP activates as an access point.
And surprise: nothing happens.

I am still consulting the documentation and I am searching this site and also Google for any clues.
Please advise. Thank you.

Re: Arduino Due + ESP8266 for stupids (connect to TCP serve

PostPosted: Thu Apr 23, 2015 7:08 am
by skaarj
Got something working.

I wrote an unencrypted echo server listening on port 8080 on a freeBSD machine.
From any computer I can telnet to the bsd and whatever I write, it sends back the char ">" as an ACK.

Following Hackaday at https://hackaday.io/project/3072/instructions - I discovered how the author managed to work with ESP8266 without any SoftwareSerial library, which cannot be applied to Arduino Due.

As a note, the code below is a personal modification of the original code presented by the user 808-tm (TM) at hackday.io. I must mention this is NOT my work, it is a study on TM's creation.

My modification to the code at the above-mentioned hackaday page is as following:

Code: Select all#define SSID        "AndroidAP"    // the FreeBSD also connects to my phone
#define PASS        "_a_lot_of_beer_"
#define DEST_HOST   "192.168.43.147"
#define DEST_IP     "192.168.43.147"
#define TIMEOUT     5000 // mS
#define CONTINUE    false
#define HALT        true

#define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor

// Print error message and loop stop.
void errorHalt(String msg)
{
  SerialUSB.println(msg);
  SerialUSB.println("HALT");
  while(true){};
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
  byte current_char   = 0;
  byte keyword_length = keyword.length();
 
  // Fail if the target string has not been sent by deadline.
  long deadline = millis() + TIMEOUT;
  while(millis() < deadline)
  {
    if (Serial1.available())
    {
      char ch = Serial1.read();
      SerialUSB.write(ch);
      if (ch == keyword[current_char])
        if (++current_char == keyword_length)
        {
          SerialUSB.println();
          return true;
        }
    }
  }
  return false;  // Timed out
}

// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
  {while(Serial1.available()) SerialUSB.write(Serial1.read());}
 
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
  echoFind("\n");        // Search for nl at end of command echo
  echoFind("\n");        // Search for 2nd nl at end of response.
  echoFind("\n");        // Search for 3rd nl at end of blank line.
}

// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
  Serial1.println(cmd);
  #ifdef ECHO_COMMANDS
    SerialUSB.print("--"); SerialUSB.println(cmd);
  #endif
 
  // If no ack response specified, skip all available module output.
  if (ack == "")
    echoSkip();
  else
    // Otherwise wait for ack.
    if (!echoFind(ack))          // timed out waiting for ack string
      if (halt_on_fail)
        errorHalt(cmd+" failed");// Critical failure halt.
      else
        return false;            // Let the caller handle it.
  return true;                   // ack blank or ack found
}

// Connect to the specified wireless network.
boolean connectWiFi()
{
  String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
  if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
  {
    SerialUSB.println("Connected to WiFi.");
    return true;
  }
  else
  {
    SerialUSB.println("Connection to WiFi failed.");
    return false;
  }
}

// ******** SETUP ********
void setup() 
{
  SerialUSB.begin(115200);         // Communication with PC monitor via USB
  Serial1.begin(9600);        // Communication with ESP8266 via 5V/3.3V level shifter
 
  Serial1.setTimeout(TIMEOUT);
  SerialUSB.println("ESP8266 Demo");
 
  delay(2000);

  echoCommand("AT+RST", "ready", HALT);    // Reset & test if the module is ready 
  SerialUSB.println("Module is ready.");
  echoCommand("AT+GMR", "OK", CONTINUE);   // Retrieves the firmware ID (version number) of the module.
  echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode.
 
  // echoCommand("AT+CWLAP", "OK", CONTINUE); // List available access points - DOESN't WORK FOR ME
 
  echoCommand("AT+CWMODE=1", "", HALT);    // Station mode
  echoCommand("AT+CIPMUX=1", "", HALT);    // Allow multiple connections (we'll only use the first).

  //connect to the wifi
  boolean connection_established = false;
  for(int i=0;i<5;i++)
  {
    if(connectWiFi())
    {
      connection_established = true;
      break;
    }
  }
  if (!connection_established) errorHalt("Connection failed");
 
  delay(5000);

  //echoCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
  echoCommand("AT+CIFSR", "", HALT);         // Echo IP address. (Firmware bug - should return "OK".)
  //echoCommand("AT+CIPMUX=0", "", HALT);      // Set single connection mode
}

// ******** LOOP ********
void loop()
{
  // Establish TCP connection
  String cmd = "AT+CIPSTART=0,\"TCP\",\""; cmd += DEST_IP; cmd += "\",8080";
  if (!echoCommand(cmd, "OK", CONTINUE)) return;
  delay(2000);
 
  // Get connection status
  if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return;

  // Build HTTP request.
  cmd = "GET / HTTP/1.1\r\nHost: "; cmd += DEST_HOST; cmd += ":8080\r\n\r\n";
 
  // Ready the module to receive raw data
  if (!echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE))
  {
    echoCommand("AT+CIPCLOSE", "", CONTINUE);
    SerialUSB.println("Connection timeout.");
    return;
  }
 
  // Send the raw HTTP request
  echoCommand(cmd, "OK", CONTINUE);  // GET
 
  // Loop forever echoing data received from destination server.
  while(true)
    while (Serial1.available()) {
      cmd="TEST\r\n\r\n";
      //cmd = "GET / HTTP/1.1\r\nHost: "; cmd += DEST_HOST; cmd += ":8080\r\n\r\n";
      //echoCommand("AT+CIPSEND=0,"+String(cmd.length()), ">", CONTINUE);
      SerialUSB.write(Serial1.read());     <-------------------------freezes here, it does not receive the ACK
    }
     
  errorHalt("ONCE ONLY");
}



Monitoring is done via SerialUSB because Arduino Due has a flaw: when connecting to Serial0 (programming port) the machine resets.

Output on minicom (warning: sometimes FreeBSD recognises Arduino Due USB port as a mouse, needs low level modifications at kernel level at both ums and uftdi modules):

Code: Select all--AT+RST
<▒▒▒▒▒▒
       ▒▒?=▒▒x
[Vendor:www.ai-thinker.com Version:0.9.2.4]

ready
Module is ready.
--AT+GMR

AT+RST

OK
--AT+CWMODE?

c▒c▒RSW▒j▒j▒WfJ[▒▒▒▒
[Vendor:www.ai-thinker.com Version:0.9.2.4]

ready
--AT+CWMODE=1
AT+CWMODE=1

no change

--AT+CIPMUX=1
AT+CIPMUX=1



OK

--AT+CWJAP="AndroidAP","_a_lot_of_beer_"
AT+CWJAP="AndroidAP","_a_lot_of_beer_"
Connection to WiFi failed.
--AT+CWJAP="AndroidAP","_a_lot_of_beer_"
AT+CWJAP="AndroidAP","_a_lot_of_beer_"
busy p...

OK
Connected to WiFi.                  <-------------------linked
--AT+CIFSR


AT+CIFSR

192.168.43.251    <------------------got an IP from Android dhcp server

--AT+CIPSTART=0,"TCP","192.168.43.147",8080    <------------connect to echo server

OK
--AT+CIPSTATUS

AT+CIPSTART=0,"TCP","192.168.43.147",8080    <------------------one more time connect

OK
--AT+CIPSEND=0,45

Linked
AT+CIPSTATUS
STATUS:3
+CIPSTATUS:0,"TCP","192.168.43.147",8080,0

OK
AT+CIPSEND=0,45
>
--GET / HTTP/1.1      <---------------original http request
Host: 192.168.43.147:8080    <---------------this is also echoed on FreeBSD


Host: 192.168.43.147:8080   <---------need to work on the request   <---- this one is not
wrong syntax

ERROR

SEND OK
--AT+CIPSEND=0,45


+IPD,0,3:>
▒--AT+CIPSEND=0,45


OK
AT+CIPSEND=0,45
AT+CIPSEND=0,45
busy p...
>   <-----------------------------------------Here I get back the ACK
▒--AT+CIPSEND=0,45
▒AT+CIPSEND=0,45
<--------------------------------------frozen, no more ACK


Still working.
Please advise.