Ligne 119 : | Ligne 119 : | ||
<br /> | <br /> | ||
<code>#include <SoftwareSerial.h></code> | <code>#include <SoftwareSerial.h></code> | ||
+ | |||
<code>SoftwareSerial mySerial(10, 11);</code> | <code>SoftwareSerial mySerial(10, 11);</code> | ||
+ | |||
<code>void setup() {</code> | <code>void setup() {</code> | ||
+ | |||
<code>Serial.begin(9600);</code> | <code>Serial.begin(9600);</code> | ||
+ | |||
<code>mySerial.begin(9600);</code> | <code>mySerial.begin(9600);</code> | ||
+ | |||
<code>}</code> | <code>}</code> | ||
+ | |||
<code>void loop() {</code> | <code>void loop() {</code> | ||
− | <code>Serial.println("AT"); | + | |
+ | <code>Serial.println("AT");</code> | ||
+ | |||
<code>if (mySerial.available()) {</code> | <code>if (mySerial.available()) {</code> | ||
+ | |||
<code>Serial.write(mySerial.read());</code> | <code>Serial.write(mySerial.read());</code> | ||
+ | |||
<code>}</code> | <code>}</code> | ||
+ | |||
<code>if (Serial.available()) {</code> | <code>if (Serial.available()) {</code> | ||
+ | |||
<code>mySerial.write(Serial.read());</code> | <code>mySerial.write(Serial.read());</code> | ||
+ | |||
<code>}</code> | <code>}</code> | ||
+ | |||
<code>delay(500);</code> | <code>delay(500);</code> | ||
− | <code>}</code | + | |
− | + | <code>}</code></translate> | |
}} | }} | ||
{{Tuto Step | {{Tuto Step | ||
|Step_Title=<translate>Testing Commands -</translate> | |Step_Title=<translate>Testing Commands -</translate> | ||
− | |Step_Content=<translate>* '''AT''' - Check serial communication with module | + | |Step_Content=<nowiki><translate>* </nowiki>'''AT''' - Check serial communication with module |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}} | }} | ||
{{Tuto Step | {{Tuto Step |
Auteur Akshayan Sinha | Dernière modification 15/08/2023 par Akshayansinha
Pas encore d'image
Creation
In 2022, over 1 million theft cases, which is a 7% rise compared to 2021, have become a great concern in India. With a population of more than 1.4 billion, you never know whom to trust apart from your own self.
Hence, we are left with only one solution - Track Your Belongings
With the increased availability of devices to access the internet, we all need an automated system, that keeps us updated on our physical product and informs us if it has been moved from its designated location.
But, how do we track them? And how should one get an alert of a theft? We shall see and build a project on that.
A Fence is a physical boundary, which is used to restrict kids and dogs from exiting the border Boundaries provide a layer of security, which also restricts entry from outside. But these boundaries need to be maintained heavily. And once an object has moved out or stolen, then the theft cannot be further located.
This brings us to a solution, which is geofencing a physical device attached to our belonging. This belonging would be a mere part of the IoT - Internet of 'Things'.
The above pictorial is a sample of Geofence in a public area. Best part? Since it is on a virtual map, having a virtual fence would have no effect on whether you own the area. This area could be your Home, Office, favorite Coffee Shop, or even parlor.
But before we get started -
You must check out PCBWAY for ordering PCBs online for cheap!
You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop.
To be able to have global connectivity and uninterrupted internet, the ideal network protocol is cellular technology. With a GPS Module attached to it, it is just perfect.
This amazing combination can only be carried by an integrated device. And at the cheapest, we considered SIM808 Module + GPS as the primary module to continue with this project. Let us look more into its features and specifications -
SIM808 GSM :
SIM808 GPS :
With all these features, it is nearly a handheld portable phone. Yes, you can make a phone call too :P
One grieving concern is, whether going ahead with a smaller size module would fit the portability requirements - Even if a SIM800 module was taken into consideration, for a prolonged secure network that requires more uptime it is a much safer option to go ahead with its successor. Hence, the SIM808 module was chosen for this project. Even though based on availability and price we can always go ahead with the SIM800.
Let us first understand the module and how to get started with a SIM module -
Prologue:
We'll use Arduino UNO to use it as a USB TTL device and send AT commands to SIM808 x module to test the working of the module using PUTTY or Arduino IDE’s Serial Monitor.
1. Make following connections > GND of both modules connected to each other.
2. (For PuTTY) TXD of SIM808 connect with TX1 (PIN 1) of Arduino UNO. Similarly RXD ->RX0.
3. (For Arduino) TXD of SIM808 connect with (PIN 10) of Arduino UNO. Similarly RXD ->11.
4. Connect the Arduino UNO to Arduino IDE and upload a default code that does not interface the serial monitor. Eg: Blink LED sketch.
5. Press Normal button for 2 seconds and leave - the Device will start after that. (Different from power sliding switch)
1. Install PuTTY Application software from https://www.putty.org/
2. Change 'Connection Type' to 'Serial'.
3. Enter Serial port on PuTTY. eg: COM8 > With baudrate as '9600'.
4. Click on 'Open' to open the serial terminal
1. Use the below code -
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
Serial.println("AT");
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
delay(500);
}
<translate>* AT - Check serial communication with module
1. Communication between Arduino & SIM808
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 6);
It sets up a SoftwareSerial instance named mySerial
using digital pins 5 (RX) and 6 (TX) on the Arduino, which will be used to communicate with the SIM808 module.
2. Function Prototypes and Global Variables
void(* resetFunc) (void) = 0;
void GPS_data();
void check_GPS();
void sendSMS();
char frame[256];
byte GNSSrunstatus;
byte Fixstatus;
char UTCdatetime[15];
char latitude[10];
float latitude_val;
char longitude[11];
float longitude_val;
long lat_val;
long lon_val;
long lat_high;
long lat_low;
long lon_high;
long lon_low;
int lat_check;
int lon_check;
char altitude[8];
char speedOTG[6];
char course[6];
char fixmode[1];
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
3. Initialization of Arduino with Cellular Device
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial.println("AT\r");
updateSerial();
mySerial.println("AT+CSQ");
updateSerial();
mySerial.println("AT+CCID");
updateSerial();
mySerial.println("AT+CPIN?");
updateSerial();
GPS_data();
lat_high = lat_val + 900;
lat_low = lat_val - 900;
lon_high = lon_val + 900;
lon_low = lon_val - 900;
}
4. Hardware - Software Serial Interconnection
void updateSerial()
{
delay(500);
while (Serial.available())
mySerial.write(Serial.read());
}
while(mySerial.available())
{
Serial.write(mySerial.read());
}
}
5. GPS Module Initialization
void GPS_data(){
mySerial.println("AT+CGNSPWR=1");
updateSerial();
mySerial.println("AT+CGNSSEQ=\"RMC\"");
updateSerial();
mySerial.println("AT+CGNSINF");
check_GPS();
}
Next, let us call check_GPS() function to update the current GPS data.
6. Update GPS Data
void check_GPS(){
...
}
This function includes the main section of the whole project, which requests for the current GPS location, and then checks whether it is within the Geo Fence limits. Let us break down the code even further!
7. Initialization of Local Variables for GPS
int8_t counter, answer;
long previous;
counter = 0;
answer = 0;
memset(frame, '\0', sizeof(frame));
previous = millis();
int8_t counter, answer;
: Two variables to keep track of loop iterations and whether the expected response is received.long previous;
: Holds the previous time value for timeout calculation.previous = millis();
: Initialize current time in milliseconds.char frame[256];
: An array to store received characters for constructing the response frame.void *memset(void *str, int c, size_t n)
8. Check for Response on UART
do {
if (mySerial.available() != 0) {
frame[counter] = mySerial.read();
counter++;
if (strstr(frame, "OK") != NULL)
{
answer = 1;
}
}
}
while ((answer == 0) && ((millis() - previous) < 2000));
This code repeatedly reads characters from the mySerial interface to construct a response frame. It checks if the response contains "OK" to set the answer flag. The loop continues until either "OK" is found or a timeout of 2000 milliseconds is reached. This ensures the program waits for the expected response before proceeding.
9. Parse the String for GPS data
frame[counter - 3] = '\0';
strtok(frame, ": ");
GNSSrunstatus = atoi(strtok(NULL, ","));
Fixstatus = atoi(strtok(NULL, ","));
strcpy(UTCdatetime, strtok(NULL, "."));
strtok(NULL,",");
strcpy(latitude, strtok(NULL, ","));
strcpy(longitude, strtok(NULL, ","));
strcpy(altitude, strtok(NULL, ","));
This code takes the string data in theframe and then copies it to the variables like GNSSrunstatus, Fixstatus, UTCdatetime, lat, long & altitude.
byte GNSSrunstatus, Fixstatus;
: Byte variables to store GNSS run status and fix status.char UTCdatetime[15];
: A character array to store UTC date and time.char latitude[10], longitude[11], altitude[8];
: Character arrays to store latitude, longitude, and altitude data.10. Print & Store the Data in the MESSAGE String
if (Fixstatus != 0){
Serial.println("GPS Data received");
Serial.print("Runstatus: ");
Serial.println(GNSSrunstatus);
Serial.print("Fixstatus: ");
Serial.println(Fixstatus);
Serial.print("UTCdatetime: ");
Serial.println(UTCdatetime);
Serial.print("latitude: ");
Serial.println(latitude);
Serial.print("longitude: ");
Serial.println(longitude);
Serial.print("altitude: ");
Serial.println(altitude);
latitude_val = atof(latitude);
lat_val = latitude_val*1000000;
Serial.println(lat_val);
longitude_val = atof(longitude);
lon_val = longitude_val*1000000;
Serial.println(lat_high);
.
.
.
If Fixstatus does not return '0', it means we have the complete GPS data. And so it can be further used according to our project.
11. Setting the SafeHouse and Alert Logic
if (lat_high == 0){
sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nAltitude : %s km\n\nInitial Position of the Module\n\nhttp://maps.google.com/maps?q=%s,%s\n", latitude, longitude, altitude, latitude, longitude);
sendSMS();
}
else{
lat_check = inRange(lat_val, lat_high, lat_low);
lon_check = inRange(lon_val, lon_high, lon_low);
Serial.print(lat_check);
Serial.println(lon_check);
if (!(lat_check)){
sprintf(MESSAGE, "Latitude : %s\nLongitude : %s\nAltitude : %s km\n\nALERT!! Module is outside the GeoFence\n\nhttp://maps.google.com/maps?q=%s,%s\n", latitude, longitude, altitude, latitude, longitude);
sendSMS();
}
}
}
else if ((Fixstatus == 0) && (lat_high == 0)){
Serial.println("Resetting Arduino to check GPS data");
delay(5000);
resetFunc();
}
}
12. Send GPS data to Another SIM number
void sendSMS()
{
Serial.println("Start to send message ...");
Serial.println(MESSAGE);
Serial.println(phone);
mySerial.println("AT+CSMP=17,167,0,0");
updateSerial();
mySerial.println("AT+CMGF=1");
updateSerial();
String sim_sms = "AT+CMGS=\"" + phone + "\"\r";
mySerial.println(sim_sms);
updateSerial();
mySerial.print(MESSAGE);
delay(500);
mySerial.print((char)26);
Serial.println("Check SMS");
}
That's it! Now that we have shared the GPS data using SMS to the specified number, it is in fact a great method to safeguard your belongings!
Let us make the connections between Arduino - SIM808 - GPS Module
Initial Position - After we flash the code to the Arduino, the code sends the initial location of the GPS while creating a Geo-Fence around the center of the location.
SMS Message
Latitude : 23.009152
Longitude : 50.252952
Altitude : 86.200 km
Initial Position of the Module
http://maps.google.com/maps?q=23.009152,50.252952
The device keeps checking its location on a loop, indefinitely Till the time the device is within half a kilometer (500m) of the above location, the device is considered within the safe house.
Geo-Fence Breach - The moment, the device moves out of the GeoFence, the device detects it as an alert and sends an SMS immediately.
SMS Message
Latitude : 24.008693
Longitude : 51.253190
Altitude : 85.000 km
ALERT!! Module is outside the GeoFence
http://maps.google.com/maps?q=24.008693,51.253190
Congratulations! You will be now able to build your own device to GeoFence. We hope that this tutorial has provided you with the information you need to build and learn throughout.
If you have any questions or need further assistance, please let me know in the comments!
en none 0 Draft
Vous avez entré un nom de page invalide, avec un ou plusieurs caractères suivants :
< > @ ~ : * € £ ` + = / \ | [ ] { } ; ? #