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?
Related
I am transmitting the CSV file saved on SD card using STM32F103 on UART line to ESP8266, The file reading and UART transmission snippet is as below.
While loop of the STM32f103 for read/transmit file.
Using the Software Serial code on ESP8266 code I can read the transmitted string from the STM32F103 and the actual length of the transmitted string is 59 but I am getting following output on the Serial monitor
ESP8266 serial monitor output
The code for Software is as follows
#include <SoftwareSerial.h>
#include <Arduino.h>
#include <stdio.h>
#include <string.h>
using namespace std;
SoftwareSerial s; //RX TX
String str= "";
String str_tx = "";
char char_array[60];
double buff[24];
int str_len = 0;
double val;
char* token;
//char* rest = char_array;
int i=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(115200, SWSERIAL_8N1, 13, 15, false, 128); //115200, SWSERIAL_8N1, 13, 15, false, 512
pinMode(13, INPUT);
pinMode(15, OUTPUT);
Serial.println("in setup 1");
Serial.println("in setup 2");
Serial.println("in setup 3");
}
void loop() {
// put your main code here, to run repeatedly:2
while(s.available() >0)
{
char ch=s.read();
if(ch != '\n')
str.concat(ch);
if( ch =='\n' )
{
/*Serial.println("------------------------------------");
Serial.println(char_array);
Serial.println("------------------------------------");*/
Serial.println(str);
str_len = str.length();
Serial.println(str_len);
str.toCharArray(char_array, str_len);
Serial.println(char_array);
//strcpy(char_array, str.c_str());
memset(char_array, 0, sizeof(char_array));
str="";
//break;
}
/*
str = s.readStringUntil('\n');
Serial.println(str);
str_len = str.length();
Serial.println(str_len);
//Serial.println(str.c_str());
//strcpy(char_array, str.c_str());
//str.toCharArray(char_array, str_len);
//Serial.println(char_array);*/
}
}
I have used the above code for the reading but the length is showing different after the 1st string which goes around 125 and because of that first time its getting copied into char buffer but further its showing empty.
Would appreciate if anyone could throw some light on what's causing this issue and how to solve it, Thankyou!
Software serial does not work very well on ESP8266. Use lower bitrate. It may be helpful but does not completely solve the problem. I had the same problem using GSM modules.
I used the hardware serial (Serial pin 1 , 3) for this communication and used a software serial to print out the debug information. It make it really hard to work with; because the port is used for programming; you need to add 470R resistor to prevent any damage to your module. Every time you change the code, you need to connect the programmer, program the module then you need to remove the programmer so the board can communicate with your STM32. But using the hardware serial was very stable and solved my problem.
I'm trying to save my received SMS into a string but it doesn't work. This is my error message :
recive_0_1:50:28: error: invalid conversion from 'int' to 'char' [-fpermissive] exit status 1 invalid conversion from 'int' to 'char'
[-fpermissive] This report would have more information with "Show
verbose output during compilation" option enabled in File ->
Preferences.**
#include <SoftwareSerial.h>
#include <string.h>
char message[160]; // max size of an SMS
char* text;
String data;
//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(4, 5); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
mySerial.begin(9600);
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+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
}
void updateSerial()
{
// char c;
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
//char data=Serial.read();
//Serial.println(data,DEC);
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
text=mySerial.read();
Serial.print(text);
//}
}
}
It looks like you're running into problems from trying to convert integers to characters. I use the below code to read from my SIM card to my SIM900 device:
1st: my software serial is defined and my setup script run:
SoftwareSerial gprsSerial(7, 8);
void setup()
{
pinMode(13, OUTPUT);
gprsSerial.begin(19200);
Serial.begin(19200);
delay(200);
// set up your AT COMMANDS HERE - I HAVE NOT INCLUDED THESE
}
Then I run my loop function as follows, calling the additional functions specified thereafter, :
void loop() {
readSMS();
delay(2000);
}
void toSerial(){
delay(200);
if(gprsSerial.available()>0){
textMessage = gprsSerial.readString();
delay(100);
Serial.print(textMessage);
}
}
void readSMS() {
textMessage = "";
gprsSerial.print("AT+CSQ\r");
toSerial();
gprsSerial.print("AT+CMGF=1\r");
toSerial();
gprsSerial.println("AT+CNMI=2,2,0,0,0\r");
delay(2000);
toSerial();
}
With the above you should not get any data type conversion hassles.
I am doing a project where I need to send data from ultrasonic sensor wirelessly present in one arduino to other arduino where I need these values in Serial monitor. But the problem is I cannot able to send these values through bluetooth. I tried to send one character, it is appearing in serial monitor.. But when I tried to the same for integer values it is not appearing in serial monitor.
I have configured Master and Slave modes for the Bluetooth. I have uploaded the image of the code which I am using to send these values. Please help me on this. Thanks in advance .
code
//# transmitting end
#define trigPin 12
#define echoPin 11
void setup() {
Serial.begin(38400); // Default communication rate of the Bluetooth module
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration;
float distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance,2); // Sends floatValue
delay(500);
}
//# receving end
#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BTSerial(10, 11);
int data=0;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(38400);
BTSerial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
int number;
if(Serial.available() > 0){ // Checks data is from the serial port
data = BTSerial.read(); // Reads the data from the serial port
//analogWrite(led,data);
delay(10);
//Serial.println(data);
}
Serial.println(data);
}
I need integer values at the serial monitor. But there I am getting some symbols like ?/<>..
From the Arduino reference, Serial.read() only reads the first available byte available in the Serial buffer. As an int is coded on 8 bytes, I would say that you need to read the incoming bytes sequentially in order to get the full value.
Maybe you can implement this by putting (Serial.available() > 0) in a while loop, concatenate the values you get in a char[8] for instance and then convert this char to a integer value.
Also, beware that you are sending floats and not int.
Thanks for the help..!
I modified the code in the receiver end to get the float values from the transmitter.. Here is my modified code
#include <SoftwareSerial.h>
int bluetoothTx = 10;
int bluetoothRx = 11;
String content; //content buffer to concatenate characters
char character; //To store single character
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup(){
bluetooth.begin(38400);
Serial.begin(9600);
}
void loop(){
bluetooth();
}
void bluetooth(){ //
while(bluetooth.available()){
character = bluetooth.read();
content.concat(character);
if(character == '\r'){ // find if there is carriage return
Serial.print(content); //display content (Use baud rate 9600)
content = ""; //clear buffer
Serial.println();
}
}
}
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.
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);
}
}