NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <iostream>
#include <string>

#include <wiringPi.h>
#include <wiringSerial.h>
#include "json.hpp"
#include <list>

#include "globals.h"

#define GSM_CTRL 1

using namespace std;
using json = nlohmann::json;


int vSetup(){
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"wPi Setup");

if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %sn", strerror (errno)) ;

while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",0,"Could not initialize GPIO");

return 1 ;
}


}




void serialTest() {


int fd;
int count;
unsigned int nextTime;

if ((fd = serialOpen("/dev/ttyAMA0", 115200)) < 0)
{
fprintf(stderr, "Unable to open serial device: %sn", strerror(errno));
return 1;
}

if (wiringPiSetup() == -1)
{
fprintf(stdout, "Unable to start wiringPi: %sn", strerror(errno));
return 1;
}

nextTime = millis() + 300;

for (count = 0; count < 256; )
{
if (millis() > nextTime)
{
printf("nOut: %3d: ", count);
fflush(stdout);
serialPutchar(fd, count);
nextTime += 300;
++count;
}

delay(3);

while (serialDataAvail(fd))
{
printf(" -> %3d", serialGetchar(fd));
fflush(stdout);
}
}

printf("n");
return 0;
}


int openSerial(){
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"Open serial port");

if ((fd = serialOpen ("/dev/ttyS0", 9600)) < 0) // 0 for rp3 1 for op zero
{
fprintf (stderr, "Unable to open serial device: %sn", strerror (errno)) ;
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",0,"Cant open serial port");
return 1 ;
}


}

int gsmSettings(){

while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"gsmSetup");


pinMode (GSM_CTRL, OUTPUT) ;
digitalWrite(GSM_CTRL, LOW);
delay(100);
digitalWrite(GSM_CTRL, HIGH);
delay(5000);

string gsmIn = "";



cout << "GSM AT" << endl;
serialPrintf(fd,"ATn");

delay(1500);
if(simPin.length() > 2) {
serialPrintf(fd,"AT+CPIN=");
serialPrintf(fd,simPin.c_str());
serialPrintf(fd,"n");

}
delay(6500);
cout << "GSM SETTINGS" << endl;
serialPrintf(fd,"AT+IFC=1,1n");
delay(150);
serialPrintf(fd,"AT+CMGF=1n");
delay(150);
serialPrintf(fd,"AT+CLIP=1n");
delay(150);
serialPrintf(fd,"ATE0n");
delay(150);
serialPrintf(fd,"AT+COLP=1n");
delay(150);
serialPrintf(fd,"AT+CSCS="IRA"n");
delay(150);
serialPrintf(fd,"AT+QCCIDn");
delay(150);
serialPrintf(fd,"AT+CIMIn");
d
cout << "GSM INIT OK" << endl;

while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"GSM Setup Finished");

putLog("SMSService", 2, "GSM Setup Finished");
}

