ESP8266 returning garbage values - arduino

I am working on an Arduino-based project. When I send AT commands manually through the serial monitor, I get the correct response, but when I try the same commands through code, the ESP8266 returns garbage values. I've attached both the responses images and also uploaded the program used.
#include <SoftwareSerial.h>
// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (2,3);
//rx=2 connected to 3 of arduino. tx=3 connected to 2 of arduino
const char SSID_ESP[]="xxxxxxxx";
const char SSID_KEY[]="xxxxxxxx";
void setup() {
Serial.begin(115200);
ESP8266.begin(115200);
// Change this to the baudrate used by ESP8266
delay(1000); // Let the module self-initialize
ESP8266.println("AT");
delay(1000);
while (ESP8266.available()) Serial.write(ESP8266.read());
delay(1000);
ESP8266.println("AT+CWJAP");
ESP8266.println(SSID_ESP);
ESP8266.println("\",\"");
ESP8266.println(SSID_KEY);
ESP8266.println("\"\r\n");
delay(1000);
while(ESP8266.available()) Serial.write(ESP8266.read());
delay(2000);
ESP8266.println("AT+CWMODE=3");
delay(1000);
while(ESP8266.available()) Serial.write(ESP8266.read());
delay(1000);
ESP8266.println("AT+CIPMUX=0");
delay(1000);
while(ESP8266.available()) Serial.write(ESP8266.read());
delay(1000);
ESP8266.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");
delay(4000);
while (ESP8266.available()) {
Serial.write(ESP8266.read());
}
}
void loop() {}

The Fix
1.
AT commands expect a \r\n at the end of commands. Here you are sending a new line after every part of the command.
ESP8266.println("AT+CWJAP");
ESP8266.println(SSID_ESP);
ESP8266.println("\",\"");
ESP8266.println(SSID_KEY);
ESP8266.println("\"\r\n");
The simple fix would be to change all the ESP8266.println() to ESP8266.write()
2.
Also the syntax for this command has a =" after AT+CWJAP
https://github.com/espressif/ESP8266_AT/wiki/CWJAP
So ESP8266.println("AT+CWJAP"); should be ESP8266.println("AT+CWJAP=\"");
Other Solution
These types of problems can be hard to debug. For this reason I try to avoid sending parts of a command. It would be easier to debug if you use a string.
This also has the benefit of being able to send the command to both serial ports so you can see exactly what gets sent.
String ConnectAPCmd = "AT+CWJAP=\"";
ConnectAPCmd += SSID_ESP;
ConnectAPCmd += "\",\"";
ConnectAPCmd += SSID_KEY;
ConnectAPCmd += "\"";
Serial.println("Sent: " + ConnectAPCmd);
ESP8266.println(ConnectAPCmd);

If you haven't changed it yourself, the ESP8266 dosen't run at baud 115200.
The default is 9600.
Even if the ESP8266 runs at baud 115200, the Arduino dosen't handle 115200 with software serial very well. You might want to change to a lower baud.
That aside I agree with #hlovdal. Use write or print and supply the \r\n to the end of each command e.g.
ESP8266.write("AT+CWJAP=");
ESP8266.write(SSID_ESP);
ESP8266.write(",");
ESP8266.write(SSID_KEY);
ESP8266.write("\r\n");

I think it might be because of low power. Try parallel connecting the power source, for example the Arduino UNO boards 3.3v with a couple of AA batteries to power up the ESP.
This made my ESP8266-01 stop returning garbage characters and also stop disconnecting every now and then.

The problem you are facing is because of baud rate - 115200.
Change the baud rate to 9600 ,it will solve your problem.

Related

Program is executing but AT commands not showing in serial monitor

