Difference between revisions of "BoSL code"

From BoSL Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 28: Line 28:
 
Congratulations! You have just programmed the BoSL board, you can repeat these steps to upload new code to the BoSL.
 
Congratulations! You have just programmed the BoSL board, you can repeat these steps to upload new code to the BoSL.
  
If you'd like some sample code check out the script below which logs the Battery Voltage, Air Pressure, and Air Temperature using the on board sensor.
+
If you'd like some sample code check out the script below which logs the Battery Voltage, Air Pressure, and Air Temperature using the on board sensor. Be sure to grab the appropriate libraries first.  
  
 
  #include <SoftwareSerial.h>
 
  #include <SoftwareSerial.h>
 
  #include <LowPower.h>
 
  #include <LowPower.h>
 
  #include <SparkFun_MS5803_I2C.h>
 
  #include <SparkFun_MS5803_I2C.h>
 
 
  #define SIMCOM_7000 // SIM7000A/C/E/G
 
  #define SIMCOM_7000 // SIM7000A/C/E/G
 
  #define BAUDRATE 9600 // MUST be below 19200 (for stability) but 9600 is more stable
 
  #define BAUDRATE 9600 // MUST be below 19200 (for stability) but 9600 is more stable
 
 
  #define CHARBUFF 196 //SIM7000 serial response buffer //longer than 255 will cause issues
 
  #define CHARBUFF 196 //SIM7000 serial response buffer //longer than 255 will cause issues
 
  #define MAXTRASMITINTERVAL 180000//milli seconds
 
  #define MAXTRASMITINTERVAL 180000//milli seconds
 
 
  // For SIM7000 BoSL board
 
  // For SIM7000 BoSL board
 
  #define PWRKEY 4
 
  #define PWRKEY 4
Line 45: Line 42:
 
  #define BOSL_RX 3 // Microcontroller RX
 
  #define BOSL_RX 3 // Microcontroller RX
 
  #define BOSL_TX 2 // Microcontroller TX
 
  #define BOSL_TX 2 // Microcontroller TX
 
 
  //Site specific config
 
  //Site specific config
 
  #define SITEID "PUT SITE IDENTIFIER HERE"
 
  #define SITEID "PUT SITE IDENTIFIER HERE"
 
  #define APN "telstra.internet"
 
  #define APN "telstra.internet"
 
 
  //default variable array (complilation purposes only)
 
  //default variable array (complilation purposes only)
 
  bool defaultVars[6] = {1,1,1,1,1,1};
 
  bool defaultVars[6] = {1,1,1,1,1,1};
 
 
  //millis timer variable  
 
  //millis timer variable  
 
  extern volatile unsigned long timer0_millis;
 
  extern volatile unsigned long timer0_millis;
 
 
  //gobal variables  
 
  //gobal variables  
 
  char response[CHARBUFF]; //sim7000 serial response buffer
 
  char response[CHARBUFF]; //sim7000 serial response buffer
 
  uint32_t lstTransmit = 0; //timestamp of last transmit (milli seconds)
 
  uint32_t lstTransmit = 0; //timestamp of last transmit (milli seconds)
 
  String dataStr; //Transmit URL
 
  String dataStr; //Transmit URL
 
 
  //reponse stings
 
  //reponse stings
 
  char temp[19];
 
  char temp[19];
