I want to perform a bluetooth communication between 2 Arduino with HC05(master) and HC06 (slave). I success with the pairing of the 2 modules, but when i send a byte that i read from a potentiometer, the slave receive another value, that can be 128, -1, 248. Below there are the Arduino's codes
Arduino master HC05
#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 potpin = 0; // analog pin used to connect the potentiometer
void setup()
{
// start the serial communication with the host computer
Serial.begin(9600);
Serial.println("Arduino with HC-05 is ready");
// start communication with the HC-05 using 9600
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
BTserial.println(analogRead(potpin));
delay(100);
Serial.println(analogRead(potpin));
}
Arduino slave HC06
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
SoftwareSerial slave(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 c = 0;
int val;
void setup()
{
// start the serial communication with the host computer
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// start communication with the HC-05 using 9600
slave.begin(9600);
Serial.println("BTserial started at 9600");
myservo.attach(9);
}
void loop()
{
if (slave.available())
{
val= slave.read();
Serial.println(val);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(15);
}
}
Thank you for every answer
In slave sketch you should replace slave.read() with parseInt().
The read() function will read a single byte. When master sends the integer value potpin=130, the function println(potpin) will convert it to 3 bytes (coded as ascii character), and will send them. On the slave side you have to read all incoming bytes, stored in string, and convert string in integral variable. parseInt() will do that in a single row.
Related
I'm using an Arduino Uno to build a smoke detection system, which works. Since I have to do an IoT project, I have to establish a secure connection with a device (I thought with my smartphone, or my PC), and I'm using a Bluetooth module HC-05 to do it. The idea is:
Send the smoke sensor data to Arduino IDE, encrypt them and display the encrypted data to the serial (and it works)
Connect Arduino to my smartphone using HC-05 and the app "Makerslab BT Demo" (already done)
Decrypt the value of the sensor when I press "1" on the app and display it;
Decrypt the value of the sensor when there's danger and display a "danger message".
(that's what I have to do now).
That's my code on Arduino IDE:
#include <AES.h>
#include <AESLib.h>
#include <AES_config.h>
#include <xbase64.h>
#include <SoftwareSerial.h>
SoftwareSerial BT(1,0);
#define VCC2 5
int smokeA0 = A0;
int buzzer = 11;
AES aes;
byte cipher[400];
char b64[400];
float sensorValue;
//char a;
void do_encrypt(String msg, String key_str, String iv_str){
byte iv[16];
memcpy(iv,(byte*)iv_str.c_str(),16);
int blen=base64_encode(b64,(char*)msg.c_str(),msg.length());
aes.calc_size_n_pad(blen);
int len=aes.get_size(); //zero padding
byte plain_p[len];
for(int i=0;i<blen;++i) plain_p[i]=b64[i];
for(int i=blen;i<len;++i) plain_p[i]='\0';
// l'AES-128-CBC encryption
int blocks = len/16;
aes.set_key((byte *)key_str.c_str(), 16);
aes.cbc_encrypt(plain_p, cipher, blocks, iv);
// use base64 encoder to encode the encrypted data:
base64_encode(b64,(char *)cipher,len);
Serial.println(String((char *)b64));
}
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
pinMode(VCC2, OUTPUT);
digitalWrite(VCC2, HIGH);
BT.begin(9600);
BT.println(F("Hi! Press "1" to know the sensor value."));
Serial.begin(115200);
Serial.println(F("gas sensor is warming up!"));
delay(2000); //allow the sensor to warm up
noTone(buzzer);
}
void loop() {
String key_str="aaaaaaaaaaaaaaaa"; //16 bytes
String iv_str="aaaaaaaaaaaaaaaa"; //16 bytes
sensorValue = analogRead(smokeA0);
String msg = String(sensorValue, 3);
do_encrypt(msg,key_str,iv_str);
/* if(BT.available()){
a=(BT.read());
if(a=='1'){
Serial.print(F("Air quality: "));
}
}*/
if(sensorValue > 300){
Serial.print(F(" | Danger!"));
BT.print(F(" | Danger!"));
tone(buzzer,1000,2000);
}
else {
noTone(buzzer);
}
Serial.println(F(""));
delay(2000);
}
I'm not sure to use the right security protocol (AES), but encryption works. How I can decrypt data?
wondering if anyone would know why is this happening.
I have HM-10 Bluetooth module that is connected to Arduino. I use Serial Bluetooth Terminal for communicating with the HM-10 module.
The code below works perfectly fine with Arduino UNO,
the LED is turned ON/OFF
I receive messages in Serial monitor
I receive messages on my mobile phone
However if I use the same sketch and the same scheme with Arduino NANO
the LED is turned ON/OFF
I receive messages in Serial monitor
I do NOT receive any message on my mobile phone. I tried almost all other pins including the TX1 & RX0pins but without any luck. It simply doesn't send any data to the RXD pin of the HM-10 module.
Is this some kind of limitation of Arduino Nano or do I have a faulty one?
// Arduino Bluetooth modul HM-10
#define RX 11
#define TX 10
#define pinLED 13
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(TX, RX);
void setup() {
Serial.begin(9600);
Serial.println("Arduino on");
bluetooth.begin(9600);
bluetooth.print("Arduino ON");
pinMode(pinLED, OUTPUT);
}
void loop() {
byte BluetoothData;
if (bluetooth.available() > 0) {
BluetoothData=bluetooth.read();
switch (BluetoothData) {
case '0':
digitalWrite(pinLED, LOW);
Serial.println("LED turned OFF");
bluetooth.println("LED turned OFF");
break;
case '1':
digitalWrite(pinLED, HIGH);
Serial.println("LED turned ON");
bluetooth.println("LED turned ON");
break;
default:
Serial.println("Unknown command");
bluetooth.println("Unknown command");
}
}
delay(100);
}
Arduino Uno R3, HC-06. There's no response to the AT commands on the serial monitor.
I want to establish Bluetooth communication between PC and Arduino. At first I had to change the baud rate from 9600 to 115200, and the AT+BAUD8 command was working well. But after that change AT commands are not working. I cannot see any respond on the serial monitor.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // Bluetooth module Tx:Digital 2 Rx:Digital 3
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
Serial.println("ATcommand"); //ATcommand Start
}
void loop() {
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
I used the above simple code and also I've already checked the Tx, Rx pins.
What I want to do in the end is Bluetooth communication with a PC as a master and an Arduino as a slave.
Is it possible to upload code to Arduino through Bluetooth?
This code works for me:
#include <SoftwareSerial.h>
SoftwareSerial BT(10,11); // TX to pin_10. RX to pin_11 of Arduino.
void setup() {
Serial.begin(9600);
BT.begin(9600);
}
void loop() {
if(Serial.available()) {
String command = Serial.readStringUntil('\n');
Serial.println(command);
BT.print(command);
}
if (BT.available()){
String retorno = BT.readStringUntil('\n');
Serial.println(retorno);
}
}
Serial Monitor with New line.
AT
AT+VERSION
AT+NAMEmy_blue
AT+BAUDx
I can send 1byte decimal number (0-256) easily over arduino, but I couldn't make it for 2 byte number.
the code below here I used for sending data between 0-256, this is works great.
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo.attach(9);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()> 0 )
{
int servopos = bluetooth.read();
Serial.println(servopos);
myservo.write(servopos);
}
}
I tried to program for 2 byte number , but I couldn't make it successfully , MSB and LSB data are not shifting properly . any help regarding this code will be much help for me go ahead .
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo;
int bluetoothTx = 7;
int bluetoothRx = 8;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo.attach(9);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
int servopos = bluetooth.read() << 8;
servopos |= bluetooth.read();
Serial.println(servopos);
myservo.write(servopos);
}
}
I'm having trouble communicating between Tera Term and an Arduino Mega over a bluetooth connection. My goal is to be able to set up the Mega so it can be later used to exchange text commands with a C++ application. Using the code I've found on this site, I can use the Arduino IDE Serial Monitor to send text to the Tera Term terminal, but I cannot send text from the Tera Term terminal to the Arduino. It never recognizes text was sent from the terminal. The bluetooth module I am using is the Bluetooth Mate Gold from SparkFun. The code's purpose is to detect incoming chars and then activate an LED. My code is shown below:
#include <SoftwareSerial.h>
int bluetoothTx = 15;
int bluetoothRx = 14;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(13, OUTPUT);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
}
void loop() {
//Read from bluetooth and write to usb serial
if(bluetooth.available()) {
char toSend = (char)bluetooth.read();
Serial.print(toSend);
flashLED();
}
//Read from usb serial to bluetooth
if(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.print(toSend);
flashLED();
}
}
void flashLED() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
The only thing that seems to work from Tera Term is entering command mode using "$$$." Doing that, I can run commands such as "D." I'm not sure why I can't send chars from Tera Term to the Arduino and have them be read. Any suggestions are appreciated.
I just built the same thing and i had to add a delay of 500 ms before configuring the module. I also had problems with receiving data since i used pin that did not support the PCINT interrupt.
delay(500);
bluetooth.begin(115200);
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);