My purpose was to send SMS using GSM SIM800L coreboard and Arduino UNO. Here is the code
#include <SoftwareSerial.h>
//Create a software serial object to communicate with SIM800L
SoftwareSerial mySerial(3, 2); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(115200);
//Begin serial communication with Arduino and SIM800L
mySerial.begin(115200);
Serial.println("Initializing...");
delay(1000);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
mySerial.println("AT+CMGS=\"+ZZxxxxxxxxx\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
updateSerial();
mySerial.print("TEST"); //text content
updateSerial();
mySerial.write(26);
}
void loop()
{
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
And here is the serial monitor output
22:31:19.430 -> Initializing...
However, when I run the code, I get the text message to my mobile phone, but I can't see any AT commands in the serial monitor. It only outputs "Initializing..." .
All the connections and baud rates are okay, checked a thousand times. Has connected 2A, 4.4v power supply to the GSM coreboard and shorten the wires, and sho No bad soldering joints. GSM module red led flash per 3 seconds. And again, I'm getting the text message to my phone. So that means the problem is with the Arduino serial monitor or code, not in the hardware. I need to see AT commands because I need to put more commands through the serial monitor, I tried typing and click send, But it's not showing anything. Any assistance you can provide would be greatly appreciated.
Your logic is reversed in the updateSerial() function.
Actually, you are sending the AT command over mySerial at the setup function, then you need to wait for the answer to come in that object mySerial.
So, you should do the while (!mySerial.available()) ; to be able to read something from it. Once this loop ends, you can read from mySerial.
However, you want to forward it to the serial monitor so, you also need to check if the Serial is available to be written to, that is why you also waits for it, resulting in the while (!mySerial.available() || !Serial.available()) ;.
Once you are sure both serials are available, you can read from one and write what you just read into the other one: Serial.Write(mySerial.read()).
Also, I do not see any need for the mySerial.write(Serial.read()) call, because the Serial is being used just to forward what you are receiving from the SIM800L, thus, you could simply remove that part.
Thus, the correction of your function would result in this:
void updateSerial()
{
delay(500);
while (!mySerial.available() || !Serial.available())
;
Serial.write(mySerial.read());
}
So, with this, everything you receive from the SIM800L is forwarded to the serial monitor.

Arduino and Processing - RX is blinking on board, but LED is not lighting up

I am trying to run a Processing sketch with my Arduino. I got it a few days ago, so I'm pretty much a noob. I made two similar sketches - one in Arduino and one in Processing. The Arduino one does work, while the Processing sketch doesn't, even though when running the Processing one, the RX lights up on the board.
I have connected an LED into the D9 on the board, with a 220 ohm resistor, and plugged the other leg into the GND. I then proceeded to run the Arduino sketch, which is a simple one, it lights up and down the LED for a second. This one worked.
I then tried running the Processing sketch, exact same code ( adapted for Processing ) using the library for Arduino, and the board seems to communicate with my sketch, as the RX is blinking each second on the board ( I tried different intervals of time and they match with the intervals at which the RX blinks ), but the LED does not turn on and off, like it did with the Arduino sketch.
I tried getting only a serial connection between the Arduino, and it worked - I connected a joystick module to the Arduino and sent the X and Y through the serial port, and the Processing sketch received the information through the serial port, so they are, indeed, communicating.
The port used is COM3 and is running at 9600 baud.
This is the Arduino sketch :
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
}
and this is the Processing ( version 3.4 ) sketch :
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
arduino = new Arduino(this, Arduino.list()[0], 9600);
arduino.pinMode(9, Arduino.OUTPUT);
}
void draw() {
arduino.digitalWrite(9, Arduino.HIGH);
delay(1000);
arduino.digitalWrite(9, Arduino.LOW);
delay(1000);
}
Well done on step by step debugging such as double checking the wiring on the electronics side and testing the blink code with the Arduino alone to isolate the issue.
If the Blink sketch is the only Arduino code you have uploaded to your board that won't suffice. Processing does send messages to Arduino (which is why you see the RX LED turn on), but there's nothing in the Arduino code that initialises Serial communication
As you can see in that example, in setup() Serial communication is initialised with 9600 baud rate (communication speed, 9600 bytes/chars per second):
Serial.begin(9600);
Then in draw() if there is data available, each character is read, then printed one at a time with a prefixed message:
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
If you upload the example linked, if you've got a single Serial port, you should see both the RX then ever so slightly after the TX LED blinking when you run your Processing sketch. If you close that sketch, open Serial Monitor in Arduino and type something then press enter you'll see the debugging message read back from Arduino.
Using these notions you could write a basic sketch like so:
int incomingByte = 0; // for incoming serial data
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
// if we received ASCII character '1', turn LED on
if(incomingByte == '1'){
digitalWrite(9,HIGH);
}
// if we received ASCII character '0', turn LED off
if(incomingByte == '0'){
digitalWrite(9,LOW);
}
}
}
Uploading this sketch to your Arduino should allow you to type 1 into Serial Monitor and press Enter to turn the LED on or 0 to turn it off.
The only thing left is to send the same data from Processing:
import processing.serial.*;
Serial arduino;
void setup(){
try{
arduino = new Serial(this, Serial.list()[0], 9600);
}catch(Exception e){
println("error connecting to serial port, double chek USB connection, serial port and close other programs using Serial");
e.printStackTrace();
}
}
void draw(){
}
void keyPressed(){
if(key == '1'){
if(arduino != null){
arduino.write('1');
}else{
println("arduino serial connection wasn't initialised");
}
background(255);
}
if(key == '0'){
if(arduino != null){
arduino.write('0');
}else{
println("arduino serial connection wasn't initialised");
}
background(0);
}
}
Minor side note: notice I'm not using delay() in Processing, I recommend using millis() instead as it doesn't block the execution of code like delay() does.
So the above looks like quite a bit of code just to blink an LED but the point is to understand the basics of Serial communication which will be useful on the long run:
initialising serial communication with Arduino (understand baud rate)
basic reading/writing of bytes over Serial
initialising serial communication from Processing and sending data
Back to your original question, you've missed an important detail regarding the Arduino library you're using in Processing: it's relying on a special Arduino sketch (firmware) called Firmata. You will be able to read more on that and how to use the library in this Arduino and Processing tutorial.
As the tutorial mentions you need to first upload this sketch from Arduino > Examples > Firmata > StandardFirmata. Also bare in mind baud rate is set to 57600, not 9600 so you need to update your code like so:
arduino = new Arduino(this, Arduino.list()[0], 57600);
To use: are you sure to put the standardfirmata
Using the Arduino software, upload the StandardFirmata example (located
in Examples > Firmata > StandardFirmata) to your Arduino board.
change the line
arduino = new Arduino(this, Arduino.list()[0], 9600);
to:
arduino = new Arduino(this, "COM3", 57600); // in Firmata -> Firmata.begin(57600);
you could add this line to look after your serial port:
println(Arduino.list());
Modify the "arduino = new Arduino(...)" line below, changing the number in Arduino.list()[0] to the number corresponding to the serial port of your Arduino board. Alternatively, you can replace Arduino.list()[0] with the name of the serial port, in double quotes, e.g. "COM3" on Windows or "/dev/tty.usbmodem621" on Mac.
I got it working with Arduino but I had to change some details. My port was "COM3" or Arduino.list()[1] (the 2nd port on the list) which you can check in Windows device manager (Ports COM & LPT: USB-SERIAL) after installing the latest drivers (maybe on the usb port that appears when you connect your Arduino under other devices) using the system update and restarting, then you may need to repeat the system update and restart 2 or 3 times. Or on Linux, you can find which port it's on with:
ls /dev/ttyUSB*
Then unplug it and check it again.
First I had to upload the Arduino IDE program (running it with the serial monitor window from the tools menu ctrl-shft-m after having the same exact baud rate on the lower right menu option as in the program). Then I could close it and compile the processing one as long as I had input that very same baud rate into the Processing program too. All 3 different bauds that I tried, 9600, 57600, 115200, worked requiring their equality between Arduino IDE, Arduino IDE Serial Monitor and Processing. If I uploaded a different project in IDE, then Processing did not even connect to the Arduino, so it had to be that same project running on it for Processing to communicate with Arduino Uno properly. Processing is basicly USING Arduino IDE by sending or receiving messages already programmed for it to do, it doesn't program the Arduino in this case. I have even gone through a big mess, trying to get Visual Micro to work (Arduino on Visual Studio) cross-platform but it still would not allow me to link other libraries and headers because of how picky Arduino's programming is! One of the best ways to learn is to check the actual arduino.cc or Processing manual command parameters after finding out where your problem is.