Line 74: Line 66:
 
  char Lstair[11];
 
  char Lstair[11];
 
  char LstNview[3];
 
  char LstNview[3];
 
 
  //SIM7000 serial object
 
  //SIM7000 serial object
 
  SoftwareSerial simCom = SoftwareSerial(BOSL_RX, BOSL_TX);
 
  SoftwareSerial simCom = SoftwareSerial(BOSL_RX, BOSL_TX);
 
 
  //DEFINE MS5803//
 
  //DEFINE MS5803//
 
  MS5803 MyAirMS5803(ADDRESS_LOW);
 
  MS5803 MyAirMS5803(ADDRESS_LOW);
 
 
 
  ////clears char arrays////
 
  ////clears char arrays////
 
  void charBuffclr(bool clrVars[6] = defaultVars){
 
  void charBuffclr(bool clrVars[6] = defaultVars){
Line 103: Line 91:
 
     }
 
     }
 
  }
 
  }
 
 
  ////clears char arrays////
 
  ////clears char arrays////
 
  void LstcharBuffclr(bool clrVars[6] = defaultVars){
 
  void LstcharBuffclr(bool clrVars[6] = defaultVars){
Line 122: Line 109:
 
     }
 
     }
 
  }
 
  }
 
 
  ////stores coordinate data as previous and clears current arrays////
 
  ////stores coordinate data as previous and clears current arrays////
 
  void charBuffAdvance(bool advVars[6] = defaultVars){
 
  void charBuffAdvance(bool advVars[6] = defaultVars){
 
     uint8_t i;
 
     uint8_t i;
   
+
    if(advVars[0]){
    if(advVars[0]){
 
 
         for(i = 0; i < 19; i++){
 
         for(i = 0; i < 19; i++){
 
             Lsttemp[i] = temp[i];
 
             Lsttemp[i] = temp[i];
Line 152: Line 137:
 
         }
 
         }
 
     }
 
     }
 
+
    bool clrVars[6] = {1,1,1,1,1,0};
    bool clrVars[6] = {1,1,1,1,1,0};
+
    charBuffclr(clrVars);
   
 
    charBuffclr(clrVars);
 
 
  }
 
  }
 
 
  ////reads battery voltage from responce char array////
 
  ////reads battery voltage from responce char array////
 
  void storeCBCresponse(){
 
  void storeCBCresponse(){
   
+
    bool end = 0;
    bool end = 0;
 
 
     uint8_t x = 0;
 
     uint8_t x = 0;
     uint8_t j = 0;
+
     uint8_t j = 0;  
   
 
 
     bool clrVars[6] = {0,0,0,0,0,1};
 
     bool clrVars[6] = {0,0,0,0,0,1};
   
 
 
     charBuffclr(clrVars);
 
     charBuffclr(clrVars);
   
 
 
     //loop through reponce to extract data
 
     //loop through reponce to extract data
 
     for (uint8_t i=0; i < CHARBUFF; i++){
 
     for (uint8_t i=0; i < CHARBUFF; i++){
 
 
         //string splitting cases
 
         //string splitting cases
 
         switch(response[i]){
 
         switch(response[i]){
Line 179: Line 156:
 
             i += 2;
 
             i += 2;
 
             break;
 
             break;
 
 
         case ',':
 
         case ',':
 
             x++;
 
             x++;
 
             j=0;
 
             j=0;
 
             break;
 
             break;
 
 
         case '\0':
 
         case '\0':
 
             end = 1;
 
             end = 1;
Line 208: Line 183:
 
         }
 
         }
 
     }
 
     }
 
 
  }
 
  }
 
 
  void setup() {
 
  void setup() {
       
+
  //clear buffers
  //clear buffers
 
 
   charBuffclr();
 
   charBuffclr();
 
   LstcharBuffclr();
 
   LstcharBuffclr();
 
+
  //ensure sim is in the off state
  //ensure sim is in the off state
 
 
   simOff();
 
   simOff();
 
+
  //begin serial
  //begin serial
 
 
   Serial.begin(BAUDRATE);
 
   Serial.begin(BAUDRATE);
 
   simCom.begin(BAUDRATE);
 
   simCom.begin(BAUDRATE);
 
 
   Serial.println("initialising sim");
 
   Serial.println("initialising sim");
 
   //initialise sim (on arduino startup only)
 
   //initialise sim (on arduino startup only)
     simInit();
+
     simInit();
       
 
 
     netReg();
 
     netReg();
 
     netUnreg();
 
     netUnreg();
 
+
  }  
  }
