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.
Related
I have started learning about arduino and just bought a bluetooth module, HC-05. From where i bought, it says it has range of approx.10 meters. I made hc-05 connection with arduino in below described ways
I am using it as a slave with default configurations, 9600 baud rate and HC-05 name with pin 1234
GND of HC05 -> GND of `arduino`
VCC of HC05 -> 5V of `arduino`
TX of HC05 -> RX of `arduino`
RX of HC05 -> TX of `arduino` via voltage divider network 2k---|---1k
Below is my arduino code
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
int pin = 13;
char c = ' ';
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
pinMode(pin,OUTPUT);
// HC-05 default serial speed for communication mode is 9600
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
if (BTserial.available()>0){
c = BTserial.read();
Serial.println(c);
switch (c){
case '1' :
digitalWrite(pin,HIGH);
break;
case '2' :
digitalWrite(pin,LOW);
break;
default: break;
}
}
}
Following things happening to me:
When I power the module I am able to discover it with my phone but only when I hold my phone very near to the module. If I move away from the HC05 module, for example 3-4 feet, I am not able to discover it.
2.After connecting with it (while holding phone near to the module), I am able to send data to it but again, if I move away , only a few steps, I am unable to send data and I disconnect from it automatically. Also if I change direction of the antenna even then no communication happens.
My purpose is to control my home's lights and TV with the help of this module and Relays. But HC05 is useless so far. I was hoping that I would code it and wire it and hang it on a wall and interact it with and andoird app.
Is it suppose to happen like this? or there's something wrong with my module's antenna
thank you.
I am trying to get my SIM800C to talk with my Arduino. There is no communication happening, though.
#include <SoftwareSerial.h>
SoftwareSerial at(2, 3);
void setup() {
Serial.begin(9600);
at.begin(9600);
}
void loop() {
// try every 2 seconds
delay(2000);
Serial.println("sending AT ... ");
at.println("AT");
while (at.available() > 0) {
Serial.write(at.read());
}
}
I am not able to get an OK back. SIM800C is supposed to detect the baud rate by itself.
I am sure there has to be a simple stupid mistake. I just don't know what to do at this point. I obviously already checked for cable break. Out of desperation I already tried to switch RX and TX. I also tried different baud rates (whatever is within the usual limitations of SoftwareSerial) but it should automatically detect it once a couple of AT commands got in anyway.
Weird enough, the pin PWX on the SIM800C needs to be hooked up to a GND to work. It started blinking every second now and is responding to AT commands.
Also it turned out that this specific module does not ship with autobauding enabled, as stated by the SIM800C documentation. The correct baud rate is 115200.
There are some problems you need to consider:
Use below sample code which transfers data between PC and SIM. Sometimes SIM module would go into power down state and won't respond on any AT command but would print some results in the serial monitor.
As already mentioned in comments it seems that your wiring is wrong and as you declared Software Serial as SoftwareSerial at(2, 3); which means pin 2 is Rx on Arduino and should connect to Tx pin of SIM and pin 3 is Tx on Arduino and should connect to Rx pin of SIM. Please don't mess with the pins and connect the pins like below correctly.
Arduino SIM
Rx 2 ----> Tx
Tx 3 ----> Rx
I'm not sure if you can power on SIM800 with a 500mA USB connector, make sure that use an external 1/2 A power supply for VCC of SIM module.
Look at the blink speed of SIM module if it connected and powered on it would blinky with 3 seconds delay and if it blinks fast, it means that it is being restarted. Also if SIM powered on correctly it would print some info like SIM READY, CALL READY, etc.
Try other baud rates like 115200 and see if you get anything on power on.
I put some macro definition to make pin mappings more clear.
#include <SoftwareSerial.h>
//SIM800 TX is connected to Arduino D2
#define SIM800_TX_PIN 2
//SIM800 RX is connected to Arduino D3
#define SIM800_RX_PIN 3
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
void setup() {
//Begin serial comunication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
while(!Serial);
//Being serial communication witj Arduino and SIM800
serialSIM800.begin(9600);
delay(1000);
Serial.println("Setup Complete!");
}
void loop() {
//Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
if(serialSIM800.available()){
Serial.write(serialSIM800.read());
}
//Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
if(Serial.available()){
serialSIM800.write(Serial.read());
}
}
Yes this module will not work in this configuration. There is a pin of V_TTL With 5V pin.. This pin enables the TTL logic converter of your GSM.. You have to connect this pin to 5V in case of arduino and to 3V in case of ESP8266.See the pin configuration here
I have a SIM800L module.
I have configured my 800L SIM module, where I connect OUT+ on LM2596 to VCC on SIM800L and OUT- on LM2596 to GND on SIM800L. Besides that, I connect TX SIM800L to pin 2 Arduino and RX SIM800L to pin 3 Arduino
Then, After the source code is uploaded to the arduino mega 2560 board, the SIM800L module flashes 3 times every 3 seconds, sometimes also blinks 7 times every 3 seconds. So on.
And until now my SIM800L module cannot send messages. where is the problem? thanks please answered
First, you must double check that the modem is connected correctly and have enough power. To ensure that I always try to read the output serial of the modem in startup and make a call to it.
If modem starts correctly it should print some data in the serial output (with default settings) and some of them prints the power issues.
You can use the following example to creating a two-way communication between your host PC and the modem. In here I'm using Serial1 with pins 18, 19.
If I remember correctly the blinks should be every 3 seconds, and if it changes it means that modem is being restarted.
After that, you can send AT commands with your host PC and check the functions.
#include <SoftwareSerial.h>
#define serialSIM800 Serial1
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//wait on host serial
while (!Serial);
//Being serial communication with Arduino and SIM800
// you should double check the default baudrate of SIM800 and set it here
serialSIM800.begin(9600);
delay(1000);
Serial.println(“Setup Complete !”);
}
void loop()
{
//Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
if (serialSIM800.available())
{
Serial.write(serialSIM800.read());
}
//Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
if (Serial.available())
{
serialSIM800.write(Serial.read());
}
}
I am trying to communicate with my ESP8266 module through Arduino Mega with the ESP's Rx pin connected to Mega's Pin 7 the ESP's Tx pin connected to Mega's Pin 6. I tried to run the following test code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(6, 7);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Started");
// set the data rate for the SoftwareSerial port
esp8266.begin(9600);
esp8266.write("AT\r\n");
}
void loop() {
if (esp8266.available()) {
Serial.write(esp8266.read());
}
if (Serial.available()) {
esp8266.write(Serial.read());
}
}
I get no response from the ESP8266 module. I then tried to use the Mega's dedicated Tx and Rx pins (1 and 0 respectively). I can send commands using the serial monitor and it replies. First I sent AT+CIOBAUD=9600 under a 115200 baud to change the baud rate of my ESP module to 9600. Then under 9600 baud I sent AT+UART_DEF=9600,8,1,0,0. It replied OK for both as expected. I tried again to run this program but I'm still getting no response from the ESP 8266 module. I also tried simply switching the Rx and Tx pins just to be safe. Still doesn't work. Any ideas on what I might be doing incorrectly?
As you've mentioned in comments, SoftwareSerial esp(6,7) works on UNO but not on Mega. That should be hint big enough to google the Arduino SoftwareSerial reference page, particularly the Limitation section.
In short, unlike UNO the Mega doesn't have Pin Change Interrupt capability on all pins. So you can't have Rx pin on pin 6.
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