Arduino_SourceCode_SmartCamera/gateway/sim5215_umts_shield.cpp

243 lines
6.5 KiB
C++
Raw Normal View History

2022-01-19 13:31:11 +01:00
#include "sim5215_umts_shield.h"
Shield::Shield(int onPin, int shieldRX, int shieldTX) : shieldSerial(shieldRX, shieldTX){
shieldSerial.begin(115200);
2022-01-20 00:37:51 +01:00
onModulePin = onPin;
pinMode(onModulePin, OUTPUT);
2022-01-19 13:31:11 +01:00
}
int Shield::init(char pin_number[], char apn[], char user_name[], char password[]) {
2022-01-20 00:37:51 +01:00
char aux_str[50];
int answer = -1;
Serial.println("Starting...");
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
delay(3000);
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
//sets the PIN code
sprintf(aux_str, "AT+CPIN=%s", pin_number);
sendATcommand(aux_str, "OK", 2000);
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
delay(3000);
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
Serial.println("Connecting to the network...");
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
Serial.print("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
// sets APN, user name and password
sprintf(aux_str, "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
answer &= sendATcommand(aux_str, "OK", 2000);
sprintf(aux_str, "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
answer &= sendATcommand(aux_str, "OK", 2000);
return answer;
}
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
void Shield::power_on(){
uint8_t answer=0;
2022-01-19 13:31:11 +01:00
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0) {
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
2022-01-20 00:37:51 +01:00
while(answer == 0) {
2022-01-19 13:31:11 +01:00
// Send AT every two seconds and wait for the answer
2022-01-20 00:37:51 +01:00
answer = sendATcommand("AT", "OK", 2000);
Serial.println(answer);
2022-01-19 13:31:11 +01:00
}
}
}
2022-01-20 00:37:51 +01:00
int8_t Shield::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
memset(response, '\0', 100); // Initialice the string
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
delay(100);
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
while( shieldSerial.available() > 0) shieldSerial.read(); // Clean the input buffer
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
int bytes = shieldSerial.println(ATcommand); // Send the AT command
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
x = 0;
previous = millis();
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
// this loop waits for the answer
do {
// if there are data in the UART input buffer, reads it and checks for the answer
if (shieldSerial.available() != 0){
// Serial.println("Data in UART");
2022-01-19 13:31:11 +01:00
response[x] = shieldSerial.read();
x++;
2022-01-20 00:37:51 +01:00
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the answer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
bool match(char * cible, char * current) {
int i = 0;
boolean ok = true;
while (i<6 && ok) {
ok = (cible[i] == current[i]);
i++;
}
return ok;
}
void add_c(char * chaine, char c) {
chaine[0] = chaine[1];
chaine[1] = chaine[2];
chaine[2] = chaine[3];
chaine[3] = chaine[4];
chaine[4] = chaine[5];
chaine[5] = c;
2022-01-19 13:31:11 +01:00
}
int8_t Shield::sendPOSTrequest(char url[], int port, char request[], unsigned int timeout) {
char aux_str[50];
unsigned long previous;
int data_size = 0;
int http_x, http_status, x, answer;
char data[400];
2022-01-20 00:37:51 +01:00
previous = millis();
sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port);
answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", timeout);
if (answer == 1) {
// Sends the request
shieldSerial.println(request);
// Sends <Ctrl+Z>
shieldSerial.write(0x1A);
http_status = 1;
answer = sendATcommand("", "+CHTTPACT: ", timeout);
if (answer == 0) {
if (http_status == 1) {
http_status = 3;
Serial.println("ERROR : No first +CHTTPACT: ");
}
} else {
// answer == 1
Serial.println("+CHTTPACT: received");
while(shieldSerial.available()==0);
aux_str[0] = shieldSerial.read();
if ((aux_str[0] == 'D') && (http_status == 1)) {
while(shieldSerial.available()<4);
shieldSerial.read(); // A
shieldSerial.read(); // T
shieldSerial.read(); // A
shieldSerial.read(); // ,
// Reads the packet size
x=0;
do {
while(shieldSerial.available()==0);
aux_str[x] = shieldSerial.read();
x++;
} while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
aux_str[x-1] = '\0';
data_size = atoi(aux_str);
// Now, search for (200 ok)
char cible[] = "200 ok";
char current[] = "\0\0\0\0\0\0";
while ((!match(cible,current)) && (data_size > 0)) {
while (shieldSerial.available() < 1);
add_c(current, shieldSerial.read());
data_size--;
}
if (match(cible,current)) {
Serial.println("Found 200 ok");
// End of the header found
http_status = 2;
// Vidus du buffer
while (data_size > 0) {
shieldSerial.read();
data_size--;
}
} else {
Serial.println("No 200 ok");
http_status = 3;
}
} else {
Serial.println("No first DATA");
http_status = 3;
}
}
if (http_status == 2) {
answer = sendATcommand("", "+CHTTPACT: ", timeout);
if (answer == 0) {
http_status = 3;
Serial.println("ERROR : No second +CHTTPACT: ");
} else {
// answer == 1
Serial.println("+CHTTPACT: received");
while(shieldSerial.available()==0);
aux_str[0] = shieldSerial.read();
Serial.println(aux_str[0]);
if (aux_str[0] == '0') {
http_status = 0;
} else {
Serial.println("No 0");
http_status = 3;
}
}
}
previous = millis() - previous;
} else {
Serial.println("!Error on CHTTPACT REQUEST!");
http_status = 3;
}
2022-01-19 13:31:11 +01:00
2022-01-20 00:37:51 +01:00
Serial.print(F("http_status= "));
Serial.println(http_status, DEC);
2022-01-19 13:31:11 +01:00
return http_status;
}
int8_t Shield::sendTextMessage(char phone_number[], char text[]) {
2022-01-20 00:37:51 +01:00
char aux_str[50];
int aux, answer;
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
sprintf(aux_str,"AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand(aux_str, ">", 2000); // send the SMS number
if (answer == 1) {
shieldSerial.println(text);
shieldSerial.write(0x1A);
answer = sendATcommand("", "OK", 20000);
if (answer == 1) {
Serial.print("Text message sent.");
} else {
Serial.print("Could not send text message.");
}
} else {
Serial.print("Could not send text message");
Serial.println(answer, DEC);
}
return answer;
2022-01-19 13:31:11 +01:00
}