Arduino: connect Arduino with ESP8266 aka ESP-01

I have an Arduino Mega 2560.
I want to connect it to ESP8266 aka ESP 01 module.
First i open and compile an empty sketch. When i start serial monitor, i write AT commands (like connect with WIFI) in serial monitor and i click on send button. In this case all works fine.
After I tested that the commands works properly , I want to write an Arduino sketch in which I implement the function to automatically send command without writing it in serial monitor.
For this purpose, i write this code:
#define SSID "test"
#define PASS "1111"
void connectWiFi() {
Serial.write(“AT+CWJAP=\"SSID\",\"PASS\"");
}
void setup() {
Serial.begin(9600);
connectWiFi();
}
void loop() {
}
When i try to execute the code in Serial monitor, it is printed only the string but the command does not work.
Why when i write this command in serial monitor works and when i try the code above, the command does not work?
Is there a way to pass and execute a command from arduino sketch? What is the problem in my code if is wrong?
Thanks in advance for response.
Sorry for my English.
Serial.write(...) makes arduino write through its serial ports (i.e. USB or pins 0 and 1). A better way to make Arduino send instructions directly to ESP is by defining "software serial" pins to communicate with ESP.
You'll need to include SoftwareSerial.h and use SoftwareSerial esp8266(2,3); for example to make pins 2 and 3 communicate serially with ESP.
Your code should look something like this:
#include <SoftwareSerial.h>
#define SSID "test"
#define PASS "1111"
SoftwareSerial esp8266(2,3);
void setup(){
Serial.begin(9600);
esp8266.begin(9600); //ensure this baudrate is similar to your ESP's
delay(500); //give it some time
esp8266.println(“AT+CWJAP=\"SSID\",\"PASS\""); //send to ESP this way
}
void loop(){
if(esp8266.available()){
while(esp8266.available()){
Serial.write(esp8266.read()); //make serial monitor print what ESP sends
}
}
}
You can also refer to this example for further detail

