-->
Page 1 of 1

Arduino command "forwarder"

PostPosted: Sat Nov 29, 2014 6:30 am
by paranoja
Hi everyone,
i just wanted to share this little sketch i wrote, which FW commands from Serial to ESP and prints the output on the Serial.
So you can have arduino connected with the usb and use the "Serial monitor" as a coolTerm or Realterm.

the hookup :
ESP TX,RX to arduino ports 10,11
ESP CH_PD&VCC to arduino 3V3
ESP GND to arduino GND

Code: Select all#include <SoftwareSerial.h>
SoftwareSerial esp(10,11); //ESP firmware has to be 0018000902 so the baudrate can be set to 9600
String str;     //serial input&output
String stresp; //ESP input& output

void setup()
{
Serial.begin(9600);
esp.begin(9600);
}

void loop()
{
Serial.println("unesi komandu/fire the AT command"); 
delay(100);
  while(Serial.available() == 0){}
  str=Serial.readString();
  esp.println(str);
  while(esp.available() == 0){}
  stresp=esp.readString();
  Serial.println(stresp);
}


Hope you will find it usefull.


KR,
Mario

Re: Arduino command "forwarder"

PostPosted: Sat Nov 29, 2014 2:20 pm
by sigxcpu
This is not the right way to do it.

1. Why waiting 100ms between commands?
2. Why blocking reading stuff from either serial?

Here's a better implementation. Please not that it is written here directly, I don't know if it compiles, but you get the idea.


Code: Select all#include <SoftwareSerial.h>
SoftwareSerial esp(10,11); //ESP firmware has to be 0018000902 so the baudrate can be set to 9600
String str;     //serial input&output
String stresp; //ESP input& output

void setup()
{
Serial.begin(9600);
esp.begin(9600);
}

void loop()
{
  if(Serial.available()) {
    char c = Serial.read();
    esp.write(c);
  }
  if(esp.available()) {
    char c = esp.read();
    Serial.write(c);
  }
}

Re: Arduino command "forwarder"

PostPosted: Mon Dec 01, 2014 11:04 am
by paranoja
thx for the input.
True, your method is cleaner....

But the goal is to talk to ESP without the usb2serial HW other than arduino, both methods do the job.

Kr,
Mario