Hi. Im making a gps tracker with a SIM808 module, and I want to be able to send a class 0 SMS to a number if the user presses the tft display. I want to add this feature in case of an emergency. I searched online that if you add "0001" at the start of the message, it becomes a class 0 message, but it didnt work, it just arrives"0001 LAT:92...." and if the user has the phone in dont disturb mode (sorry if I named it wrong), the user wont read the message instantly. Is there a way to format the message so it becames a class 0 message? Thanks
(Just for clearing, I'm thinking about using this ONLY with my cellphone, I don't plan to disturb other people by doing this)
That's nice but people reading the replies could.....
I'm not able to provide the solution but if You post the formatted code, using code tags, "</>" symbol, Your question will more likely catch the interest of better helpers.
If anyone has any idea how to send the class 0, please notify me personally, I would appreciate it very much!
This is my code until now:
#include "FS.h"
#include "Free_Fonts.h"
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <DFRobot_SIM808.h>
#include <TimeLib.h>
#include <TinyGPS++.h>
#include "SD.h"
#include <iostream>
#include <sstream>
#include <string>
TinyGPSPlus gps;
String dataMessage;
int satelites = 0;
bool escribi = 0;
double ref1;
double ref2;
int ledPin = 4; // LED conectado al pin digital 13
int inPin = 2; // pulsador conectado al pin digital 7
int val = 0; // variable para almacenar el valor leido
float escrib = 0;
int indicador = 0;
char phone[16];
char datetime[24];
bool grabar = false;
bool escribir = false;
#define PHONE_NUMBER "3158140214"
const int offset = -5;
bool mensaje = 0;
char charglobal[32];
time_t prevDisplay = 0;
bool pausar = 0;
TFT_eSPI tft = TFT_eSPI();
#define CALIBRATION_FILE "/TouchCalData1"
#define REPEAT_CAL false
DFRobot_SIM808 sim808(&Serial2);
uint16_t t_x = 0, t_y = 0;
bool pressed;
bool aes = 0;
int buf;
#define MESSAGE_LENGTH 20
char gprsBuffer[64];
char *s = NULL;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(9600,SERIAL_8N1,17,16);
// Initialise the TFT screen
tft.init();
pinMode(ledPin, OUTPUT); // configura el pin digital 13como salida
pinMode(inPin, INPUT); // configura el pin digital 7 como entrada
// Set the rotation before we calibrate
tft.setRotation(1);
drawmenu(3);
while(!SD.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
// Calibrate the touch screen and retrieve the scaling factors
touch_calibrate();
while(!sim808.init()) {
delay(1000);
Serial.print("Sim808 init error\r\n");
drawmenu(1);
}
if( sim808.attachGPS()){
Serial.println("Open the GPS power success");
drawmenu(0);
}
else{
Serial.println("Open the GPS power failure");
drawmenu(2);
}
// Clear the screen
}
void loop() {
// put your main code here, to run repeatedly:
val = 1; // lee el pin de entrada
digitalWrite(ledPin, val);
mensaje = 0;
if(mensaje){
if(!aes){
aes = sim808.detachGPS();
sim808.detachGPS();
Serial.println(aes);
delay(300);
}
if(sim808.readable()){
sim808_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
//Serial.print(gprsBuffer);
//************** Detect the current state of the telephone or SMS ************************
if(NULL != strstr(gprsBuffer,"RING")) {
sim808.answer();
}else if(NULL != (s = strstr(gprsBuffer,"+CMTI: \"SM\""))) { //SMS: $$+CMTI: "SM",24$$
char message[MESSAGE_LENGTH];
int messageIndex = atoi(s+12);
sim808.readSMS(messageIndex, message,MESSAGE_LENGTH);
Serial.print("Recv Message: ");
Serial.println(message);
}
sim808_clean_buffer(gprsBuffer,32);
}
}
else{
if(Serial2.available() == 0){
escribi = 0;
}
while (Serial2.available() > 0){
val = digitalRead(inPin); // lee el pin de entrada
digitalWrite(ledPin, val);
if (gps.encode(Serial2.read())){
val = digitalRead(inPin); // lee el pin de entrada
digitalWrite(ledPin, val);
tft.setFreeFont(FF18);
/*
Serial.print(gps.date.year());
Serial.print("/");
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print(" ");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
*/
setTime(gps.time.hour(),gps.time.minute(), gps.time.second(),gps.date.day(),gps.date.month(), gps.date.year());
adjustTime(offset * SECS_PER_HOUR);
if (timeStatus()!= timeNotSet) {
if (now() != prevDisplay) {
prevDisplay = now();
digitalClockDisplay();
}
}
//Serial.print("latitude :");
//Serial.println(gps.location.lat(),6);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(String(gps.location.lat(),6), 100, 20, GFXFF);
//Serial.print("longitude :");
//Serial.println(gps.location.lng(),6);
tft.setTextColor(TFT_WHITE,TFT_RED);
tft.drawString(String(gps.location.lng(),6), 100, 45, GFXFF);
tft.setTextColor(TFT_WHITE,TFT_BLUE);
//Serial.print("Satelites: ");
satelites = gps.satellites.value();
//Serial.println(satelites);
String satel = String(satelites) + " ";
tft.drawString(satel, 230, 0, GFXFF);
String altura = String(gps.altitude.meters())+" m ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(String(altura), 200, 180, GFXFF);
//Serial.print("speed_kph :");
//Serial.println(gps.speed.kmph());
drawspeed(gps.speed.kmph());
//Serial.print("heading :");
tft.setFreeFont(FF18);
//Serial.println(gps.course.deg());
String depende = String(gps.course.deg())+ " ";
tft.setTextColor(TFT_WHITE,TFT_DARKGREEN);
tft.drawString(depende, 150, 200, GFXFF);
if(grabar){
if(!pausar){
if(registrar(gps.location.lng(),g ps.location.lat())){
//Serial.println("PAUSADO AUTOMATICO");
}
else{
dataMessage = FormatPlacemark(0,gps.speed.kmph(),indicador,gps.location.lng(),gps.location.lat(),gps.course.deg(),gps.altitude.meters(),gps.satellites.value(),0);//bool inicio, float velocidad,int id,float longitud, float latitud, int inclinacion, float altura, int satelites
appendFile(SD,charglobal, dataMessage.c_str(),0);
indicador = indicador + 1;
Serial.println("igri");
}
}
else{
}
}
pressed = tft.getTouch(&t_x, &t_y);
if(pressed){
String non = String(t_x)+":"+String(t_y);
Serial.println(non);
if(t_x>270 && t_x<310){
if(t_y>100 && t_y<130){
Serial.println("AEEEE");
if(!pausar){
dataMessage = FormatPlacemark(0,gps.speed.kmph(),indicador,gps.location.lng(),gps.location.lat(),gps.course.deg(),gps.altitude.meters(),gps.satellites.value(),1);//bool inicio, float velocidad,int id,float longitud, float latitud, int inclinacion, float altura, int satelites
appendFile(SD,charglobal, dataMessage.c_str(),0);
tft.fillRect(270, 100, 30, 30, TFT_YELLOW);
pausar = 1;
delay(200);
}
else if(pausar){
dataMessage = FormatPlacemark(0,gps.speed.kmph(),indicador,gps.location.lng(),gps.location.lat(),gps.course.deg(),gps.altitude.meters(),gps.satellites.value(),2);//bool inicio, float velocidad,int id,float longitud, float latitud, int inclinacion, float altura, int satelites
appendFile(SD,charglobal, dataMessage.c_str(),0);
tft.fillRect(270, 100, 30, 30, TFT_GREEN);
pausar = 0;
delay(200);
}
}
}
if(t_y>200){
if(t_x>190){
String todos = "0001Ubicacion actual: "+String(gps.location.lat(),6)+","+String(gps.location.lng(),6)+"A una altura de: "+String(gps.altitude.meters(),1)+" m Con inclinacion de: "+String(gps.course.deg()) +" Grados con una velocidad de "+String(gps.speed.kmph())+"Km/h en la fecha "+String(day())+"/"+String(month())+"/"+String(year())+" A la hora "+String(hour())+":"+String(minute());
Serial.println(todos);
sim808.detachGPS();
sim808.detachGPS();
int str_len = todos.length() + 1;
char char_array[str_len];
todos.toCharArray(char_array, str_len);
bool nou = sim808.sendSMS((char *)PHONE_NUMBER, (char *)char_array);
if(nou){Serial.println("SI");}else{Serial.println("no");
}
sim808.attachGPS();
sim808.attachGPS();
}
else if(t_x<40){
Serial.println("AE");
if(grabar){
tft.fillRect(270, 100, 30, 30, TFT_RED);
grabar = 0;
String fin = "</Document>\n</kml>";
int str5_len = fin.length() + 1;
char header4[str5_len];
fin.toCharArray(header4, str5_len);
appendFile(SD, charglobal, header4,1);
delay(200);
}
else if(!grabar){
tft.fillRect(270, 100, 30, 30, TFT_GREEN);
String eo = "/Doc"+String(day())+String(month())+String(year())+String(hour())+String(minute())+String(second())+".txt";
int str_len = eo.length() + 1;
char char_array[str_len];
eo.toCharArray(charglobal, str_len);
File file = SD.open(charglobal);
if(!file) {
Serial.println("File doesn't exist");
Serial.println("Creating file...");
writeFile(SD, char_array,"A");
String header = "<?xml version='1.0' encoding='utf-8'?>\n <kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:kml='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>\n<Document>\n <name>Lector de GPS</name>\n <Style id='def_arrow'>\n <IconStyle>\n <Icon><href>http://digihud.mrchops.org/img/kml/track_arrow.png</href> </Icon>\n </IconStyle>\n </Style>\n";
int str_lon = header.length() + 1;
char header2[str_lon];
header.toCharArray(header2, str_lon);
appendFile(SD, charglobal, header2,0);
//String eo = FormatPlacemark(0,0)
}
else {
Serial.println("File already exists");
}
file.close();
grabar = 1;
}
}
}
}
if(!val){
mensaje = 1;
}
}
if(!val){
mensaje = 1;
}
}
if(!val){
mensaje = 1;
}
}
}
void drawmenu(int sis){
if(sis==1){
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF18); // Select the font
tft.drawString("Inicializando SIM808", 100, 100, GFXFF);
}
if(sis==3){
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF18); // Select the font
tft.drawString("Inicializando tarjeta SD", 10, 100, GFXFF);
}
else if (sis==2){
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF18); // Select the font
tft.drawString("Error", 100, 100, GFXFF);
}
else if(sis==0){
tft.fillScreen(TFT_BLACK);
tft.fillRect(0, 0, 320, 20, TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setFreeFont(FF18); // Select the font
tft.drawString("Lector GPS", 0, 0, GFXFF);
tft.drawString("ST:", 180, 0, GFXFF);
tft.fillRect(0,20,320,25,TFT_BLACK);
tft.fillRect(0,45,320,25,TFT_RED);
tft.drawString("Lat:", 0, 20, GFXFF);
tft.drawString("Lon:", 0, 45, GFXFF);
tft.drawRect(0,70,320,90,TFT_YELLOW);
tft.drawString("Km/h", 265, 140, GFXFF);
tft.fillRect(0, 160, 320, 40, TFT_BLACK);
tft.fillRect(0, 200, 320, 40, TFT_DARKGREEN);
tft.drawString("Altura:", 0, 180, GFXFF);
tft.drawString("Inclinacion:", 0, 200, GFXFF);
tft.drawString("Enviar MSG", 190, 220, GFXFF);
tft.drawString("Grabar", 0, 220, GFXFF);
tft.fillRect(270, 100, 30, 30, TFT_RED);
}
}
void touch_calibrate()
{
uint16_t calData[5];
uint8_t calDataOK = 0;
// check file system exists
if (!SPIFFS.begin()) {
Serial.println("Formating file system");
SPIFFS.format();
SPIFFS.begin();
}
// check if calibration file exists and size is correct
if (SPIFFS.exists(CALIBRATION_FILE)) {
if (REPEAT_CAL)
{
// Delete if we want to re-calibrate
SPIFFS.remove(CALIBRATION_FILE);
}
else
{
File f = SPIFFS.open(CALIBRATION_FILE, "r");
if (f) {
if (f.readBytes((char *)calData, 14) == 14)
calDataOK = 1;
f.close();
}
}
}
if (calDataOK && !REPEAT_CAL) {
// calibration data valid
tft.setTouch(calData);
} else {
// data not valid so recalibrate
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
if (REPEAT_CAL) {
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.println("Set REPEAT_CAL to false to stop this running again!");
}
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
// store data
File f = SPIFFS.open(CALIBRATION_FILE, "w");
if (f) {
f.write((const unsigned char *)calData, 14);
f.close();
}
}
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void digitalClockDisplay(){
// digital clock display of the time
/*
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
*/
String hor = String(hour())+":"+String(minute())+":"+String(second())+" ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(hor, 20, 160, GFXFF);
String fec = String(day())+"/"+String(month())+"/"+String(year())+" ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(fec, 160, 160, GFXFF);
}
void drawspeed(float si){
int speed = int(si);
tft.setFreeFont(FF24);
String dato;
if(speed<10){
dato = String(speed)+" ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(dato, 10, 80, GFXFF);
}
else if(speed<100){
dato = String(speed)+" ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(dato, 10, 80, GFXFF);
}
else if(speed<1000){
dato = String(speed)+" ";
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(dato, 10, 80, GFXFF);
}
}
void dibujarpal(int ant){
tft.fillRect(30,220,40,20,TFT_DARKGREEN);
if(ant == 0){
tft.fillRect(34,235,6,7,TFT_WHITE);
tft.fillRect(43,230,6,12,TFT_WHITE);
tft.fillRect(52,225,6,20,TFT_WHITE);
}
if(ant == 1){
tft.fillRect(34,235,6,7,TFT_WHITE);
tft.fillRect(43,230,6,12,TFT_WHITE);
}
if(ant == 2){
tft.fillRect(34,235,6,7,TFT_WHITE);
}
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message,bool nose) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
if(nose){
file.close();
}
}
char* header(){
std::cout << "<?xml version='1.0' encoding='utf-8'?>\n";
//std::cout << "<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:kml='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
return 0;
}
String FormatPlacemark(bool inicio, float velocidad,int id,float longitud, float latitud, int inclinacion, float altura, int satelites,int pausar)
{
String header;
String color;
if (inicio){
header = "<name>INICIO</name>";
}
else{
header = " <name>"+String(velocidad)+"</name>";
}
if(pausar == 1){
header = "<name>PAUSADO</name>";
color = "#ff1100";
}
if(pausar == 2){
header = "<name>REESTABLECIDO</name>";
color = "#0cf20c";
}
String descripcion = "<![CDATA[Fecha: "+String(day())+"/"+String(month())+"/"+String(year())+" A la hora "+String(hour())+":"+String(minute())+"<br />Longitud: "+String(longitud,6)+"<br />Latitud: "+String(latitud,6)+"<br />Velocidad: "+String(velocidad)+"Km/h<br />Inclinacion: "+String(inclinacion)+"Grados<br />Altura: "+String(altura)+" metros<br /> Satelites vistos: "+String(satelites)+"]]>";
String ss(" <Placemark id='"+String(id)+"'>\n");
ss+= String(header);
ss+= String(
" <description>"+String(descripcion)+"</description>\n");
ss +=( " <TimeStamp></TimeStamp>\n"
" <styleUrl>#def_arrow</styleUrl>\n"
" <Style>\n"
" <IconStyle>\n"
" <color>"+color+"</color>\n"
" <heading>"+String(inclinacion)+"</heading>\n"
" <gx:headingMode>northUp</gx:headingMode>\n"
" </IconStyle>\n"
" </Style>\n"
" <ExtendedData>\n"
" <Data name='velocidad'>\n"
" <value>"+String(velocidad)+"Km/h</value>\n"
" </Data>\n"
" <Data name='Inclinacion'>\n"
" <value>"+String(inclinacion)+"</value>\n"
" </Data>\n"
" </ExtendedData>\n"
" <Point>\n"
" <coordinates>"+String(longitud,6)+","+String(latitud,6)+"</coordinates>\n"
" </Point>\n"
" </Placemark>\n");
return ss;
}
bool registrar(double val1,double val2){
bool resf;
if(val1 == ref1 && val2 == ref2 ){
resf = 1;
}
else{
resf = 0;
}
ref1 = val1;
ref2 = val2;
String ae = "PS 1: "+String(ref1,6)+" PS 2: "+String(ref2,6)+" FN: "+String(resf);
return resf;
}
Perfect! I cross my fingers for a knowing helper to step in, likely using personal message to avoid the public the knowledge.
God may hear you! Thanks for the help!
Thanks. God hears me and I hear him.
I think they meant you have to add two BINARY bytes: 0x00 and 0x01. To do that you have to switch your device to "PDU" mode. The 'message' is not just text but a bunch of required header information:
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.