How to convert the incoming characters coming from gsm module into a string? - 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.

Related

scanning the barcode and control led

I have a serial barcode scanner and Arduino Uno.
I need to configuration..one Correct barcode scanning led on.
my code is
void setup ()
{
Serial.begin (9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if(Serial.available())
{
String command =Serial.readStringUntil('\n');
Serial.println(command);
if(command =="1010007")
{
digitalWrite(3, HIGH);
digitalWrite(4,LOW);}
else
if(command !="1010007")
{digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(3,LOW);}
}}
im scanning barcode 1010007 it showing serial monitor g⸮⸮⸮⸮ like this
how to fix it
plz help
and i try another code it showing correctly barcode,,,,,i scan 1010007 serial monitor also same 1010007
but led not on...code is below,,,
#include <SoftwareSerial.h>
SoftwareSerial barcode = SoftwareSerial(2, 3, true); // RX, TX
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
barcode.begin(9600);
delay(1000);
}
void loop()
{
if(barcode.available())
{
char data = barcode.read();
Serial.write(data);
if(data=="101007")
{
digitalWrite(3, HIGH);
}
}}

Sim800l save input as a 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.

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

Arduino sending SmS using sim800l

how can i write the phone number to the arduino using Sim800l module...
the right code is this
serialSIM800.write("AT+CMGS=\"+639261001204\"\r\n");
i want to to change the phone number to String so that i can send any number...
void send()
{
digitalWrite(led, HIGH);
serialSIM800.begin(9600);
delay(1000);
char s1 = "AT+CMGS=\"+";
char s2= number;
char s3="\"\r\n"";
char sms = s1+s2+s3;
//Set SMS format to ASCII
serialSIM800.write("AT+CMGF=1\r\n");
delay(1000);
//Send new SMS command and message number
serialSIM800.write(sms);
delay(1000);
//Send SMS content
serialSIM800.write("Your student Go inside the School");
delay(1000);
serialSIM800.write((char)26);
delay(250);
}

How to use BLE Shield based on HM-10 bluetooth module?

I'm a new bie on arduino projects. I would like to ask you for some help. I bought a BLE Shield for Arduino from ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html ). They made this shield using Hm-10 Bluetooth module(http://www.jnhuamao.cn/bluetooth.asp?ID=1). Itead Studio has no sample codes using this shield. I have no idea on how to program it or send AT commands from Arduino.
I read the “AT commands” at the data sheet (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) and I tried to send "AT commands” from arduino to BLE shield using this code ( http://arduino.cc/en/Reference/SoftwareSerial ) but I only received the commands back.
Did anybody here ever use this HM-10 bluetooth module ?
I need some arduino sketch for help !
#include <SoftwareSerial.h>
int led = 13;
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] ={4800,9600,14400,19200,28800,38400,57600,115200};
int i = 1;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
while(!Serial){}
Serial.write("AT sent");
delay(500);
bluetooth.write("AT+NAME?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available())
{
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+NAMEFlightline"); //Check Status
delay(500);
while (bluetooth.available()) {
Serial.write((char)bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+CHAR0x2901"); //add charicteristic
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+RELI0");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+SHOW1");
delay(100);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
}
void testAllBaudRates(){
for(int j=0; j<8; j++)
{
bluetooth.begin(baudrate[j]);
delay(100);
Serial.println("boud rate " + String(baudrate[j],DEC) +" i-> "+String(j,DEC));
// Serial.println("");
bluetooth.write("AT");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
Serial.println();
}
delay(100);
}
}
// and now a few blinks of the LED,
// so that we know the program is running
void loop()
{
//Read from bluetooth and write to usb serial
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend == 'x'){
digitalWrite(led,HIGH);
Serial.println("set high");
bluetooth.write("RXOK");
}else if(toSend == 'y'){
digitalWrite(led,LOW);
Serial.println("set low");
bluetooth.write("RXOK");
}
Serial.print(toSend);
}
//Read from usb serial to bluetooth
while(Serial.available())
{
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
}
Have a look at my sketch above I have a few things to point out that I wasted time on.
make sure you have the line
while(!Serial){}
Or you may get have a working shield but miss the responses as the serial monitor is no ready.
remember that you wont get a response from the blue-tooth module, with a command from the Serial Monitor if it is connected to a device. It is connected to the device when the light stops flashing.
if you run this sketch you should get this output
AT sent
OK+Set:3
OK+Get:0x2901 <- this may be blank the first time you run it
OK+Set:Flightline
OK+Set:0x2901
OK+Set:0
OK+Set:1
the most comprehensive list of AT commands can be found here
[All the AT commands and a good explanation][1]
You will need to at Characteristics to the device as I have done here
bluetooth.write("AT+CHAR?");
or you will find it to connect to iOS and Android
If you are connecting to Android use the BluetoothLE Classes not Bluetooth ones.
You can use this sketch with baud rate autodetect to control your HM-10. This is a part of Apploader project that allows to upload to Arduino board over BLE.
This is a little late too, but try the following code, if you send it "AT" it should give you back an "OK":
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}

Resources