Sim800l save input as a string - string

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.

Related

Arduino to PC secure bluetooth connection(cryptography)

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?

How to Get the RSSI value for Multiple devices while scan by ESP32

I am using the below code to get the RSSI value. I am able to get the value successfully for one Device at a time. But I am confused about how to get the RSSI value for multiple devices while scanning by ESP32.
The below code snippet I am using.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 10; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
Serial.print(" RSSI: ");
Serial.println(advertisedDevice.getRSSI());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, true);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
// you can access first device with foundDevices.getDevice(0).getAddress()
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
Can anyone advice how Do I get the multiple RSSI value while scanning devices

How to send float values from one bluetooth module to other(HC 05)

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();
}
}
}

trying to decode RS485 protocol

I am trying to decode signal that two devices are using over RS485.
I captured signal, from which i got baud rate (57600, one bit is 17,3us long):
Screenshot of the signal
This signal is BIN: 0000000000010010001001111110011110011011111110...
Then i wrote simple capture program in arduino:
void setup() {
Serial.begin(57600);
Serial1.begin(57600);
}
void loop() {
if (Serial1.available()){
byte temp=Serial1.read();
if(temp==0){
Serial.println();
}else{
Serial.print(temp,HEX);
}
}
}
Which got me following output:
https://justpaste.it/6sw5g
What is my next step in decoding this communication?

How to convert the incoming characters coming from gsm module into a string?

Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.
#include <SoftwareSerial.h>
#include <String.h>
char inchar[255];
SoftwareSerial cell(2,3);
int led1 = 22;
#define powerOn 4
int i;
//char comparestring[160];
char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command ended with null terminator
void setup()
{
// ilagay sa loob ng setup
digitalWrite(powerOn, HIGH);
delay(1500);
digitalWrite(powerOn, LOW);
delay(5000);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.begin(9600);
cell.b-egin(9600);
delay(30000);
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("GSM SHIELD IS NOW OK AND READY");
}
void loop()
{
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
{
digitalWrite(led1, HIGH);
cell.write("AT+CMGS=\"");
cell.write("09267955775");
cell.write("\"\r");
delay(1000);
cell.write("\nTerminal Monitoring System");
delay(1000);
cell.write(0x1A); // End the SMS with a control-z
}
else
{
Serial.println("\nInvalid Keyword! Type ?");
dig-italWrite(led1, LOW);
}
}
}
these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
inchar is a string so try using strcmp(inchar,command) for comparing the two.

Resources