Communication entre ARDUINO NANO 33 BLE SENSE? - arduino

I am new and I would really need your experience and your help : Here is my project:
I’m trying to make 2 Arduino nano 33 ble sense communicate:I need to send the accelerations and gyro data from the first one and receive them in the second, How i can did ?
The idea is to do 6 Arduino nano 33 ble sense each associate with an electrochemical technology sensor (UART), and send the data tram to the central board which will retrieve all the informations from the differents sensors via BLE on each board.
Then I will associate with the central card (the one which recovers all the informations of the 6 cards) a LORA module to send the data to a PC (I would like to do it in real time).
Thank you .

Thank you for your answer, I managed to communicate two cards at the same time with the examples of arduino nano
-File → Examples → ArduinoBLE → Peripheral → LED
-File → Examples → ArduinoBLE → Central → LEDControl
But for example I haven't yet known how to transfer the information on the temperature sensor from the first Arduino nano 33 ble sense board and read them on the second
nano 33 ble sense.
Why sensor of Temperature ?
Because after I am going to have another sensor that I am going to associate with the nano 33, that's why In first, I need to try with the temperature or humidity sensor of nano 33. Thank you!

you can find an excellent short tutorial on the link below about connecting two Arduino nano using BLE. One acting as peripheral device and the other as central device.
"In this tutorial, we will learn how to exchange information between two Arduino boards, the Nano 33 BLE and the Nano 33 BLE Sense, through Bluetooth® Low Energy. For this, we will be using the ArduinoBLE library."
code for programming the central device
/*
BLE_Central_Device.ino
This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE Sense
as a central device and looks for a specified service and characteristic in a
peripheral device. If the specified service and characteristic is found in a
peripheral device, the last detected value of the on-board gesture sensor of
the Nano 33 BLE Sense, the APDS9960, is written in the specified characteristic.
The circuit:
- Arduino Nano 33 BLE Sense.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
#include <Arduino_APDS9960.h>
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int gesture = -1;
int oldGestureValue = -1;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!APDS.begin()) {
Serial.println("* Error initializing APDS9960 sensor!");
}
APDS.setGestureSensitivity(80);
if (!BLE.begin()) {
Serial.println("* Starting BLE module failed!");
while (1);
}
BLE.setLocalName("Nano 33 BLE (Central)");
BLE.advertise();
Serial.println("Arduino Nano 33 BLE Sense (Central Device)");
Serial.println(" ");
}
void loop() {
connectToPeripheral();
}
void connectToPeripheral(){
BLEDevice peripheral;
Serial.println("- Discovering peripheral device...");
do
{
BLE.scanForUuid(deviceServiceUuid);
peripheral = BLE.available();
} while (!peripheral);
if (peripheral) {
Serial.println("* Peripheral device found!");
Serial.print("* Device MAC address: ");
Serial.println(peripheral.address());
Serial.print("* Device name: ");
Serial.println(peripheral.localName());
Serial.print("* Advertised service UUID: ");
Serial.println(peripheral.advertisedServiceUuid());
Serial.println(" ");
BLE.stopScan();
controlPeripheral(peripheral);
}
}
void controlPeripheral(BLEDevice peripheral) {
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("* Connected to peripheral device!");
Serial.println(" ");
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
return;
}
Serial.println("- Discovering peripheral device attributes...");
if (peripheral.discoverAttributes()) {
Serial.println("* Peripheral device attributes discovered!");
Serial.println(" ");
} else {
Serial.println("* Peripheral device attributes discovery failed!");
Serial.println(" ");
peripheral.disconnect();
return;
}
BLECharacteristic gestureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);
if (!gestureCharacteristic) {
Serial.println("* Peripheral device does not have gesture_type characteristic!");
peripheral.disconnect();
return;
} else if (!gestureCharacteristic.canWrite()) {
Serial.println("* Peripheral does not have a writable gesture_type characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
gesture = gestureDetectection();
if (oldGestureValue != gesture) {
oldGestureValue = gesture;
Serial.print("* Writing value to gesture_type characteristic: ");
Serial.println(gesture);
gestureCharacteristic.writeValue((byte)gesture);
Serial.println("* Writing value to gesture_type characteristic done!");
Serial.println(" ");
}
}
Serial.println("- Peripheral device disconnected!");
}
int gestureDetectection() {
if (APDS.gestureAvailable()) {
gesture = APDS.readGesture();
switch (gesture) {
case GESTURE_UP:
Serial.println("- UP gesture detected");
break;
case GESTURE_DOWN:
Serial.println("- DOWN gesture detected");
break;
case GESTURE_LEFT:
Serial.println("- LEFT gesture detected");
break;
case GESTURE_RIGHT:
Serial.println("- RIGHT gesture detected");
break;
default:
Serial.println("- No gesture detected");
break;
}
}
return gesture;
}
code for programming the peripheral device
/*
BLE_Peripheral.ino
This program uses the ArduinoBLE library to set-up an Arduino Nano 33 BLE
as a peripheral device and specifies a service and a characteristic. Depending
of the value of the specified characteristic, an on-board LED gets on.
The circuit:
- Arduino Nano 33 BLE.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
enum {
GESTURE_NONE = -1,
GESTURE_UP = 0,
GESTURE_DOWN = 1,
GESTURE_LEFT = 2,
GESTURE_RIGHT = 3
};
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";
int gesture = -1;
BLEService gestureService(deviceServiceUuid);
BLEByteCharacteristic gestureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
if (!BLE.begin()) {
Serial.println("- Starting BLE module failed!");
while (1);
}
BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
BLE.setAdvertisedService(gestureService);
gestureService.addCharacteristic(gestureCharacteristic);
BLE.addService(gestureService);
gestureCharacteristic.writeValue(-1);
BLE.advertise();
Serial.println("Nano 33 BLE (Peripheral Device)");
Serial.println(" ");
}
void loop() {
BLEDevice central = BLE.central();
Serial.println("- Discovering central device...");
delay(500);
if (central) {
Serial.println("* Connected to central device!");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");
while (central.connected()) {
if (gestureCharacteristic.written()) {
gesture = gestureCharacteristic.value();
writeGesture(gesture);
}
}
Serial.println("* Disconnected to central device!");
}
}
void writeGesture(int gesture) {
Serial.println("- Characteristic <gesture_type> has changed!");
switch (gesture) {
case GESTURE_UP:
Serial.println("* Actual value: UP (red LED on)");
Serial.println(" ");
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_DOWN:
Serial.println("* Actual value: DOWN (green LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_LEFT:
Serial.println("* Actual value: LEFT (blue LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
digitalWrite(LED_BUILTIN, LOW);
break;
case GESTURE_RIGHT:
Serial.println("* Actual value: RIGHT (built-in LED on)");
Serial.println(" ");
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
break;
default:
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LED_BUILTIN, LOW);
break;
}
}
you can find the original tutorial here:
https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device
there is also a really good explanation and how to here:
https://ladvien.com/arduino-nano-33-bluetooth-low-energy-setup/

Related

LED not glowing

I a beginner to IoT. I want to glow LED connected to arduino for this I have made connections as described below, also see the image of breadboard attached here.
LED Connections
Connected
Arduino GND to one leg of LED and 3.3V to another leg of LED.
My Arduino program is-
void setup() {
Serial.begin(9600);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.set("LED_STATUS",0);
}
int n = 0;
void loop() {
// get value
n = Firebase.getInt("LED_STATIS");
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(D1,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(D1,LOW);
return;
}
delay(1000);
}
Serial monitor shows LED is ON as shown in screenshot.
Serial Monitor
i think you should declare pinMode in setup, and you put LED in D0
#define LED D0 // Led in NodeMCU at pin GPIO16 (D0).
void setup() {
pinMode(LED, OUTPUT); // LED pin as output.
}
void loop() {
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(LED ,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(LED ,LOW);
return;
}
}
note that it has built-in LED, but its active low, it should glow when logic is 0
and you should using pull-up resistor, because you can't depend current supply from GPIO pin since its only provide 12mA

NRF24L01 with ATTiny and Uno not connecting

I have an ATTiny85 connected to an NRF24L01+ module using this wiring diagram: diagram. The ATTiny85 periodically goes in and out of sleep to send some value to a receiver, an Arduino Uno. If the ATTiny is running off the Arduino power supply (3.3v), everything works correctly. When I run the ATTiny off of a separate CR2032 coin cell that delivers around 3v, the Arduino never receives any data. I have a status LED hooked up to the ATTiny to ensure that the ATTiny is waking correctly, which it is. Here's the code for both:
EDIT:
Connecting it to an external 3.3v not from the Uno makes everything work - why wouldn't the coin cell's voltage work? I think everything is rated below 2.8v, the CR2032 minimum.
ATTiny Code
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define CE_PIN 3
#define CSN_PIN 3 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(1000);
setup_watchdog(6);
}
volatile int watchdog_counter = 0;
ISR(WDT_vect) {
watchdog_counter++;
}
void loop()
{
sleep_mode(); //Go to sleep!
if(watchdog_counter >= 5)
{
digitalWrite(4, HIGH);
watchdog_counter = 0;
payload = 123456;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
digitalWrite(4, LOW);
}
}
//Sleep ATTiny85
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
Receiver Code
#define CE_PIN 7
#define CSN_PIN 8
#include <SPI.h>
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
while (!Serial);
Serial.begin(115200);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openReadingPipe(1, address); // Write to device address 'SimpleNode'
radio.startListening();
Serial.println("Did Setup");
}
void loop(void){
if (radio.available()) {
radio.read( &payload, sizeof(unsigned long) );
if(payload != 0){
Serial.print("Got Payload ");
Serial.println(payload);
}
}
}
Is the problem here that the ATTiny and Uno need to be turned on at the same time to establish a connection, or is it something to do with the battery, or something else entirely? Any help would be appreciated.
I'm experiencing the same problem when running Arduino Nano from a battery.
Nano has a 3.3V pin that I'm using for powering the NRF24L01+ module.
When the voltage from my battery-pack drops under 3.3V, the 3.3V pin voltage also drops. After few minutes, the RF module is not sending any messages.
I fixed the problem temporarily by routing the battery through a 12V step-up regulator that I bought earlier for a different project. These 12V then go to the "UN" pin on Nano which accepts 6-20V. This setup works nicely but is definitely not optimal.
Therefore I'm planning to use a 3.3V step-up regulator such as Pololu 3.3V Step-Up Voltage Regulator U1V11F3 which, according to the supplier, can efficiently generate 3.3V from input voltages as low as 0.5V.
I think this might be helpful to your project as well.

Issue while connecting ESP8266 with Arduino Mega. It always says "Module have no response."

I am having Issue while connecting ESP8266 with Arduino Mega. It always says "Module have no response."
Please check and correct me if I am wrong anywhere.
I am using below code and and wiring diagram is
My Code:
//#include <SoftwareSerial.h>
//use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).
#define SSID "SopraSteria"
#define PASS "1234567890"
#define DST_IP "220.181.111.85" //baidu.com
//SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
//serial 2 is to esp8266
Serial2.begin(9600);//9600 (mine), 57600, 115200
Serial2.setTimeout(2000);
//serial 0 is to usb
Serial.begin(115200);
while(!Serial);
while(!Serial2);
//dbgSerial.begin(9600); //can't be faster than 19200 for softserial
//dbgSerial.println("ESP8266 Demo");
Serial.println("ESP8266 Demo on Mega2560");
while(Serial2.available()>0)
Serial2.read();
delay(1000);
//test if the module is ready
Serial2.println("AT+RST");
//delay(1000);
//delay(1000);
Serial.println("Resetting module");
Serial2.flush();
//if(Serial2.find("ready"))
if(Serial2.find("Ready")||Serial2.find("ready"))
{
//dbgSerial.println("Module is ready");
Serial.println("Module is ready");
}
else
{
//dbgSerial.println("Module have no response.");
Serial.println("Module have no response.");
//while(1);
}
delay(1000);
//connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){
//while(1);
Serial.println("Not Connected.");
}
delay(1000);
//print the ip addr
Serial2.println("AT+CIFSR");
Serial.println("ip address:");
while (Serial2.available())
Serial.write(Serial2.read());
//set the single connection mode
Serial2.println("AT+CIPMUX=0");
}
void loop()
{
//connectWiFi();
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
Serial2.println(cmd);
Serial.println(cmd);
Serial.println(cmd);
if(Serial2.find("Error")) return;
cmd = "GET / HTTP/1.0\r\n\r\n";
Serial2.print("AT+CIPSEND=");
Serial2.println(cmd.length());
if(Serial2.find(">"))
{
//dbgSerial.print(">");
Serial.print(">");
}else
{
Serial2.println("AT+CIPCLOSE");
//dbgSerial.println("connect timeout");
Serial.println("connect timeout");
delay(1000);
return;
}
Serial2.print(cmd);
delay(2000);
//Serial.find("+IPD");
while (Serial2.available())
{
char c = Serial2.read();
//dbgSerial.write(c);
Serial.write(c);
//if(c=='\r') dbgSerial.print('\n');
if(c=='\r') Serial.print('\n');
}
//dbgSerial.println("====");
Serial.println("====");
delay(1000);
}
boolean connectWiFi()
{
Serial2.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
//dbgSerial.println(cmd);
Serial2.println(cmd);
Serial.println(cmd);
delay(2000);
if(Serial2.find("OK"))
{
//dbgSerial.println("OK, Connected to WiFi.");
Serial.println("OK, Connected to WiFi.");
return true;
}else
{
//dbgSerial.println("Can not connect to the WiFi.");
Serial.println("Can not connect to the WiFi.");
return false;
}
}
From the schema I can see, that device uses 3V logic (I guess it from tension divider)? In that case if you communicate with 5V logic, then you should use 5v/3v buffers on Tx and Rx. This is my guess and my advice:
Always debug communication problems with oscilloscope.
Without this you simply cannot know what is happening.
Also make some other debugging. You assume, that device responds with ready or Ready, but try to print this out, what it actually really gave on output. Otherwise it's impossible for us to debug your piece of hardware without your hardware in the hands.
Also please make a note, that this is not appropriate forum for this kind of questions as there is https://arduino.stackexchange.com/.
Sorry #DawidPi thats not completly correct.
Yes it´s right thats not opimal to do it with a voltage divider. But this is not the problem. To solve the problem with the 3.3 TX will work (surely at 9600 baud --> I did it already like this)
Your problem is the power of your Arduino board. The ESP needs much current. Your board will not support this high current. You have to supply your ESP wih an external supply. I can recommend a fix voltage regulator like the LF33CV. Youse it with two condensators and it will work :-)
Wiring like here: .
If you use a LF33CV your UE should be between 4.3 and 40V. For testing I used the 5V of my USB-port. That will also work.
Try starting your ESP like this:
#define pin_reset 2 //Connect to RST pin of ESP8266
bool start()
{
pinMode(PinReset, OUTPUT);
delay(1);
pinMode(PinReset, INPUT);
delay(1000);
if (Serial.find("Ready"))
{
delay(1000);
return true;
}
else
{
return false;
}
}

Receiving SMS using GSM and controlling LED using Arduino

Has someone come up with a solution with the above stated problem?
We are using Arduino Duemilanove and SIM 900 GSM module (http://robokits.co.in/shop/index.php?main_page=product_info&products_id=303)
We've tried to work on the similar problem of lightning LEDs from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on. E.g. #a1b1c1d1 will switch on all the LEDs.
When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the LEDs lighten up. But if we send the sms with having content "#a1b1c1d1", we don't see any function of LEDs.
It would be great if anyone can give some guidance about the same.
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If #a1b1c1d1 comes as sms, all LEDs should light up.
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
//first led
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
//Second led
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
// Third led
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
//Fourth led
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
First do not use delay
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
This is neither necessary nor reliable. Instead of waiting some random time, you can check the network status with AT+CFUN and/or AT+COPS. If the GSM module is already attached to a network when you open the serial connection, it is a waste of time waiting like that. And if is not attached you should wait explicitly for that to happen (polling CFUN/COPS or enabling AT+CREG), otherwise you risk waiting too short time. See the 27.007 specification for more information for those commands.
Second do not use delay
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Please do not write code like this. See this answer on why using delay is such a bad idea, and this answer for suggestion to how to handle properly.

How to read an SMS from Arduino and get LED to switch on or off

#include <SoftwareSerial.h>
char inchar; //Will hold the incoming character from the serial port.
SoftwareSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
int led1 = A2;
void setup()
{
// Prepare the digital output pins
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH);
//Initialize GSM module serial port for communication.
cell.begin(19200);
delay(30000); // Give time for GSM module to register on network, etc.
cell.println("AT+CMGF=1"); // Set SMS mode to text
delay(200);
cell.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If a character comes in from the cellular module...
if(cell.available() >0)
{
delay(10);
inchar=cell.read();
if (inchar=='a')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
delay(10);
}
cell.println("AT+CMGD=1,4"); // Delete all SMS
}
}
This is the code for receiving SMSes from the cellular network. I am using the Arduino Gboard with SIM900. There is no error in the code, but the LED on the board doesn't switch on or off in response to an SMS.
Why?
Here's a fully functional code for sending a command thru SMS using Arduino and GSM, it will also reply the state of the light.
#include <SoftwareSerial.h>
SoftwareSerial GPRS(10, 11);
String textMessage;
String lampState;
const int relay = 12; //If you're using a relay to switch, if not reverse all HIGH and LOW on the code
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // The current state of the light is ON
Serial.begin(9600);
GPRS.begin(9600);
delay(5000);
Serial.print("GPRS ready...\r\n");
GPRS.print("AT+CMGF=1\r\n");
delay(1000);
GPRS.print("AT+CNMI=2,2,0,0,0\r\n");
delay(1000);
}
void loop(){
if(GPRS.available()>0){
textMessage = GPRS.readString();
Serial.print(textMessage);
delay(10);
}
if(textMessage.indexOf("ON")>=0){ //If you sent "ON" the lights will turn on
// Turn on relay and save current state
digitalWrite(relay, HIGH);
lampState = "ON";
Serial.println("Lamp set to ON\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched ON.\r");
GPRS.write( 0x1a );
delay(1000);
}
if(textMessage.indexOf("OFF")>=0){
// Turn off relay and save current state
digitalWrite(relay, LOW);
lampState = "OFF";
Serial.println("Lamp set to OFF\r\n");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); /// RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp was finally switched OFF.\r");
GPRS.write( 0x1a );
delay(1000);
}
if(textMessage.indexOf("STATUS")>=0){
String message = "Lamp is " + lampState;
GPRS.print("AT+CMGF=1");
delay(1000);
Serial.println("Lamp state resquest");
textMessage = "";
GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
delay(500);
GPRS.print("Lamp is currently ");
GPRS.println(lampState ? "ON" : "OFF"); // This is to show if the light is currently switched on or off
GPRS.write( 0x1a );
delay(1000);
}
}
Change
AT+CNMI=3,3,0,0
to:
AT+CNMI=2,2,0,0,0
The simplest way is the best way.
// if You use SoftwareSerial lib, declare object for GSM
SoftwareSerial gsm(8,9); // TX, RX
void setup(){
// initialise UART and GSM communication between Arduino and modem
Serial.begin(115200);
gsm.begin(115200);
// wait 5-10sec. for modem whitch must connect to the network
delay(5000);
// configure modem - remember! modem didn't remeber Your's configuration!
gsm.print("at+cmgf=1\r"); // use full functionality (calls, sms, gprs) - see app note
gsm.print("at+clip=1\r"); // enable presentation number
gsm.print("at+cscs=\"GSM\"\r"); // configure sms as standard text messages
gsm.print("at+cnmi=1,2,0,0,0\r"); // show received sms and store in sim (probobly, I don't compre this settings with app note but it's working :)
}
void loop(){
String response = gsmAnswer();
if(response.indexOf("+CMT:") > 0 ) { // SMS arrived
// Now You can parse Your Message, if You wont controll only LED, just write
if(response.indexOf("LED ON") > 0) {
digitalWrite(LED_PIN, HIGH); // enable it
}else if(response.indexOf("LED OFF") > 0) {
digitalWrite(LED_PIN, LOW); // turn off
}
delay(1000);
}
}
String gsmAnswer(){
String answer;
while(!gsm.available());
while(gsm.available()){
delay(5);
if(Serial.available() > 0){
char s = (char)gsm.read();
answer += s;
}
}
return answer;
}
One think more, incomming sms has the following format:
+CMT: "+48xxxxxxxxx","","17/07/07,21:57:04+08"
Test of arrived messages
You should first know exactly what the response is before attempting to parse it.
Try something simple like the following code (note: untested!) to get a feeling of what you should look for:
void loop() {
if(cell.available() > 0) {
char ch = cell.read();
Serial.print(ch);
}
}
My guess is you'll see more than just a '0' or a '1' :)
void loop() {
while(cell.available() > 0) {inchar = cell.read(); readString+=c;delay(1);} ///can be a delay up to (10) so you can get a clear reading
Serial.print(readString); /// Declare a string " String readString; "
for (i=0; i<200; i++){ /// Serch for the txt you sent up to (200) times it depends how long your ""readString" is
if(readString.substring(i,i+4)=="RING"){ //// I am looking for the word RING sent from my phone
digitalWrite(13,HIGH);
break;
}
}
}
this will help you find the specific word ir your reading (text message)

Resources