string trim(const string& str)
{
size_t first = str.find_first_not_of(' ');
if (string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}


int checkIncoming(){
serialFlush(fd);
serialPrintf(fd,"AT+CMGL="REC UNREAD",1n");
string gsmIn = "";

// how much delay needed ?
delay (150) ;

while (serialDataAvail (fd))
{
int a = serialGetchar (fd);
char b = a;
gsmIn += b;
fflush (stdout) ; // is it neccesary ??
}

std::size_t found = gsmIn.find("ERROR");
if (found!=std::string::npos){


return 0;
}

found = gsmIn.find("REC UNREAD");
if (found!=std::string::npos){


while(isLoggerBusy){
sleepcp(41);
}
//putLog("SMSService",2,"GSM incoming Message :" + gsmIn );

string mesNumToDel = gsmIn.substr(gsmIn.find(":") + 2, gsmIn.find(",") - gsmIn.find(":") - 2);
cout << "mesNumTo --"<<mesNumToDel << "--" << endl;

int virgul1 = gsmIn.find(",");
int virgul2 = gsmIn.find(",", virgul1 + 1);
int virgul3 = gsmIn.find(",", virgul2 + 1);
int virgul4 = gsmIn.find(",", virgul3 + 1);

string senderId = gsmIn.substr(virgul2 + 2, virgul3 - virgul2 -3);
cout << "senderID --"<<senderId << "--" << endl;

string messageTime = gsmIn.substr(virgul4 + 2, gsmIn.find_last_of(""") - virgul4 -5);

cout << "mesTime --"<<messageTime << "--" << endl;

string incomingMessage = gsmIn.substr(gsmIn.find_last_of(""") + 3 , gsmIn.find("OK") - gsmIn.find_last_of(""") - 7 ) ;

int inMesLeng = incomingMessage.length();
char bufCharArr [inMesLeng + 1];
strcpy(bufCharArr, incomingMessage.c_str());
string parsedInMes = "";
for (int i=0; i<inMesLeng; i++){

if(int(bufCharArr[i]) == 165) {

} else if (int(bufCharArr[i]) == 164){

} else if (int(bufCharArr[i]) == 163){

}else if (int(bufCharArr[i]) == 165){

}else if (int(bufCharArr[i]) == 36){

} else {
parsedInMes += bufCharArr[i];
//cout << bufCharArr[i] << int(bufCharArr[i]) << endl;
}

}

incomingMessage = parsedInMes;

cout << "incmessa --"<< incomingMessage << "--" << endl;

while(isLoggerBusy){
sleepcp(41);
}

//putLog("SMSService",2,"GSM Parsed Message sender=" + senderId + "- message=" +incomingMessage+ "- time="+ messageTime +"-");

json inMessage = {
{"Action","ResetRequest"},
{"Data" ,
{
{"S",messageTime},
{"M",incomingMessage},
{"P",senderId}
}
}
};

incomingMessages.push_back(inMessage.dump());

int n = mesNumToDel.length();
char char_array[n+1];
strcpy(char_array, mesNumToDel.c_str());
//deleting it

serialFlush(fd);
serialPrintf(fd,"AT+CMGD=");
serialPrintf(fd,char_array);
serialPrintf(fd,"n");
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"Deleting Message");
delay(2000);
serialFlush(fd);

} else{
found = gsmIn.find("ERROR");
if (found!=std::string::npos){

}
}

}
const char* gsm_send_1 = "AT+CMGS="";


int sendMessage(){

json j = json::parse(messageSendingData.front());

string mes = j["Content"].dump();
string no = j["To"].dump();


while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"Sending Message to=" + no + "-");

mes = mes.substr(1,mes.length()-2);
no = no.substr(1,no.length()-2);

find_and_replace(mes, "\n", "n");

cout <<"SMS Service mes " << mes << endl;
cout <<"SMS Service no " << no << endl;

int n1 = mes.length();
char char_arrayMes[n1+1];
strcpy(char_arrayMes, mes.c_str());

int n2 = no.length();
char char_arrayNo[n2+1];
strcpy(char_arrayNo, no.c_str());

delay(100);
serialPrintf(fd,gsm_send_1);
serialPrintf(fd,char_arrayNo);
serialPrintf(fd,""");
serialPutchar (fd,0x0d);
serialPrintf(fd,char_arrayMes);
serialPutchar (fd,0x1a);
serialPutchar (fd,0x0d);
serialPrintf(fd,"n");
delay(100);
// wait for answer...
bool canOut = false;
string responseString = "";
while(!canOut){
delay(1000);
while ( serialDataAvail (fd) )
{
int a = serialGetchar (fd);
char b = a;
if(a != 0){
responseString += b;
}
fflush (stdout) ; // is it neccesary ??
}
if(responseString.length() > 3){
if(responseString.find("OK") != std::string::npos){
canOut = true;
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"SMS Sent");
} else if (responseString.find("ERROR") != std::string::npos){
canOut = true;
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",0,"SMS cant send. Error=" +responseString );
}
cout <<"SMS send response :" << responseString << "asd"<<endl;
}
}
json feed = {
{"Action","Status"},
{"Data" ,
{
{"IsFinalized",true},
{"TransactionId",j["TransactionId"].dump()},
{"Status",responseString}
}
}
};

feedBackMessages.push_back(feed.dump());

while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"Clear Out Box...");
serialPrintf(fd,"AT+CMGD=1,3");
serialPrintf(fd,"n");
mes = "";
no = "";
delay(2000);
messageSendingData.pop_front();

}


void *threadSMS (void *threadid)
{
exitProcessGsm = false;
gsmThreadRunning = true;
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"Service Started");
cout <<"SMS Service Started, " << endl;
int count ;
serialTest();
vSetup();
openSerial();
gsmSettings();
while(true){
checkIncoming();
while(!messageSendingData.empty()){
cout <<"SendMessage------" << endl;
sendMessage();
}
delay(1000);
if(exitProcessGsm){
while(isLoggerBusy){
sleepcp(41);
}
putLog("SMSService",2,"ExitProcess...");
gsmThreadRunning = false;
pthread_exit(NULL);
}
}
gsmThreadRunning = false;
pthread_exit(NULL);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.