Arduino: connect Arduino with ESP8266 aka ESP-01 - arduino

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

Related

ESP32 Connected to GPS module. No serial out unless holding down reset button

I'm new to Arduino and I'm having some trouble. I have a 16E TTL GPS module connected to the RX and TX pins on my NodeMCU ESP32 board and have a simple Arduino sketch i wrote to output the data to the serial monitor.
String data = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
data = Serial.read();
Serial.print(data);
delay(500);
}
I am only getting the GPS data in the serial monitor while I am holding down the RST button on the board and an output of "-1" every cycle otherwise.
I have tried looking up the problem but I cant seem to find a solution and I have tried figuring out how to use serial in detail but I'm admittedly confused.
I expected the data to just be printed every loop.
You're using Serial both to output debugging messages and to talk to the GPS.
The RX and TX pins that you connected the GPS to are the same serial port as the USB serial chip connects to. Every time you write something Serial it goes to both the USB port and the GPS. So when you read anything from the GPS, you immediately write it back to it.
You can only use the serial port for one thing at a time. Since it's connected to a USB serial chip, your best bet is to use a second serial port for the GPS.
For instance:
#define RX1_PIN 9
#define TX1_PIN 10
String data = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1, RX1_PIN, TX1_PIN);
}
void loop() {
data = Serial1.read();
Serial.print(data);
delay(500);
}
You should set RX_1PIN and TX1_PIN to be whatever pin numbers are convenient for you; just be sure that they're pins that are available on your board and aren't being used for something else.

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 SoftwareSerial corrupted output

Good afternoon,
I am trying to communicate through WiFi with ESP8266 module on Arduino. So far I have succeded to make my hardware setup and a very basic communication in between Arduino and ESP8266 module. I am using SoftwareSerial library to communicate, however the data outputs printed to the Serial seems quite corrupted, even though the module succesfully connects.
When I sent AT+CWJAP="AndroidAP","52689785" on Serial console, this is the output:
AT+CWJAP="AndroidAP","52689785"
AT+C⸮⸮P⸮⸮⸮⸮⸮⸮⸮ѕ͉b⸮⸮⸮⸮⸮⸮ѕ⸮ɂ⸮⸮j
WIFI DISCONNQ(UH⸮WIFI CONNECTED
WHFI GOT IP
OK
Below is my full code, I communicate with ESP module through pins 10 and 11:
#include "SoftwareSerial.h"
SoftwareSerial softSerial(10, 11); // RX, TX
void setup()
{
Serial.begin(9600);
// Serial.setTimeout(30);
softSerial.begin(115200);
// softSerial.setTimeout(30);
while(!Serial);
}
void loop()
{
if (softSerial.available())
{
String message = softSerial.readString();
Serial.print(message);
}
if (Serial.available())
{
String message = Serial.readString();
Serial.print(message);
softSerial.print(message);
}
}
I would appreciate if you could show me the solution for a better communication in between Arduino and ESP module, thanks!
Most of the ESP8266 modules will be working better at 115200 baud rate.
The reason for gibberish output can be:
either you may be viewing output at 9600 baud rate
visit the link

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

Receiving Response from Serial Device in Arduino MEGA

I am a newbie with Arduino Mega 2560 .I have been trying to connect the Arduino and SIM900A module(GSM/GPRS module).I have connected the USB to my PC(Serial instance) and pins 18(Tx) and 19(Rx) to Rx and Tx in the GSM/GPRS module respectively and the GND pin(GSM/GPRS) module's is connected to GND,one near pin 13 in the Arduino.
Power connection:-
I am powering using 12V supplies for each of the boards.
The below is my code.
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
delay(1000);
Serial.print("Initial Setup !!");
delay(5000);
}
void loop()
{
if(Serial.available())
{
char a=Serial.read();
Serial1.print(a);
//Serial.print(a);
}
if(Serial1.available())
{
char B=Serial1.read();
Serial.print(B);
//Serial.print(a);
}
}
I am able to get the initial response in the "Serial Monitor" like (+CFUN:1,+CPIN:READY)(Once I open the Serial monitor I used to press the reset in the GSM/GPRSmodule).
But when I type some AT commands in the Serial Monitor,I am not able to get the response like "OK" from the GPRS/GSM Module.
Please let me know what I should be doing for getting the responses back from GSM/GPRS module.
Have you tried cutting out the Arduino, for just a moment? Get yourself a UART and wire up TX/RX to the GMS respectively. Then plug it into your PC and launch terminal (Tera Term, etc.).
Try issuing some AT commands and make sure you're getting correct responses/echos. You may also want to try a tool called QNavigator (free download).

Resources