Gateway OK

This commit is contained in:
bcadot 2022-01-20 00:37:51 +01:00
parent 6c5f6045b1
commit 4108b4bd7d
3 changed files with 250 additions and 234 deletions

View file

@ -16,9 +16,11 @@ struct message {
float data; float data;
}; };
struct message msg = {-1, -1, -1, 0.0};
void setup() { void setup() {
// Initialisation du moniteur série // Initialisation du moniteur série
Serial.begin(9600); Serial.begin(115200);
while (!Serial); while (!Serial);
// Initialisation des leds // Initialisation des leds
@ -34,7 +36,7 @@ void setup() {
// Initialisation de shield // Initialisation de shield
Serial.println("Initialisation shield UMTS"); Serial.println("Initialisation shield UMTS");
shield.power_on(); shield.power_on();
shield.init("0000", "", "", ""); shield.init("0000", "mmsbouygtel.com", "", "");
// Initialisation LoRa // Initialisation LoRa
Serial.println("Initialisation LoRa transceiver"); Serial.println("Initialisation LoRa transceiver");
@ -50,7 +52,8 @@ void setup() {
turnOnLed(Green); turnOnLed(Green);
} }
void receiveLoRa(struct message * msg) { void receiveLoRa() {
msg.vers = -1;
int packetSize = LoRa.parsePacket(); int packetSize = LoRa.parsePacket();
if (packetSize) { if (packetSize) {
turnOnLed(Blue); turnOnLed(Blue);
@ -59,7 +62,10 @@ void receiveLoRa(struct message * msg) {
while (LoRa.available()) { while (LoRa.available()) {
String stringData = LoRa.readString(); String stringData = LoRa.readString();
const char* LoRaData = stringData.c_str(); const char* LoRaData = stringData.c_str();
sscanf(LoRaData, "%d-%d-%d-%f", &(msg->vers), &(msg->type), &(msg->cameraId), &(msg->data)); int partie_entiere;
int partie_decimale;
sscanf(LoRaData, "%d-%d-%d-%d.%d", &(msg.vers), &(msg.type), &(msg.cameraId), &partie_entiere, &partie_decimale);
msg.data = float(partie_entiere) + (float(partie_decimale))/100;
Serial.print(LoRaData); Serial.print(LoRaData);
Serial.print(""); Serial.print("");
} }
@ -71,21 +77,56 @@ void receiveLoRa(struct message * msg) {
} }
} }
void printMessage(struct message msg) { void printMessage() {
Serial.println("Message : "); if (msg.vers > 0) {
Serial.print("\tVersion : "); Serial.println("Message : ");
Serial.print(msg.vers); Serial.print("\tVersion : ");
Serial.print("\tType : "); Serial.println(msg.vers);
Serial.print(msg.type); Serial.print("\tType : ");
Serial.print("\tCamera ID : "); Serial.println(msg.type);
Serial.print(msg.cameraId); Serial.print("\tCamera ID : ");
Serial.print("\tData : "); Serial.println(msg.cameraId);
Serial.println(msg.data); Serial.print("\tData : ");
Serial.println(""); Serial.println(msg.data);
Serial.println("");
}
}
void processMessage() {
if (msg.vers > 0) {
delay(50);
const char url[] = "rasp.pikouri.fr";
int port = 80;
//Event
if (msg.type == 1) {
char request[150];
sprintf(request, "POST /api/cameras/%d/counter HTTP/1.1\r\nHost: rasp.pikouri.fr\r\n\r\n", msg.cameraId);
Serial.println(request);
shield.sendPOSTrequest(url, port, request, 20000);
shield.sendTextMessage("0783852909", "Event");
}
//Temperature
else if (msg.type == 2) {
char request[200];
String tempData = String(msg.data);
sprintf(request, "POST /api/cameras/%d/temperature HTTP/1.1\r\nHost: rasp.pikouri.fr\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", msg.cameraId, tempData.length(),tempData.c_str());
Serial.println(request);
if (shield.sendPOSTrequest(url, port, request, 20000) == 3) {
shield.power_on();
delay(1000);
shield.power_on();
shield.init("0000", "mmsbouygtel.com", "", "");
} else {
Serial.println("Request sent\n\n\n");
}
} else {
Serial.print("Type not indentified: ");
Serial.println(msg.type);
}
}
} }
void loop() { void loop() {
struct message msg; receiveLoRa();
receiveLoRa(&msg); processMessage();
printMessage(msg);
} }

View file

@ -2,36 +2,43 @@
Shield::Shield(int onPin, int shieldRX, int shieldTX) : shieldSerial(shieldRX, shieldTX){ Shield::Shield(int onPin, int shieldRX, int shieldTX) : shieldSerial(shieldRX, shieldTX){
shieldSerial.begin(115200); shieldSerial.begin(115200);
onModulePin = onPin; onModulePin = onPin;
pinMode(onModulePin, OUTPUT); pinMode(onModulePin, OUTPUT);
} }
int Shield::init(char pin_number[], char apn[], char user_name[], char password[]) { int Shield::init(char pin_number[], char apn[], char user_name[], char password[]) {
char aux_str[50]; char aux_str[50];
int answer = 0; int answer = -1;
//sets the PIN code
sprintf(aux_str, "AT+CPIN=%s", pin_number); Serial.println("Starting...");
answer &= sendATcommand(aux_str, "OK", 2000);
delay(3000); delay(3000);
//check that connected to home network or partner network //sets the PIN code
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); sprintf(aux_str, "AT+CPIN=%s", pin_number);
sendATcommand(aux_str, "OK", 2000);
// sets APN, user name and password delay(3000);
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); Serial.println("Connecting to the network...");
answer &= sendATcommand(aux_str, "OK", 2000);
return answer; while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
Serial.print("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
// 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;
} }
void Shield::power_on() { void Shield::power_on(){
uint8_t answer=0; uint8_t answer=0;
// checks if the module is started // checks if the module is started
answer = sendATcommand("AT", "OK", 2000); answer = sendATcommand("AT", "OK", 2000);
if (answer == 0) { if (answer == 0) {
@ -40,48 +47,68 @@ void Shield::power_on() {
delay(3000); delay(3000);
digitalWrite(onModulePin,LOW); digitalWrite(onModulePin,LOW);
// waits for an answer from the module // waits for an answer from the module
while (answer == 0) { while(answer == 0) {
// Send AT every two seconds and wait for the answer // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000); answer = sendATcommand("AT", "OK", 2000);
Serial.println(answer);
} }
} }
} }
int8_t Shield::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) { int8_t Shield::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
delay(100); memset(response, '\0', 100); // Initialice the string
while (shieldSerial.available() > 0) shieldSerial.read(); // Clean the input buffer delay(100);
// shieldSerial.println("-- Serial cleaning done --");
int bytes = shieldSerial.println(ATcommand); // Send the AT command while( shieldSerial.available() > 0) shieldSerial.read(); // Clean the input buffer
x = 0; int bytes = shieldSerial.println(ATcommand); // Send the AT command
previous = millis();
// this loop waits for the answer x = 0;
do { previous = millis();
// if there are data in the UART input buffer, reads it and checks for the asnwer
if (shieldSerial.available() != 0) { // this loop waits for the answer
// check if the desired answer is in the response of the module 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");
response[x] = shieldSerial.read(); response[x] = shieldSerial.read();
x++; x++;
if (strstr(response, expected_answer) != NULL) { // check if the desired answer is in the response of the module
answer = 1; if (strstr(response, expected_answer) != NULL)
} {
} answer = 1;
// Waits for the answer with time out }
} while ((answer == 0) && ((millis() - previous) < timeout)); }
if (answer == 0 && strlen(response) != 0) { // Waits for the answer with time out
Serial.print("AT response="); }
Serial.println(response); while((answer == 0) && ((millis() - previous) < timeout));
}
return answer; 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;
} }
int8_t Shield::sendPOSTrequest(char url[], int port, char request[], unsigned int timeout) { int8_t Shield::sendPOSTrequest(char url[], int port, char request[], unsigned int timeout) {
@ -90,181 +117,126 @@ int8_t Shield::sendPOSTrequest(char url[], int port, char request[], unsigned in
int data_size = 0; int data_size = 0;
int http_x, http_status, x, answer; int http_x, http_status, x, answer;
char data[400]; char data[400];
previous = millis(); previous = millis();
sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port); sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port);
answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", timeout); answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", timeout);
if (answer == 1) { if (answer == 1) {
// Sends the request // Sends the request
shieldSerial.println(request); shieldSerial.println(request);
// Sends <Ctrl+Z> // Sends <Ctrl+Z>
shieldSerial.write(0x1A); shieldSerial.write(0x1A);
http_status = 1; 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(); // ,
while ((http_status == 1) || (http_status == 2)) { // Reads the packet size
answer = sendATcommand("", "+CHTTPACT: ", timeout); x=0;
if (answer == 0) { do {
if (http_status == 1) { while(shieldSerial.available()==0);
http_status = 3; aux_str[x] = shieldSerial.read();
} else if (http_status == 2) { x++;
http_status = 5; } while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
}
Serial.print(F("http_status= "));
Serial.println(http_status, DEC);
} else {
// answer == 1
while(shieldSerial.available()==0);
aux_str[0] = shieldSerial.read();
if ((aux_str[0] == 'D') && (http_status == 1)) { aux_str[x-1] = '\0';
Serial.println("DATA,xxx line"); data_size = atoi(aux_str);
// Data packet with header
while(shieldSerial.available()<4); // Now, search for (200 ok)
shieldSerial.read(); // A char cible[] = "200 ok";
shieldSerial.read(); // T char current[] = "\0\0\0\0\0\0";
shieldSerial.read(); // A while ((!match(cible,current)) && (data_size > 0)) {
shieldSerial.read(); // , 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;
// Reads the packet size // Vidus du buffer
x=0; while (data_size > 0) {
do { shieldSerial.read();
while(shieldSerial.available()==0); data_size--;
aux_str[x] = shieldSerial.read(); }
x++; } else {
} while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n')); Serial.println("No 200 ok");
http_status = 3;
}
} else {
Serial.println("No first DATA");
http_status = 3;
}
}
aux_str[x-1] = '\0'; if (http_status == 2) {
data_size = atoi(aux_str); answer = sendATcommand("", "+CHTTPACT: ", timeout);
// Serial.print("Packet size="); if (answer == 0) {
// Serial.println(data_size, DEC); 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;
}
}
}
// Now, search the end of the HTTP header (\r\n\r\n) previous = millis() - previous;
do { } else {
//wait for 4 characters Serial.println("!Error on CHTTPACT REQUEST!");
while (shieldSerial.available() < 3); http_status = 3;
}
data_size--; Serial.print(F("http_status= "));
if (shieldSerial.read() == '\r') { Serial.println(http_status, DEC);
Serial.println("Found first");
data_size--;
if (shieldSerial.read() == '\n') {
Serial.println("Found second");
data_size--;
if (shieldSerial.read() == '\r') {
Serial.println("Found third");
data_size--;
if (shieldSerial.read() == '\n') {
Serial.println("Found end of the header");
// End of the header found
http_status = 2;
}
}
}
}
} while ((http_status == 1) && (data_size > 0));
if (http_status == 2) {
// Reads the data
http_x = 0;
Serial.println("Starting reading data");
do {
if (shieldSerial.available() != 0) {
data[http_x] = shieldSerial.read();
http_x++;
data_size--;
// Serial.print("Bytes read=");
// Serial.println(http_x, DEC);
} else {
delay(1);
}
} while(data_size > 0);
data[http_x] = '\0';
Serial.println("Finished reading data");
}
} else if ((aux_str[0] == 'D') && (http_status == 2)) {
// Data packet with header
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);
do {
if(shieldSerial.available() != 0){
data[http_x] = shieldSerial.read();
http_x++;
} else {
delay(1);
}
} while(data_size > 0);
data[http_x] = '\0';
} else if (aux_str[0] == '0') {
// end of the AT command
http_status = 0;
} else {
// unknown response
http_status = 4;
Serial.print(char(aux_str[0]));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
Serial.print(char(shieldSerial.read()));
}
}
}
previous = millis() - previous;
// Serial.println(previous, DEC);
if (http_status == 0) {
Serial.print(F("HTTP data: "));
Serial.println(data);
} else {
Serial.print(F("http_status= "));
Serial.println(http_status, DEC);
}
} else {
Serial.println("!Error on CHTTPACT!");
}
return http_status; return http_status;
} }
int8_t Shield::sendTextMessage(char phone_number[], char text[]) { int8_t Shield::sendTextMessage(char phone_number[], char text[]) {
char aux_str[50]; char aux_str[50];
int aux, answer; int aux, answer;
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
sprintf(aux_str,"AT+CMGS=\"%s\"", phone_number); sprintf(aux_str,"AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand(aux_str, ">", 2000); // send the SMS number answer = sendATcommand(aux_str, ">", 2000); // send the SMS number
if (answer == 1) { if (answer == 1) {
shieldSerial.println(text); shieldSerial.println(text);
shieldSerial.write(0x1A); shieldSerial.write(0x1A);
answer = sendATcommand("", "OK", 20000); answer = sendATcommand("", "OK", 20000);
if (answer == 1) { if (answer == 1) {
Serial.print("Text message sent."); Serial.print("Text message sent.");
} else { } else {
Serial.print("Could not send text message."); Serial.print("Could not send text message.");
} }
} else { } else {
Serial.print("Could not send text message"); Serial.print("Could not send text message");
Serial.println(answer, DEC); Serial.println(answer, DEC);
} }
return answer; return answer;
} }

View file

@ -26,7 +26,10 @@ class Shield {
//Sends a POST request to url:port via AT commands to the shield //Sends a POST request to url:port via AT commands to the shield
int8_t sendPOSTrequest(char url[], int port, char request[], unsigned int timeout); int8_t sendPOSTrequest(char url[], int port, char request[], unsigned int timeout);
int8_t sendTextMessage(char phone_number[], char text[]); //Sends a text message to the desired phone number
int8_t sendTextMessage(char phone_number[], char text[]);
int availableMemory();
}; };
#endif #endif