I have a problem which is driving me crazy .I want to use dfmini player to play Songs on the arduino and output it on a 8 ohm 0.5W Speaker , and nothing is working .I am using the DFRobotDFPlayerMini Library code as is but nothing is playing at all .Here is a photo of my connections.
Image Here
I apologize if my photo is not clear as i don't have time for software drawing, and i didnt use a breadboard as actually the DF Player is slightly bigger than the holes.
The Arduino Pins Used is 11,10 and 5V and 2 Pins for the GND
Here is the Code of the DFRobot Library used as is .
The Files on the SD Card are in a folder mp3 and written as 0001.mp3 0002.mp3 ..etc
When i Connect and try it out . The Blue Led on MP3 Player doesn't light up , and on the Serial Monitor Baud 115200 :
DFRobot DFPlayer Mini
DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
DFPlayer Mini online.
and on Serial Monitor Baud 9600:
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ܙf⸮⸮⸮⸮⸮⸮⸮⸮
Would be very thankful if anyone can solve this problem.
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(10); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
static unsigned long timer = millis();
if (millis() - timer > 3000) {
timer = millis();
myDFPlayer.next(); //Play next mp3 every 3 second.
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
Try changing:
if (!myDFPlayer.begin(mySoftwareSerial, false))
to:
if (!myDFPlayer.begin(mySoftwareSerial, true, false))
The problem as it appeared was in the Arduino Cable , Make Sure that the cable itself is working properly
Related
I am new to microcontrollers programming. I just got my hands on the arduino uno board. I am trying to build the popular "bluetooth robot car" using the uno, adafruit motorshield v1 and HC-05 bluetooth module. My code attached below compiles and uploads without any error or warning. While not stacking the shield and not attaching the bluetooth module, instead seeing "Robot remote control mode" on the serial monitor, I see some "Rbnld tedisjvv shhsjj hshhs". When I attach and connect the bluetooth module and send a command 'R', instead of seeing 'R' on the monitor I see 'R' followed by an up-pointing arrow. I have searched for help on the web on similar problems but none has helped. I have tried other codes for serial communication(like blinking an led when 'B' is read from serial) and it worked perfectly.I need help.
// include the Adafruit motor v1 library
#include <AFMotor.h>
AF_DCMotor MotorFR(1); // Motor for drive Front Right on M1
AF_DCMotor MotorFL(2); // Motor for drive Front Left on M2
AF_DCMotor MotorBL(3); // Motor for drive Back Left on M3
AF_DCMotor MotorBR(4); // Motor for drive Back Right on M4
const int buzPin = 2; // set digital pin 2 as buzzer pin (use active buzzer)
const int ledPin = A5; // set digital pin A5 as LED pin (use super bright LED)
int valSpeed = 255;
void setup(){
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("*Robot Remote Control Mode*");
pinMode(buzPin, OUTPUT); // sets the buzzer pin as an Output
pinMode(ledPin, OUTPUT); // sets the LED pin as an Output
// Set the speed to start, from 0 (off) to 255 (max speed)
MotorFL.setSpeed(valSpeed);
MotorFR.setSpeed(valSpeed);
MotorBL.setSpeed(valSpeed);
MotorBR.setSpeed(valSpeed);
// turn off motor
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
}
void loop() {
while (Serial.available() > 0) {
char command = Serial.read(); // gets one byte from serial buffer
Serial.println(command);
switch(command){
case 'F': // move forward
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'B': // move backward
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'R': // turn right
SetSpeed(valSpeed);
MotorFL.run(FORWARD);
MotorFR.run(BACKWARD);
MotorBL.run(FORWARD);
MotorBR.run(BACKWARD);
break;
case 'L': // turn left
SetSpeed(valSpeed);
MotorFL.run(BACKWARD);
MotorFR.run(FORWARD);
MotorBL.run(BACKWARD);
MotorBR.run(FORWARD);
break;
case 'G': // forward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'H': // backward left
MotorFL.setSpeed(valSpeed/4);
MotorBL.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'I': // forward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(FORWARD);
MotorFR.run(FORWARD);
MotorBL.run(FORWARD);
MotorBR.run(FORWARD);
break;
case 'J': // backward right
MotorFR.setSpeed(valSpeed/4);
MotorBR.setSpeed(valSpeed/4);
MotorFL.run(BACKWARD);
MotorFR.run(BACKWARD);
MotorBL.run(BACKWARD);
MotorBR.run(BACKWARD);
break;
case 'S': // stop
MotorFL.run(RELEASE);
MotorFR.run(RELEASE);
MotorBL.run(RELEASE);
MotorBR.run(RELEASE);
break;
case 'V': // beep buzzer
digitalWrite(buzPin, HIGH);
delay(150);
digitalWrite(buzPin, LOW);
delay(100);
digitalWrite(buzPin, HIGH);
delay(250);
digitalWrite(buzPin, LOW);
break;
case 'W': // turn light on
digitalWrite(ledPin, HIGH);
break;
case 'w': // turn light off
digitalWrite(ledPin, LOW);
break;
case '0': // set speed motor to 0 (min)
SetSpeed(0);
break;
case '1': // set speed motor to 30
SetSpeed(30);
break;
case '2': // set speed motor to 55
SetSpeed(55);
break;
case '3': // set speed motor to 80
SetSpeed(80);
break;
case '4': // set speed motor to 105
SetSpeed(105);
break;
case '5': // set speed motor to 130
SetSpeed(130);
break;
case '6': // set speed motor to 155
SetSpeed(155);
break;
case '7': // set speed motor to 180
SetSpeed(180);
break;
case '8': // set speed motor to 205
SetSpeed(205);
break;
case '9': // set speed motor to 230
SetSpeed(230);
break;
case 'q': // set speed motor to 255 (max)
SetSpeed(255);
break;
}
}
}
// function for setting speed of motors
void SetSpeed(int val){
valSpeed = val;
MotorFL.setSpeed(val);
MotorFR.setSpeed(val);
MotorBL.setSpeed(val);
MotorBR.setSpeed(val);
}```
I'm desperately looking for a solution for my SIM800l v2. The network LED just keeps blinking every second. It does not restart but it keeps blinking and not picking up a signal.
I tried powering up using a laptop USB port but it does not solve it. I also used a charger adapter that is 5V and 2A but still does not solve the problem. I also tried changing the sim card but still does not solve the problem.
Here is the Wiring diagram that I followed:
Credits to miliohm.com
Code:
SoftwareSerial sim(10, 11);
int _timeout;
String _buffer;
String number = "+6281222329xxx"; //-> change with your number
void setup() {
//delay(7000); //delay for 7 seconds to make sure the modules get the signal
Serial.begin(9600);
_buffer.reserve(50);
Serial.println("Sistem Started...");
sim.begin(9600);
delay(1000);
Serial.println("Type s to send an SMS, r to receive an SMS, and c to make a call");
}
void loop() {
if (Serial.available() > 0)
switch (Serial.read()) {
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
case 'c':
callNumber();
break;
}
if (sim.available() > 0)
Serial.write(sim.read());
}
void SendMessage() {
//Serial.println ("Sending Message");
sim.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(200);
//Serial.println ("Set SMS Number");
sim.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
delay(200);
String SMS = "Hello, how are you? greetings from miliohm.com admin";
sim.println(SMS);
delay(100);
sim.println((char)26);// ASCII code of CTRL+Z
delay(200);
_buffer = _readSerial();
}
void RecieveMessage() {
Serial.println ("SIM800L Read an SMS");
sim.println("AT+CMGF=1");
delay(200);
sim.println("AT+CNMI=1,2,0,0,0"); // AT Command to receive a live SMS
delay(200);
Serial.write ("Unread Message done");
}
String _readSerial() {
_timeout = 0;
while (!sim.available() && _timeout < 12000) {
delay(13);
_timeout++;
}
if (sim.available()) {
return sim.readString();
}
}
void callNumber() {
sim.print (F("ATD"));
sim.print (number);
sim.print (F(";\r\n"));
_buffer = _readSerial();
Serial.println(_buffer);
}
I was also stuck on this for ages. Got a DC-DC converter to ensure I had the right voltage. I connected caps on both sides of the DC-DC. I tried most everything to no avail.
My issue in the end was the inefficiency of breadboards and jumper wires. So if you are using a breadboard or jumper wires, shorten them as much as possible, like 2-3cm to ensure that enough current can be delivered to the SIM800L module
Try to connect a cap as close as possible between Vcc and GND. If the module connects to the network after you entered the SIM pin the current rises and may causes a voltage drop.
I am doing an autonomous car project, I need manual control as well as an autonomous function, so the manual control is done through wif using "gesture control" and for the autonomous control I want to send the location data via Bluetooth, I will be choosing between both, using a toggle switch connected to ground and one of the pins. I went through the inbuilt wifi Bluetooth switch program but I have no idea how to modify it. I am still okay if I can send data through the HTTP request but, then I have to connect it to a network right? But again I don't have any idea, how to write the code to switch between the two networks.I am using an ESP32 devkit
This is the inbuilt example code for Bluetooth switch
#include "WiFi.h"
#define STA_SSID "your-ssid"
#define STA_PASS "your-pass"
#define AP_SSID "esp32"
enum { STEP_BTON, STEP_BTOFF, STEP_STA, STEP_AP, STEP_AP_STA, STEP_OFF, STEP_BT_STA, STEP_END };
void onButton(){
static uint32_t step = STEP_BTON;
switch(step){
case STEP_BTON://BT Only
Serial.println("** Starting BT");
btStart();
break;
case STEP_BTOFF://All Off
Serial.println("** Stopping BT");
btStop();
break;
case STEP_STA://STA Only
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
break;
case STEP_AP://AP Only
Serial.println("** Stopping STA");
WiFi.mode(WIFI_AP);
Serial.println("** Starting AP");
WiFi.softAP(AP_SSID);
break;
case STEP_AP_STA://AP+STA
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
break;
case STEP_OFF://All Off
Serial.println("** Stopping WiFi");
WiFi.mode(WIFI_OFF);
break;
case STEP_BT_STA://BT+STA
Serial.println("** Starting STA+BT");
WiFi.begin(STA_SSID, STA_PASS);
btStart();
break;
case STEP_END://All Off
Serial.println("** Stopping WiFi+BT");
WiFi.mode(WIFI_OFF);
btStop();
break;
default:
break;
}
if(step == STEP_END){
step = STEP_BTON;
} else {
step++;
}
//little debounce
delay(100);
}
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case SYSTEM_EVENT_AP_START:
Serial.println("AP Started");
WiFi.softAPsetHostname(AP_SSID);
break;
case SYSTEM_EVENT_AP_STOP:
Serial.println("AP Stopped");
break;
case SYSTEM_EVENT_STA_START:
Serial.println("STA Started");
WiFi.setHostname(AP_SSID);
break;
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println("STA Connected");
WiFi.enableIpV6();
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.print("STA IPv4: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("STA Disconnected");
break;
case SYSTEM_EVENT_STA_STOP:
Serial.println("STA Stopped");
break;
default:
break;
}
}
void setup() {
Serial.begin(115200);
pinMode(0, INPUT_PULLUP);
WiFi.onEvent(WiFiEvent);
Serial.print("ESP32 SDK: ");
Serial.println(ESP.getSdkVersion());
Serial.println("Press the button to select the next mode");
}
void loop() {
static uint8_t lastPinState = 1;
uint8_t pinState = digitalRead(0);
if(!pinState && lastPinState){
onButton();
}
lastPinState = pinState;
}
Below is part of the main code i use, i want to modify it so i can either switch between bluetooth and esp now or between espnow and http
*/
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <TinyGPS++.h> // Tiny GPS Plus Library
#define CHANNEL 4
uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
struct __attribute__((packed)) DataStruct {
//char text[32];
int x;
int y;
unsigned long time;
};
DataStruct myData;
//*****************************************************************************************************
// GPS Locations
unsigned long Distance_To_Home; // variable for storing the distance to destination
int ac =0; // GPS array counter
int wpCount = 0; // GPS waypoint counter
double Home_LATarray[50]; // variable for storing the destination Latitude - Only Programmed for 5 waypoint
double Home_LONarray[50]; // variable for storing the destination Longitude - up to 50 waypoints
int increment = 0;
#define autopilot 13
void gesturecontroll();
void getGPS();
void getCompass();
void setWaypoint();
void move();
int blueToothVal;
void setup()
{ Serial.begin(9600); // Serial 0 is for communication with the computer
S2.begin(9600); // Serial 2 is for GPS communication at 9600 baud - DO NOT MODIFY - Ublox Neo 6m
Serial.println("ESPNow/Basic/Slave Example");
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP);
// configure device AP mode
// This is the mac address of the Slave in AP Mode
esp_wifi_set_mac(ESP_IF_WIFI_STA, &mac[0]);
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
// Init ESPNow with a fallback logic
if (esp_now_init()!=0) {
Serial.println("*** ESP_Now init failed");
while(true) {};
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.
esp_now_register_recv_cb(OnDataRecv);
Serial.print("Aheloiioi");
// Extras////////////////////////////////////////////////////////////////////////////////////
pinMode(autopilot, INPUT);
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
memcpy(&myData, data, sizeof(myData));
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Recv from: "); Serial.println(macStr);
Serial.print("Last Packet Recv Data: ");
Serial.println(myData.x);
Serial.println(myData.y);
Serial.println("");
Serial.println();
//move();
Serial.println();
}
//********************************************************************************************************
// Main Loop
void loop()
{ if (autopilot == HIGH)
{
move(); // going for manual control
}
else
{
getGPS(); // Update the GPS location
getCompass(); // Update the CompaSerial Heading
Ping(); // Use at your own discretion, this is not fully tested
}
}
I could have tried if all these initial setups came under an "if statement" in the void loop, but these initial setups come in the void setup,so i have no idea on how to proced
I think this example need hardware also like toggle switch between GPIO 0 & ground on top of the code these comments are there.
//Sketch shows how to switch between Wi-Fi and Bluetooth or use both
// Button is attached between GPIO 0 and GND and modes are switched with each press
this example code as wifi.h library included but "BluetoothSerial.h" library is not included I don't it works properly in real time without this "BluetoothSerial.h" library
About terminology in ESP32 Wi-Fi go through it:- https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html
I think first prepare hardware as given in example then start coding.
I have a ESP8266(ESP-12-E) and a DFPlayer Mini
I am running the sample application :
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(2, 5); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(10); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
static unsigned long timer = millis();
if (millis() - timer > 3000) {
timer = millis();
myDFPlayer.next(); //Play next mp3 every 3 second.
}
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
I have the following pins setup on the DFPlayer Mini counting down the left of the board 1-8:
1 - +5v
2 - ESP-12-E GPIO 2 - Software serial TX
3 - ESP-12-E GPIO 5 - Software serial RX
4 -
5 -
6 - Speaker +
7 - GND
8 - Speaker -
The Serial monitor shows:
DFRobot DFPlayer Mini Demo Initializing DFPlayer ... (May take 3~5
seconds) Unable to begin:
1.Please recheck the connection!
2.Please insert the SD card!
Soft WDT reset
ctx: cont sp: 3ffefc30 end: 3ffefe10 offset: 01b0
stack>>> 3ffefde0: 3ffeeab0 3ffeea88 3ffeed2c 402020c4 3ffefdf0: 3fffdad0 00000000 3ffeeddc 40202fc4 3ffefe00: feefeffe feefeffe
3ffeedf0 40100710 <<
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
The stack trace formatted is:
Decoding 3 results
0x402020c4: setup at /Users/user/Documents/Arduino/dfplayer_simple/dfplayer_simple.ino line 42
0x40202fc4: loop_wrapper at /Users/user/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/cores/esp8266/core_esp8266_main.cpp line 57
0x40100710: cont_norm at /Users/user/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/cores/esp8266/cont.S line 109
I see no activity on the DFPlayer Mini LED, and am not sure where to go from here.
A tad late, but using myDFPlayer.begin(mySoftwareSerial, false) as mentioned has been our work around on every 'duino i've tried (esp8266, esp32, mega, uno, teensy, etc). Might as well just eliminate that check code entirely, as once you tell it no ack, it will pass the test even if no DFPlayer is connected.
On my NodeMCU ESP-12E, because of the label vs. GPIO aliasing, instead of
SoftwareSerial mySoftwareSerial(5,6); //didn't work
I had to use
SoftwareSerial mySoftwareSerial(D5,D6); //worked great!
Using the Arduino IDE, NodeMCU 1.0 ESP-12E board. Weird how the samples online that I came across didn't show this detail. Here's a link to pinout references and other good info for a variety of esp8266 boards.
https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
Servo myservo;
Password password = Password( "1234" ); //password to unlock box, can be changed
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };
int x=0;
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() //if i add sms(); function it workssss
{
Serial.begin(9600); //Start a Serial COM
Serial.println(F("ARDUINO SECURITY SYSTEM V1.0"));
Serial.print(F("Checking GSM COM..."));
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+XXXXX","Your Home Security system is powered up"));
Serial.println(F("Good To GO!!"));
}
else
{
Serial.println(F("Could not connect to GSM modem"));
}
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent);//add an event listener for this keypad
}
void loop(){
keypad.getKey();
myservo.write(0);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter : ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case 'A': checkPassword(); delay(1); break;
case 'C': checkPassword(); delay(1); break;
case 'D': checkPassword(); delay(1); break;
case 'B': password.reset(); delay(1); break;
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open box
Serial.println("Accepted");
Serial.write(254);delay(50);
//Add code to run if it works
myservo.write(5); //160deg
digitalWrite(11, HIGH);//turn on
delay(2000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else
{
Serial.println("Denied"); //if passwords wrong keep box locked
Serial.write(254);delay(10);
x++;
if(x==3)
//add code to run if it did not work
{
myservo.write(0);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
if (gsm.begin(9600))
{
(sms.SendSMS("+XXXXX","Your Home Security system is being bridged"));
Serial.println("USER WARNED");
}
}
}
}
;
}
In the picture the same code doesn't seem to work when I place the lines
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+8613668914901","Your Home Security system is being bridged"));
but this lines work great inside the void setup function.
How can I fix this problem? Inside the void setup the sketch works fine but when I also put the code in the function CheckPassword it doesn't send SMS.
I also tried to create a function let's say void SMS and call it in the checkPassword function but it doesn't solve the problem, btw the same function when called in the void setup works fine.
You are supposed to initialize the gsm only once, in the setup function.
In your code you attempt to initialize it again in the checkPassword method, and that is obviously not going to work.
Thus you should remove the line gsm.begin(9600) from the checkPassword function.
UPDATE 1:
In your scheme you reserve the pins 9, 8, 7, 6, 5, 4, 3, 2 for the Keyboard. However, at the same time you reserve pins 2, 3 for your GSM module (see GSM.cpp):
#define _GSM_TXPIN_ 2
#define _GSM_RXPIN_ 3
Using the same pins for multiple purposes can often result (if not done properly) in undefined behaviour which in the best scenario means that your sketch isn't doing what it is supposed to do, and in the worst scenario it might damage your components.
You are already using pins 0, 1 for the Serial library, but according to your code the pins 10, 11, 13 should still be free if you want to relocate the existing pins to your components.
Notice also the following warnings inside the GSM library:
[3] My shield doesn't work. Why?
Check this steps and then ask for support on the issues' page on google
code.
1) SIM900 and SIM908 require about 1 A during the hardest tasks.
You should have an external power source that can provide about
1 A at 8-12 V
2) If the SIM90X blinks (1 Hz) for some seconds and then turn off,
probably it's a communication's problem. Check the switch/jumpers
for Serial communication.
3) Arduino Uno has 2 KB of RAM. Library takes about 80% (we are working
to reduce it), if you use more than 20% left, Arduino can restart
or print on serial strange strings.
4) Check the jumper of communication, power source (battery or externel) and charge.