How to make AT commands work programatically in arduino for ESP8266 wifi module

I am doing a simple tcp communication from an arduino to raspberry-pi wirelessly with an ESP8266 wifi module on arduino uno.The tcp server is running on the raspberry-pi.I am able to do TCP communication with the following AT commands in arduino serial monitor at a baudrate of 9600.
AT+CIPMUX=1
AT+CIPSTART=4,"TCP","192.168.43.150",7777
AT+CIPSEND=4,5
>hai
How to do this programatically in an arduino sketch.I used the following code on my arduino uno,but still without any success.The baudrate is 9600 only since it is working directly in serial monitor.
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3);
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
}
void loop()
{
esp8266.println("AT");
if(esp8266.available()) // check if the esp is sending a message
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
Serial.write(c);
}
}
}
The connections are as follows
ESP8266 Arduino Uno
Vcc 3.3V
CH_PD 3.3V
RX RX(PIN 2)
TX TX(PIN 3)
GND GND
This might be a bit late, but I got stuck with a similar problem fairly recently. If it's sorted then feel free to ignore this.
Depending on firmware version of your ESP8266 module the baud rate of 9600 may not work, try out 115200 instead - it may prove to be more reliable?
I think the main reason your code above isn't working is because of the face that the ESP needs both newline and carriage returns at the end of the AT command. The serial monitor adds these on for you. Rather than sending AT try sending AT\r\n. This should encourage the ESP to reply with OK, or if the echo is turned on AT\r\nOK.
Serial.available() also checks that there is content in a receive buffer - this takes time unfortunately so I had to put a delay(10) in there to get it to register a character in the buffer.
#include <SoftwareSerial.h>
//i find that putting them here makes it easier to
//edit it when trying out new things
#define RX_PIN 2
#define TX_PIN 3
#define ESP_BRATE 115200
SoftwareSerial esp8266(RX_PIN, TX_PIN);
void setup()
{
Serial.begin(9600);
esp8266.begin(ESP_BRATE); // I changed this
}
void loop()
{
esp8266.println("AT\r\n"); //the newline and CR added
delay(10); //arbitrary value
if(esp8266.available()) // check if the esp is sending a message
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
Serial.write(c);
}
}
}
My next problem is that the0 replies for my ESP are unreliable - sometimes they are read as OK but sometime they are garbage values. I suspect it's a matter of not enough power to the module.
I have come across the same problem and yet not have found a solution.
But your connections are a bit of, you have to connect the TX pin of your ESP8266 module to the RX pin of your arduino and the RX pin of your ESP8266 module to the TX pin.
Hope this helps you on your way

xBee communication Arduino to Arduino

I have a realy weird problem with my xBee S1 Pro moduls. I used the XCTU software to configure them. I set one Arduino to recive data and the other one to transmit. When i use the XCTU Software to send some testframes, it works, the reciver gets the data. But if i want my arduinos to communicate it dosent work. I assume that the moduls are configured the right way because PC -> Arduino works. So i'll provide the Sketches so you can tell me whats going wrong
reciver
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0){
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
Serial.print("recived some data: ");
Serial.println(Serial.read());
Serial.flush();
}
}
and now the sender
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("data");
Serial.flush();
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(4000);
}
whats wrong? any ideas?
I would check the connections to the Xbee, i.e. make sure that RX is actually connected to DOUT and TX is connected to DIN. Also if you are sending actual "Frames" then it sounds like you are running your xbees in api mode so you will need to do more then just send out "data" what you want is for it to be running in serial pass through mode.
One last thing to check is if you are using arduino Leonardo or Micro the hardware Serial port is Serial1 not Serial.
You're using the same Serial port for communicating with XBee and USB. That's the problem. You need to set another Serial port instead of the used for USB to communicate with XBee.
It will be good if you share what arduino boards and shields you are using.
So first thing is first... If these xBees have separate passcodes at a different baud rate, shit is going to happen. Also what command mode are these in (AT or API)? Factory default settings ? Accepting AT commands to change these ? (By default you are in AT mode)
Open a serial program (I use coolTerm for OS X ). Ensure to setup correctly of these steps.
Once you know these transmitters are talking at the same baud, passcode, etc... Ensure you have the code uploaded to your Arduinos BEFORE connecting these transmitters to the RX/TX pins with a simple serial read and write.
The code seems right, but make sure what you are trying to send. Xbees can transmit and receive 8bit data only.
First send a known byte of data such as a=100; and see whether this data receives there perfectly or not.

Resources