+
  void loop() {  
   
+
       charBuffclr()  
  void loop() {
 
     
 
       charBuffclr();
 
     
 
 
 
 
       Serial.println("PRESS");
 
       Serial.println("PRESS");
 
       pressread();
 
       pressread();
 
       Serial.println("TEMP");
 
       Serial.println("TEMP");
       tempread();
+
       tempread();  
     
 
 
       if(shouldTrasmit()){
 
       if(shouldTrasmit()){
 
             simOn();
 
             simOn();
             simOn();
+
             simOn();        
           
+
             netUnreg();
             netUnreg();
+
             CBCread();        
   
+
             Transmit();
             CBCread();
 
           
 
             Transmit();  
 
 
 
 
             simOff();
 
             simOff();
       }
+
       }  
   
+
     Serial.println("Sleep");  
 
 
     Serial.println("Sleep");
 
   
 
 
     Sleepy(60);
 
     Sleepy(60);
 
 
  }
 
  }
 
 
 
  void pressread(){
 
  void pressread(){
 
   float pressvar;
 
   float pressvar;
 
   uint32_t pressint;
 
   uint32_t pressint;
 
 
 
   MyAirMS5803.reset();
 
   MyAirMS5803.reset();
 
   MyAirMS5803.begin();
 
   MyAirMS5803.begin();
 
 
 
   pressvar = MyAirMS5803.getPressure(ADC_4096);
 
   pressvar = MyAirMS5803.getPressure(ADC_4096);
 
 
   pressint = (int)10*pressvar;
 
   pressint = (int)10*pressvar;
 
 
   ltoa(pressint, press, 10);
 
   ltoa(pressint, press, 10);
 
  }
 
  }
 
 
  void tempread(){
 
  void tempread(){
 
   float tempvar;
 
   float tempvar;
 
 
 
   MyAirMS5803.reset();
 
   MyAirMS5803.reset();
   MyAirMS5803.begin();
+
   MyAirMS5803.begin();  
 
+
   tempvar = MyAirMS5803.getTemperature(CELSIUS, ADC_512);  
   tempvar = MyAirMS5803.getTemperature(CELSIUS, ADC_512);
 
 
 
 
   itoa(int(100*tempvar), temp, 10);
 
   itoa(int(100*tempvar), temp, 10);
 
  }
 
  }
 
 
 
 
  ////SLEEPS FOR SET TIME////
 
  ////SLEEPS FOR SET TIME////
  void Sleepy(uint16_t tsleep){ //Sleep Time in seconds
+
  void Sleepy(uint16_t tsleep){ //Sleep Time in seconds  
   
+
     simCom.flush(); // must run before going to sleep
     simCom.flush(); // must run before going to sleep
 
 
 
     Serial.flush(); // ensures that all messages have sent through serial before arduino sleeps
 
     Serial.flush(); // ensures that all messages have sent through serial before arduino sleeps
 
     simOff();
 
     simOff();
 
 
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
 
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
 
     delay(50);
 
     delay(50);
Line 304: Line 242:
 
     noInterrupts();
 
     noInterrupts();
 
     timer0_millis += 8000;
 
     timer0_millis += 8000;
     interrupts();  
+
     interrupts();      
   
 
   
 
 
     while(tsleep >= 16){
 
     while(tsleep >= 16){
 
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
 
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
Line 313: Line 249:
 
         noInterrupts();
 
         noInterrupts();
 
         timer0_millis += 8000;
 
         timer0_millis += 8000;
         interrupts();
+
         interrupts();      
       
+
      tsleep -= 8;
        tsleep -= 8;
 
 
     }
 
     }
 
  }
 
  }
 
 
  ////TRANSMITS DATA TO WEB////
 
  ////TRANSMITS DATA TO WEB////
  void Transmit(){
+
  void Transmit(){    
 
+
     dataStr = "AT+HTTPPARA=\"URL\",\"www.cartridgerefills.com.au/EoDC/databases/WriteMe.php?SiteName=";  
   
 
   
 
     dataStr = "AT+HTTPPARA=\"URL\",\"www.cartridgerefills.com.au/EoDC/databases/WriteMe.php?SiteName=";
 
   
 
 
     dataStr += SITEID;
 
     dataStr += SITEID;
 
     dataStr += ".csv&T=";
 
     dataStr += ".csv&T=";
Line 333: Line 263:
 
     dataStr += "&B=";
 
     dataStr += "&B=";
 
     dataStr += CBC;
 
     dataStr += CBC;
     dataStr += "\"";
+
     dataStr += "\"";
 
+
     simOn();  
     simOn();
+
     netReg();
   
 
     netReg();
 
   
 
   
 
 
     ///***check logic
 
     ///***check logic
 
   //set CSTT - if it is already set, then no need to do again...
 
   //set CSTT - if it is already set, then no need to do again...
Line 349: Line 275:
 
             sendATcmd(F("AT+CSTT=\"telstra.internet\""), "OK",1000);
 
             sendATcmd(F("AT+CSTT=\"telstra.internet\""), "OK",1000);
 
         }
 
         }
   
 
   
 
 
     //close open bearer
 
     //close open bearer
 
     sendATcmd(F("AT+SAPBR=2,1"), "OK",1000);
 
     sendATcmd(F("AT+SAPBR=2,1"), "OK",1000);
Line 359: Line 283:
 
         sendATcmd(F("AT+SAPBR=1,1"), "OK",1000);
 
         sendATcmd(F("AT+SAPBR=1,1"), "OK",1000);
 
     }
 
     }
   
 
 
     sendATcmd(F("AT+HTTPINIT"), "OK",1000);
 
     sendATcmd(F("AT+HTTPINIT"), "OK",1000);
 
     sendATcmd(F("AT+HTTPPARA=\"CID\",1"), "OK",1000);
 
     sendATcmd(F("AT+HTTPPARA=\"CID\",1"), "OK",1000);
 
 
 
     sendATcmd(dataStr, "OK",1000);
 
     sendATcmd(dataStr, "OK",1000);
 
