I'm using two header file, SoftwareSerial.h and ESP8266WiFi.h. When I compile and upload programs, with ESP8266WiFi.h, it had an error "Failed to connect to ESP8266: Timed out waiting for packet header". I tried it with only ESP8266WiFi.h, but it had an same error. To use this header file, what should I do?
Here's my circuit and code.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "circuits4you";
const char *pass = "password";
unsigned int localPort = 2000; // local port to listen for UDP packets
IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);
WiFiUDP udp;
char packetBuffer[9]; //Where we get the UDP data
void setup()
{
Serial.begin(9600);
Serial.println();
WiFi.softAP(ssid, pass); //Create Access point
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
void loop()
{
int cb = udp.parsePacket();
if (!cb)
{
if(Serial.available()>0)
{
udp.beginPacket(ClientIP, 2000);
char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266
udp.endPacket();
}
}
else {
udp.read(packetBuffer, 1);
Serial.print(packetBuffer);
delay(20);
}
}
enter image description here
Your code is designed for using ESP8266 as a stand-alone microcontroller running with Arduino Core. I realised that you are confusing about the operation modes of ESP8266. There are basically two ways to operate with an ESP8266.
Uses ESP8266 as a stand-alone micropcontroller, in this case, the ESP8266 needs to have the firmware such as RTOS or Arduino Core for ESP8266;
Uses ESP8266 as a plain WiFi device which can be communicate with AT Commands.
Currently your code is designed for running on a stand-alone ESP8266, but I realised that your hardware configuration is configure for communicating with ESP8266 as a WiFi device, and you have the AT command firmware on your ESP8266. In order for Arduino to communicate with ESP8266 via AT Commands, you will need run this Arduino sketch to get started, the sketch basically read the AT command you type in on Serial Monitor and send it to ESP8266, and echo out the msg received from ESP8266:
#include <SoftwareSerial.h>
SoftwareSerial espSerial(2, 3); //RX,TX
// Arduino pin 2 (RX) to ESP8266 TX
// Arduino pin 3 to voltage divider then to ESP8266 RX
// Connect GND from the Arduiono to GND on the ESP8266
// Pull ESP8266 CH_PD HIGH
void setup()
{
Serial.begin(9600); // for communication with the host computer
espSerial.begin(9600); // Soft derail for communicate with ESP8266
// Remember to set 'Both NL & CR' in the serial monitor
}
void loop()
{
// listen for data from the ESP8266 and then write it to the serial monitor
if ( espSerial.available() ) {
Serial.write( espSerial.read() );
}
// listen for manual input from user and send it to the ESP8266
if ( Serial.available() ) {
mySerial.write( Serial.read() );
}
}
Remember to set 'Both NL & CR' in the serial monitor
There is a good official AT Command example from Expressif that you could follow, include UDP communication. You can manually type in the AT Commands via Serial Monitor step by step to test the ESP8266. Once everything works, you could then write the sketch to automate all the AT commands in program to replace the code in the loop().
If communicating with ESP8266 is too much of work and too tedious for you, you might want to consider to use ESP8266 as a stand-alone microcontroller by upgrading your ESP8266 with firmware such as Arduino Core. You can then use the code you shown. But your hardware need to change a little bit in order to set the ESP8266 to firmware flash mode, you need to ground the GPIO0 on ESP8266, and have a reset switch on ESP8266 between reset pin and ground, so that you can press it to put ESP8266 into programming mode.
Please make sure your target board is set to ESP8266 on your Arduino IDE in this case.
Related
I've been trying to connect two HC-05 bluetooth modules together as master and slave devices. I know that to do this i need to establish one as a slave device and one as a master using the AT command mode. I am using an arduino nano with each of the modules and the circuit i have used is shown:
Vcc -----> 5V
GND ----> GND
Rx ------> Rx
Tx ------> Tx
I followed various online tutorials and have used this code:
include SoftwareSerial.h
SoftwareSerial BTSerial(0, 1); // RX | TX
void setup()
{
Serial.begin(9600);
BTSerial.begin(9600); // HC-05 default speed in AT command more
Serial.println("Enter AT commands:");
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available()){
Serial.write(BTSerial.read());
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available()){
BTSerial.write(Serial.read());
}
}
Using the button on the module or by setting the EN pin high, i am able to put the module into AT mode as displayed by the LED blinking every 2 seconds. However, i receive no response after sending commands to the module using the serial monitor when i should receive a confirmation of my command.
Any ideas where i'm going wrong?
Here's the solution that eventually worked for me: I used this circuit with a voltage divider:
Vcc -----> 5V
GND ----> GND
D2 ------> Tx
D3 ------> Rx
I ended up having to buy an Uno for this to work, I'm assuming then that my Nano's were faulty in some way. I then used the following code:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX
void setup()
{
Serial.begin(9600);
BTSerial.begin(38400); // HC-05 default speed in AT command more
Serial.println("Enter AT commands:");
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available()){
Serial.write(BTSerial.read());
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available()){
BTSerial.write(Serial.read());
}
}
This allowed me to enter the AT mode and also receive responses.
One of the issues was that I was using the Tx and Rx pins which are also used to communicate with the computer so cannot be used with the HC-05 at the same time.
Another issue was the baudrate: I alternated between 9600 and 38400 for each communication until I found a combination that worked, and adjusted the speed in the Serial monitor so that it made sense.
Then I was able to use the command mode normally.
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
I have a Xbee explorer on the pc, and one arduino with the arduino wireless shield with another Xbee there. Using XCTU I can receive data from the arduino to the PC but not the other way around, sending using XCTU to the arduino.
If I send from the XCTU only the led RSSI from the arduino wireless shield is on but it should be the RX led.
This is the tutorial I'm using Link
Here's the code I'm using on the arduino, booth antennas are S1 Xbee antennas and the antennas are all default values restore
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
SoftwareSerial XBee(10,11); // RX, TX
void setup()
{
// Set up both ports at 9600 baud. This value is most important
// for the XBee. Make sure the baud rate matches the config
// setting of your XBee.
XBee.begin(9600);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{ // If data comes in from serial monitor, send it out to XBee
Serial.println("first if");
XBee.write(Serial.read());
}
if (XBee.available())
{ // If data comes in from XBee, send it out to serial monitor
Serial.println("second if");
Serial.write(XBee.read());
}
}
Please precise the operating mode on your two xbee modules thier model( it can be hard to communicate when using XBEE3), and their DL, DH
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
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).