From ee37c413c025f7c6eb23de15629cf28aaa80882d Mon Sep 17 00:00:00 2001 From: pfaure Date: Wed, 19 Jan 2022 13:31:11 +0100 Subject: [PATCH] Code node fini, gateway en cours --- gateway/gateway.ino | 19 ++- gateway/led.cpp | 2 +- gateway/sim5215_umts_shield.cpp | 270 ++++++++++++++++++++++++++++++++ gateway/sim5215_umts_shield.h | 32 ++++ node.ino | 0 node/node.ino | 120 ++++++++++++++ 6 files changed, 438 insertions(+), 5 deletions(-) create mode 100644 gateway/sim5215_umts_shield.cpp create mode 100644 gateway/sim5215_umts_shield.h delete mode 100644 node.ino create mode 100644 node/node.ino diff --git a/gateway/gateway.ino b/gateway/gateway.ino index 4942b4f..e697edb 100644 --- a/gateway/gateway.ino +++ b/gateway/gateway.ino @@ -1,16 +1,27 @@ #include "led.h" +#include "sim5215_umts_shield.h" + +Shield shield(5,3,4); void setup() { initLed(Green, 8); initLed(Blue, 7); initLed(Red, 6); + + blinkLed(Green); + shield.power_on(); + shield.init("0000", "", "", ""); + turnOnLed(Green); } void loop() { - blinkLed(Red); + turnOnLed(Red); + shield.sendTextMessage("0659377414", "Led RED ON"); + turnOffLed(Red); + delay(10000); + turnOnLed(Blue); - delay(1000); - turnOnLed(Green); - delay(1000); + shield.sendTextMessage("0659377414", "Led BLUE ON"); turnOffLed(Blue); + delay(10000); } diff --git a/gateway/led.cpp b/gateway/led.cpp index f945a64..a29ed3b 100644 --- a/gateway/led.cpp +++ b/gateway/led.cpp @@ -15,7 +15,7 @@ void initLed(color c, int pin) { int blinkLed(color c) { if (pins[c] != -1) { - for (int i = 0; i<5; i++) { + for (int i = 0; i<10; i++) { digitalWrite(pins[c], LED_ON); delay(50); digitalWrite(pins[c], LED_OFF); diff --git a/gateway/sim5215_umts_shield.cpp b/gateway/sim5215_umts_shield.cpp new file mode 100644 index 0000000..af743ec --- /dev/null +++ b/gateway/sim5215_umts_shield.cpp @@ -0,0 +1,270 @@ +#include "sim5215_umts_shield.h" + +Shield::Shield(int onPin, int shieldRX, int shieldTX) : shieldSerial(shieldRX, shieldTX){ + shieldSerial.begin(115200); + onModulePin = onPin; + pinMode(onModulePin, OUTPUT); +} + +int Shield::init(char pin_number[], char apn[], char user_name[], char password[]) { + char aux_str[50]; + int answer = 0; + //sets the PIN code + sprintf(aux_str, "AT+CPIN=%s", pin_number); + answer &= sendATcommand(aux_str, "OK", 2000); + + delay(3000); + + //check that connected to home network or partner network + while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); + + // 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() { + + uint8_t answer=0; + + // 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 + while (answer == 0) { + // Send AT every two seconds and wait for the answer + answer = sendATcommand("AT", "OK", 2000); + } + } +} + +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 + + delay(100); + + while (shieldSerial.available() > 0) shieldSerial.read(); // Clean the input buffer +// shieldSerial.println("-- Serial cleaning done --"); + + int bytes = shieldSerial.println(ATcommand); // Send the AT command + + x = 0; + previous = millis(); + + // this loop waits for the answer + do { + // if there are data in the UART input buffer, reads it and checks for the asnwer + if (shieldSerial.available() != 0) { + // check if the desired answer is in the response of the module + response[x] = shieldSerial.read(); + x++; + 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) { + Serial.print("AT response="); + Serial.println(response); + } + return answer; +} + +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]; + 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 + shieldSerial.write(0x1A); + http_status = 1; + + while ((http_status == 1) || (http_status == 2)) { + answer = sendATcommand("", "+CHTTPACT: ", timeout); + if (answer == 0) { + if (http_status == 1) { + http_status = 3; + } else if (http_status == 2) { + http_status = 5; + } + 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)) { + Serial.println("DATA,xxx line"); + // 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); + // Serial.print("Packet size="); + // Serial.println(data_size, DEC); + + // Now, search the end of the HTTP header (\r\n\r\n) + do { + //wait for 4 characters + while (shieldSerial.available() < 3); + + data_size--; + if (shieldSerial.read() == '\r') { + 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; +} + +int8_t Shield::sendTextMessage(char phone_number[], char text[]) { + 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; +} diff --git a/gateway/sim5215_umts_shield.h b/gateway/sim5215_umts_shield.h new file mode 100644 index 0000000..c7c7631 --- /dev/null +++ b/gateway/sim5215_umts_shield.h @@ -0,0 +1,32 @@ +#ifndef SIM5215_UMTS_SHIELD_H +#define SIM5215_UMTS_SHIELD_H + +#include +#include + +class Shield { + + private: + int onModulePin; + SoftwareSerial shieldSerial; + + public: + //Create instance and setup Serial and power pin + Shield(int onPin, int shieldRX, int shieldTX); + + //Init shield + int init(char pin_number[], char apn[], char user_name[], char password[]); + + //Power on the shield if shut down + void power_on(); + + //Sends an AT command to the shield + int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout); + + //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 sendTextMessage(char phone_number[], char text[]); +}; + +#endif diff --git a/node.ino b/node.ino deleted file mode 100644 index e69de29..0000000 diff --git a/node/node.ino b/node/node.ino new file mode 100644 index 0000000..67b9d93 --- /dev/null +++ b/node/node.ino @@ -0,0 +1,120 @@ +#include +#include +#include + +#define MESURE_PERIOD 10 + +const int cameraID = 0x1; + +const int pinCmdFlash = 4; +const int pinTemperature = A0; +const int pinPir = 2; + +const int pinNSS = 10; +const int pinRST = 9; +const int pinDIO0 = 3; + +volatile boolean movementState = false; +volatile boolean mesurementState = false; + +void setup() { + // Initialisation du moniteur série + Serial.begin(9600); + while (!Serial); + + // Initialisation de la commande du Flash + Serial.println("Initialising IR FLASH"); + pinMode(pinCmdFlash, OUTPUT); + digitalWrite(pinCmdFlash, LOW); + + // Initialisation du capteur PIR + Serial.println("Initialising PIR sensor"); + attachInterrupt(digitalPinToInterrupt(pinPir), setMovementState, RISING); + + // Initialisation du timer de mesure + Serial.println("Initialising Temperature sensor"); + MsTimer2::set(MESURE_PERIOD*1000, setMesurementState); + + + Serial.println("Initialising LoRa emiter"); + while (!LoRa.begin(868E6)) { + Serial.println("."); + delay(500); + } + LoRa.setSyncWord(0x12); + + Serial.println("Starting periodic measurement"); + MsTimer2::start(); + + Serial.println("Initialisation done"); +} + +void setMovementState() { + movementState = true; +} + +void setMesurementState() { + mesurementState = true; +} + +float readVoltage(int pin) { + int mesure = analogRead(pin); + return (float(mesure)*5.0)/1024.0; +} + +float readVoltageNTimes(int pin, int N) { + int mesure = 0; + int i = 0; + for (i=0; i