+
    sendATcmd(F("AT+HTTPACTION=0"), "200",2000);
  sendATcmd(F("AT+HTTPACTION=0"), "200",2000);
 
 
     sendATcmd(F("AT+HTTPTERM"), "OK",1000);
 
     sendATcmd(F("AT+HTTPTERM"), "OK",1000);
 
   //close the bearer connection
 
   //close the bearer connection
     sendATcmd(F("AT+SAPBR=0,1"), "OK",1000);
+
     sendATcmd(F("AT+SAPBR=0,1"), "OK",1000);  
   
+
     netUnreg();  
     netUnreg();
 
   
 
 
     lstTransmit = millis();
 
     lstTransmit = millis();
 
     charBuffAdvance();
 
     charBuffAdvance();
}
+
  }
 
+
  ////RETURNS TRUE IF SIM7000 SHOULD TRANSMIT////
////RETURNS TRUE IF SIM7000 SHOULD TRANSMIT////
+
  bool shouldTrasmit(){   
bool shouldTrasmit(){   
 
 
     //checks to see if it has been longer than max transmit interval
 
     //checks to see if it has been longer than max transmit interval
 
     if (MAXTRASMITINTERVAL < millis() - lstTransmit){
 
     if (MAXTRASMITINTERVAL < millis() - lstTransmit){
 
         return 1;
 
         return 1;
     }
+
     }
   
 
 
     //if all checks for transmit are not nessesary, return false
 
     //if all checks for transmit are not nessesary, return false
 
     return 0;
 
     return 0;
 
  }
 
  }
 
 
 
  ////power down cellular functionality////
 
  ////power down cellular functionality////
 
  void netUnreg(){
 
  void netUnreg(){
 
     sendATcmd(F("AT+CFUN=0"), "OK", 1000);
 
     sendATcmd(F("AT+CFUN=0"), "OK", 1000);
 
  }
 
  }
 
 
  ////register to network////
 
  ////register to network////
 
  void netReg(){
 
  void netReg(){
     sendATcmd(F("AT+CFUN=0"), "OK", 1000);
+
     sendATcmd(F("AT+CFUN=0"), "OK", 1000);  
   
 
 
     if(sendATcmd(F("AT+CFUN=1"), "+CPIN: READY", 1000) == 0){
 
     if(sendATcmd(F("AT+CFUN=1"), "+CPIN: READY", 1000) == 0){
 
         sendATcmd(F("AT+CFUN=6"), "OK", 10000);
 
         sendATcmd(F("AT+CFUN=6"), "OK", 10000);
         delay(10000);
+
         delay(10000);      
       
 
 
         sendATcmd(F("AT+CFUN=1"), "OK", 1000);
 
         sendATcmd(F("AT+CFUN=1"), "OK", 1000);
 
     }
 
     }
Line 406: Line 318:
 
     sendATcmd(F("AT+CREG?"), "+CREG: 0,1", 2000);
 
     sendATcmd(F("AT+CREG?"), "+CREG: 0,1", 2000);
 
  }
 
  }
 
 
  ////sends at command, checks for reply////
 
  ////sends at command, checks for reply////
 
  bool sendATcmd(String ATcommand, char* expctAns, unsigned int timeout){
 
  bool sendATcmd(String ATcommand, char* expctAns, unsigned int timeout){
 
     uint32_t timeStart;
 
     uint32_t timeStart;
 
     bool answer;
 
     bool answer;
     uint8_t a=0;
+
     uint8_t a=0;  
   
+
     do{a++;  
     do{a++;
+
     Serial.println(ATcommand);  
   
+
     answer=0;  
     Serial.println(ATcommand);
 
   
 
     answer=0;
 
   
 
 
     timeStart = 0;
 
     timeStart = 0;
 
 
 
     delay(100);
 
     delay(100);
 
 
     while( simCom.available() > 0) {
 
     while( simCom.available() > 0) {
 
         simCom.read();    // Clean the input buffer
 
         simCom.read();    // Clean the input buffer
     }
+
     }  
   
 
 
     simCom.println(ATcommand);    // Send the AT command  
 
     simCom.println(ATcommand);    // Send the AT command  
 
 
 
     uint8_t i = 0;
 
     uint8_t i = 0;
 
     timeStart = millis();
 
     timeStart = millis();
 
     memset(response, '\0', CHARBUFF);    // Initialize the string
 
     memset(response, '\0', CHARBUFF);    // Initialize the string
 
 
     // this loop waits for the answer
 
     // this loop waits for the answer
 
 
     do{
 
     do{
 
         if(simCom.available() != 0){     
 
         if(simCom.available() != 0){     
Line 446: Line 345:
 
                 answer = 1;
 
                 answer = 1;
 
             }
 
             }
         }  
+
         }                      
           
 
           
 
       
 
 
         // Waits for the asnwer with time out
 
         // Waits for the asnwer with time out
 
     }
 
     }
 
     while((answer == 0) && ((millis() - timeStart) < timeout));  
 
     while((answer == 0) && ((millis() - timeStart) < timeout));  
 
 
     if (expctAns == "0"){
 
     if (expctAns == "0"){
 
                 answer = 1;
 
                 answer = 1;
 
             }
 
             }
     Serial.println(response);
+
     Serial.println(response);  
   
+
     }while(answer == 0 && a < 5);  
     }while(answer == 0 && a < 5);
 
   
 
 
     a = 0;
 
     a = 0;
 
     return answer;
 
     return answer;
 
  }
 
  }
 
 
 
 
  ////initialises sim on arduino startup////
 
  ////initialises sim on arduino startup////
  void simInit(){
+
  void simInit(){  
   
 
 
   simOn();
 
   simOn();
    
+
   sendATcmd(F("AT+IPR=9600"),"OK",1000);  
      sendATcmd(F("AT+IPR=9600"),"OK",1000);
+
  sendATcmd(F("ATE0"),"OK",1000);
     
+
  sendATcmd(F("AT&W0"),"OK",1000);
      sendATcmd(F("ATE0"),"OK",1000);
+
   ////powers on SIM7000////
     
 
      sendATcmd(F("AT&W0"),"OK",1000);
 
    
 
}
 
 
 
 
 
////powers on SIM7000////
 
 
  void simOn() {
 
  void simOn() {
 
     //do check for if sim is on
 
     //do check for if sim is on
pinMode(PWRKEY, OUTPUT);
+
    pinMode(PWRKEY, OUTPUT);
pinMode(BOSL_TX, OUTPUT);
+
    pinMode(BOSL_TX, OUTPUT);
digitalWrite(BOSL_TX, HIGH);
+
    digitalWrite(BOSL_TX, HIGH);
pinMode(BOSL_RX, INPUT_PULLUP);
+
    pinMode(BOSL_RX, INPUT_PULLUP);
 
+
    digitalWrite(PWRKEY, LOW);
 
+
        // See spec sheets for your particular module
digitalWrite(PWRKEY, LOW);
+
        delay(100); // For SIM7000
// See spec sheets for your particular module
+
        digitalWrite(PWRKEY, HIGH);
delay(100); // For SIM7000
 
 
 
digitalWrite(PWRKEY, HIGH);
 
 
     delay(5000);
 
     delay(5000);
 
  }
 
  }
 
 
  ////powers off SIM7000////
 
  ////powers off SIM7000////
 
  void simOff() {
 
  void simOff() {
//  TX / RX pins off to save power
+
        //  TX / RX pins off to save power
 
+
        digitalWrite(BOSL_TX, LOW);
digitalWrite(BOSL_TX, LOW);
+
        digitalWrite(BOSL_RX, LOW);
digitalWrite(BOSL_RX, LOW);
+
        digitalWrite(PWRKEY, LOW);
 
+
        // See spec sheets for your particular module
digitalWrite(PWRKEY, LOW);
+
        delay(3000); // For SIM7000
 
+
        digitalWrite(PWRKEY, HIGH);
// See spec sheets for your particular module
 
delay(3000); // For SIM7000
 
 
 
digitalWrite(PWRKEY, HIGH);
 
 
     delay(10);
 
     delay(10);
 
  }
 
  }
 
 
  void CBCread(){
 
  void CBCread(){
     if (sendATcmd(F("AT+CBC"), "OK",1000)){
+
     if (sendATcmd(F("AT+CBC"), "OK",1000)){    
       
+
         storeCBCresponse();    
         storeCBCresponse();
 
       
 
 
     }
 
     }
 
  }
 
  }
  
 
 
 
 
 
 
 
Please visit our github for further details:
 
Please visit our github for further details:
 
#BoSL GitHub[[https://github.com/Monash-EPHM-Lab]]
 
#BoSL GitHub[[https://github.com/Monash-EPHM-Lab]]

Latest revision as of 05:33, 18 December 2019

Programming BoSL

The BoSL Board can be easily programmed with custom code using the Arduino IDE. Flowing these steps will setup the Arduino IDE software and configure it for working with BoSL.

1. Download the latest version of the Arduino IDE: https from: //www.arduino.cc/en/main/software

2. Install the IDE as per the installer instructions.

3. Connect a battery Pack to the BoSL DC power jack, ensure that the voltage is between 3.5 V and 5.5 V or the BoSL will be damaged. The supplied 3 AA battery pack is suggested for your first attempt.

4. Connect the BoSL board to the computer via the micro-USB port, the first time you plug a new board into your computer it may take a while to recognise as drivers are being installed.

5. Launch the Arduino IDE and open the script which you wish to program onto the BoSL

6. Under the Tools tab at the top of the window select Board: "Arduino Pro\Pro Mini"

7. Under the Tools tab at the top of the window select Processor: "ATmega328P (3.3V 8MHz)"

8. Under the Tools tab at the top of the window under Port select the COM port of your BoSL Board.

9. At the top bar click upload (round button with a right arrow). You should see some progress indication in the bottom console

10. The BoSL is now being programmed with your code, you should see lights flashing on the BoSL board.

11. Wait until the console says "Done Uploading" and disconnect the BoSL board.

Congratulations! You have just programmed the BoSL board, you can repeat these steps to upload new code to the BoSL.

If you'd like some sample code check out the script below which logs the Battery Voltage, Air Pressure, and Air Temperature using the on board sensor. Be sure to grab the appropriate libraries first.

#include <SoftwareSerial.h>
#include <LowPower.h>
#include <SparkFun_MS5803_I2C.h>
#define SIMCOM_7000 // SIM7000A/C/E/G
#define BAUDRATE 9600 // MUST be below 19200 (for stability) but 9600 is more stable
#define CHARBUFF 196 //SIM7000 serial response buffer //longer than 255 will cause issues
#define MAXTRASMITINTERVAL 180000//milli seconds
// For SIM7000 BoSL board
#define PWRKEY 4
#define DTR 5 // Connect with solder jumper
#define BOSL_RX 3 // Microcontroller RX
#define BOSL_TX 2 // Microcontroller TX
//Site specific config
#define SITEID "PUT SITE IDENTIFIER HERE"
#define APN "telstra.internet"
//default variable array (complilation purposes only)
bool defaultVars[6] = {1,1,1,1,1,1};
//millis timer variable 
extern volatile unsigned long timer0_millis;
//gobal variables 
char response[CHARBUFF]; //sim7000 serial response buffer
uint32_t lstTransmit = 0; //timestamp of last transmit (milli seconds)
String dataStr; //Transmit URL
//reponse stings
char temp[19];
char press[11];
char EC[12];
char air[11];
char Nview[3];
char CBC[5];
//previous reponse strings
char Lsttemp[19];
char Lstpress[11];
char LstEC[12];
char Lstair[11];
char LstNview[3];
//SIM7000 serial object
SoftwareSerial simCom = SoftwareSerial(BOSL_RX, BOSL_TX);
//DEFINE MS5803//
MS5803 MyAirMS5803(ADDRESS_LOW);
////clears char arrays////
void charBuffclr(bool clrVars[6] = defaultVars){
   if(clrVars[0]){
   memset(temp, '\0', 19);
   }
   if(clrVars[1]){
   memset(press, '\0', 11);
   }
   if(clrVars[2]){
   memset(EC, '\0', 12);
   }
   if(clrVars[3]){
   memset(air, '\0', 11);
   }
   if(clrVars[4]){
   memset(Nview, '\0', 3); 
   }
   if(clrVars[5]){
   memset(CBC, '\0', 5); 
   }
}
////clears char arrays////
void LstcharBuffclr(bool clrVars[6] = defaultVars){
   if(clrVars[0]){
   memset(Lsttemp, '\0', 19);
   }
   if(clrVars[1]){
   memset(Lstpress, '\0', 11);
   }
   if(clrVars[2]){
   memset(LstEC, '\0', 12);
   }
   if(clrVars[3]){
   memset(Lstair, '\0', 11);
   }
   if(clrVars[4]){
   memset(LstNview, '\0', 3);
   }
}
////stores coordinate data as previous and clears current arrays////
void charBuffAdvance(bool advVars[6] = defaultVars){
   uint8_t i;
    if(advVars[0]){
       for(i = 0; i < 19; i++){
           Lsttemp[i] = temp[i];
       }
   }
   if(advVars[1]){
       for(i = 0; i < 11; i++){
           Lstpress[i] = press[i];
       }
   }
   if(advVars[2]){
       for(i = 0; i < 12; i++){
           LstEC[i] = EC[i];
       }
   }
   if(advVars[3]){
       for(i = 0; i < 11; i++){
           Lstair[i] = air[i];
       }
   }
   if(advVars[4]){
       for(i = 0; i < 3; i++){
           LstNview[i] = Nview[i];
       }
   }
    bool clrVars[6] = {1,1,1,1,1,0};
    charBuffclr(clrVars);
}
////reads battery voltage from responce char array////
void storeCBCresponse(){
    bool end = 0;
   uint8_t x = 0;
   uint8_t j = 0;   
   bool clrVars[6] = {0,0,0,0,0,1};
   charBuffclr(clrVars);
   //loop through reponce to extract data
   for (uint8_t i=0; i < CHARBUFF; i++){
       //string splitting cases
       switch(response[i]){
       case ':':
           x = 9;
           j=0;
           i += 2;
           break;
       case ',':
           x++;
           j=0;
           break;
       case '\0':
           end = 1;
           break;
       case '\r':
           x++;
           j=0;
           break;
       }
       //write to char arrays
       if (response[i] != ','){
           switch(x){
               case 11:
                   CBC[j] = response[i];
               break;             
           }
           //increment char array counter
           j++;
       }
       //break loop when end flag is high
       if (end){
           i = CHARBUFF; 
       }
   }
}
void setup() {
  //clear buffers
 charBuffclr();
 LstcharBuffclr();
  //ensure sim is in the off state
 simOff();
  //begin serial
 Serial.begin(BAUDRATE);
 simCom.begin(BAUDRATE);
 Serial.println("initialising sim");
 //initialise sim (on arduino startup only)
    simInit();  
    netReg();
    netUnreg();
} 
void loop() {   
     charBuffclr()   
     Serial.println("PRESS");
     pressread();
     Serial.println("TEMP");
     tempread();   
     if(shouldTrasmit()){
           simOn();
           simOn();         
           netUnreg();  
           CBCread();         
           Transmit();
           simOff();
     } 
   Serial.println("Sleep"); 
   Sleepy(60);
}
void pressread(){
 float pressvar;
 uint32_t pressint;
 MyAirMS5803.reset();
 MyAirMS5803.begin();
 pressvar = MyAirMS5803.getPressure(ADC_4096);
 pressint = (int)10*pressvar;
 ltoa(pressint, press, 10);
}
void tempread(){
 float tempvar;
 MyAirMS5803.reset();
 MyAirMS5803.begin(); 
 tempvar = MyAirMS5803.getTemperature(CELSIUS, ADC_512); 
 itoa(int(100*tempvar), temp, 10);
}
////SLEEPS FOR SET TIME////
void Sleepy(uint16_t tsleep){ //Sleep Time in seconds   
   simCom.flush(); // must run before going to sleep	
   Serial.flush(); // ensures that all messages have sent through serial before arduino sleeps
   simOff();
   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
   delay(50);
   //advance millis timer as it is paused in sleep
   noInterrupts();
   timer0_millis += 8000;
   interrupts();       
   while(tsleep >= 16){
       LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
       delay(50);
       //advance millis timer as it is paused in sleep
       noInterrupts();
       timer0_millis += 8000;
       interrupts();       
      tsleep -= 8;
   }
}
////TRANSMITS DATA TO WEB////
void Transmit(){      
   dataStr = "AT+HTTPPARA=\"URL\",\"www.cartridgerefills.com.au/EoDC/databases/WriteMe.php?SiteName=";   
   dataStr += SITEID;
   dataStr += ".csv&T=";
   dataStr += temp;
   dataStr += "&D=";
   dataStr += press;
   dataStr += "&B=";
   dataStr += CBC;
   dataStr += "\"";  
   simOn();   
   netReg();  
   ///***check logic
  //set CSTT - if it is already set, then no need to do again...
       sendATcmd(F("AT+CSTT?"), "OK",1000);   
       if (strstr(response, "telstra.internet") != NULL){
           //this means the cstt has been set, so no need to set again!
           Serial.println("CSTT already set to APN ...no need to set again");
      } else {
           sendATcmd(F("AT+CSTT=\"telstra.internet\""), "OK",1000);
       }
   //close open bearer
   sendATcmd(F("AT+SAPBR=2,1"), "OK",1000);
   if (strstr(response, "1,1") == NULL){
       if (strstr(response, "1,3") == NULL){
       sendATcmd(F("AT+SAPBR=0,1"), "OK",1000);
       }
       sendATcmd(F("AT+SAPBR=1,1"), "OK",1000);
   }
   sendATcmd(F("AT+HTTPINIT"), "OK",1000);
   sendATcmd(F("AT+HTTPPARA=\"CID\",1"), "OK",1000);
   sendATcmd(dataStr, "OK",1000);
   sendATcmd(F("AT+HTTPACTION=0"), "200",2000);
   sendATcmd(F("AT+HTTPTERM"), "OK",1000);
 //close the bearer connection
   sendATcmd(F("AT+SAPBR=0,1"), "OK",1000); 
   netUnreg(); 
   lstTransmit = millis();
   charBuffAdvance();
 }
 ////RETURNS TRUE IF SIM7000 SHOULD TRANSMIT////
 bool shouldTrasmit(){   
   //checks to see if it has been longer than max transmit interval
   if (MAXTRASMITINTERVAL < millis() - lstTransmit){
       return 1;
   }  
   //if all checks for transmit are not nessesary, return false
   return 0;
}
////power down cellular functionality////
void netUnreg(){
   sendATcmd(F("AT+CFUN=0"), "OK", 1000);
}
////register to network////
void netReg(){
   sendATcmd(F("AT+CFUN=0"), "OK", 1000);    
   if(sendATcmd(F("AT+CFUN=1"), "+CPIN: READY", 1000) == 0){
       sendATcmd(F("AT+CFUN=6"), "OK", 10000);
       delay(10000);        
       sendATcmd(F("AT+CFUN=1"), "OK", 1000);
   }
   delay(2000);
   sendATcmd(F("AT+CREG?"), "+CREG: 0,1", 2000);
}
////sends at command, checks for reply////
bool sendATcmd(String ATcommand, char* expctAns, unsigned int timeout){
   uint32_t timeStart;
   bool answer;
   uint8_t a=0;    
   do{a++;    
   Serial.println(ATcommand);    
   answer=0;    
   timeStart = 0;
   delay(100);
   while( simCom.available() > 0) {
       simCom.read();    // Clean the input buffer
   }    
   simCom.println(ATcommand);    // Send the AT command 
   uint8_t i = 0;
   timeStart = millis();
   memset(response, '\0', CHARBUFF);    // Initialize the string
   // this loop waits for the answer
   do{
       if(simCom.available() != 0){    
           response[i] = simCom.read();
           i++;
           // check if the desired answer is in the response of the module
           if (strstr(response, expctAns) != NULL)    
           {
               answer = 1;
           }
       }                        
       // Waits for the asnwer with time out
   }
   while((answer == 0) && ((millis() - timeStart) < timeout)); 
   if (expctAns == "0"){
               answer = 1;
           }
   Serial.println(response);    
   }while(answer == 0 && a < 5);    
    a = 0;
    return answer;
}
////initialises sim on arduino startup////
void simInit(){    
 simOn();
 sendATcmd(F("AT+IPR=9600"),"OK",1000);    
 sendATcmd(F("ATE0"),"OK",1000);
 sendATcmd(F("AT&W0"),"OK",1000);
 ////powers on SIM7000////
void simOn() {
   //do check for if sim is on
   pinMode(PWRKEY, OUTPUT);
   pinMode(BOSL_TX, OUTPUT);
   digitalWrite(BOSL_TX, HIGH);
   pinMode(BOSL_RX, INPUT_PULLUP);
   digitalWrite(PWRKEY, LOW);
       // See spec sheets for your particular module
       delay(100); // For SIM7000
       digitalWrite(PWRKEY, HIGH);
   delay(5000);
}
////powers off SIM7000////
void simOff() {
       //  TX / RX pins off to save power
       digitalWrite(BOSL_TX, LOW);
       digitalWrite(BOSL_RX, LOW);
       digitalWrite(PWRKEY, LOW);
       // See spec sheets for your particular module
       delay(3000); // For SIM7000
       digitalWrite(PWRKEY, HIGH);
    delay(10);
}
void CBCread(){
    if (sendATcmd(F("AT+CBC"), "OK",1000)){      
        storeCBCresponse();      
    }
}

Please visit our github for further details:

  1. BoSL GitHub[[1]]