Monday, July 18, 2016

Receive alerts and messages from your Arduino Mega

Picture of Receive alerts and messages from your Arduino Mega
Hi, we had a project that requires integrating the GPRS/GSM Shield -EFCom from ElecFreaks. Searching the internet for information we found out that, there is a lot of information for connecting this shield for Arduino UNO, but when it came to Arduino MEGA, it was always bits and pieces. This inspired us in TechDepot Egypt to create this instructable.
Most probably if you are watching this, that you already know what is needed to do the job, but let's review it here.
You will need:
- 1 x Arduino Mega 2560 similar to this one
- 1 x USB cable for programming, note that the termination must look like this one to work with Mega
- 1 x GPRS/GSM Shield -EFCom from ElecFreaks (the one we had available) and it comes with:
- 1 x Power Supply
- 1 x Antenna
- 2 x Dupont Jumper Cables Male-Female, if you don't know what this is, look here

Step 1: Preparing the Shield

Picture of Preparing the Shield
The first thing to do is to insert a valid SIM card into the proper place at the bottom of the card. Turn the card over, slide the door, insert the card then slide the door back into its place and make sure it was properly closed.
Right after you make sure that the SIM card is properly installed, attach the antenna to the shield. Don't hold the antenna by the black plastic part as the cap may come out. Always hold the antenna from the metal bolt-like base and turn clockwise until it is firmly attached.

Step 2: Plugging the Shield

Picture of Plugging the Shield
Now with the shield prepared we are ready to plug it into the Mega. The key is to match the 8 pins as in the photos above and all will go ok on both sides. Just make sure that there are no shield pins not plugged into the Mega. Look from both sides. All pins should be plugged respectively and inserted all the way.

Step 3: Connecting the Rx and Tx on the shield side

Picture of Connecting the Rx and Tx on the shield side
Now we need to connect the Transmit (Tx) and the Recieve (Rx) link between the shield and the Mega. When using Arduino UNO you don't need to do anything as the shield comes with 2 jumpers (first picture) to configure it to use pins 2 and 3 (UNO serial port pins) for communication with the shield but unfortunately, this is not the case with the Mega. Mega can not use pins 2 and 3 as a serial port. So, instead, there are a number of pins that you can use. If you need to know all the available pins, you can check the Quick Start Guide from ElecFreaks.
Using any 2 pins other than pins 2 and 3 requires you to remove the 2 jumpers and connect the Female end of the 2 Dupont cables as shown in the second picture.

Step 4: Connecting the Tx and Rx on the Arduino Mega side

Picture of Connecting the Tx and Rx on the Arduino Mega side
Each Arduino board has one or more hardware serial port(s) and some pins that can act as a software serial port(s). In the case of the Arduino Mega and in order to enhance the performance we went with using the hardware serial port named (Serial1) and which can be accessed via pins 18 and 19 (first picture).
Now with the shield plugged into the Arduino Mega and the Female end of the Dupont cable is connected to the GSM shield the way we explained in the last step, now:
- Connect the Tx in the GSM shield -> Rx1 in the Arduino Mega board (pin 19)
- Connect the Rx in the GSM shield -> Tx1 in the Arduino Mega board (pin 18)
Check the second picture where we used different colors for the wires to help you get the idea and help us not to get confused :)

Step 5: Get ready to program your Arduino

Picture of Get ready to program your Arduino
Congratulations, you can now grab your USB cable and connect the Mega to you PC or MAC and finally start programming.
When you first connect the USB, the red LED of PWR in the shield will light, then the blue LED named STA will light and the NET LED will blink fast. Few seconds and only the red PWR will be the only LED lit. Don't worry, this absolutely normal. This shield is power hungry, and the power coming from the USB can not provide the needed power to do its job. Later on, we will connect the power adaptor that came with the shield and all will be fine on the power level. Hopefully :).
Now start your Arduino IDE on your laptop, go to Tools, choose the Port you are connected to and make sure to choose the Arduino Mega as the board you are intending to work on.

Step 6: Finally the Code is here :)

const int shieldPwr = 6; // This is the pin to use to switch the shield programmatically (pin 9 in UNO)
void setup() {
// put your setup code here, to run once
pinMode(shieldPwr, OUTPUT); // Prepare the pin that we are going to use to switch the shield ON
digitalWrite(shieldPwr, HIGH); // This will simulate as if you've pressed the shield power button
delay(2000); // Pressed for 2 seconds. By now the STA blue LED should be ON and NET LED blinking fast
digitalWrite(shieldPwr, LOW); // Now this simulates releasing the power button
delay(10000); // Wait 10 seconds for GSM connection. NET LED should now blink every 3 seconds or so
Serial1.begin(9600); // Prepare the Seial(1) connection between Arduino and GSM Shield
sendSMS("Hello from your Arduino!"); // We've created a function "sendSMS" that you can call to send SMS
// Check your mobile now! :))
}
void sendSMS(String str) {
Serial1.println("AT+CMGF=1"); //Because we want to send the SMS in text mode
delay(1000);
Serial1.println("AT+CMGS=\"************\""); // Replace * with the Number to send to
delay(1000);
Serial1.println(str); // The text for the message
delay(1000);
Serial1.write(byte(26)); // sends ctrl+z end of message
delay(1000);
}
void loop() {
// Nothing to put here in this example :)
}

Step 7: The last step

Picture of The last step
Now upload the sketch to your Arduino Mega. You can leave the USB connected or remove it. Grab the power supply that came with the shield (first picture) and plug its barrel connector into the Arduino's Mega power socket (as shown in picture 2).
Now and very carefully connect the adapter to the wall mains. Please always take care when dealing with mains electricity.
You should shortly receive the greeting message we typed in the code on your mobile phone.
Hope this helps. :)

0 comments:

Post a Comment