Difference between revisions of "BoSL code"

From BoSL Wiki
Jump to navigation Jump to search
Line 31: Line 31:
  
 
  #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
#define DTR 5 // Connect with solder jumper
+
#define DTR 5 // Connect with solder jumper
#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];
char press[11];
+
char press[11];
char EC[12];
+
char EC[12];
char air[11];
+
char air[11];
char Nview[3];
+
char Nview[3];
char CBC[5];
+
char CBC[5];
//previous reponse strings
+
//previous reponse strings
char Lsttemp[19];
+
char Lsttemp[19];
char Lstpress[11];
+
char Lstpress[11];
char LstEC[12];
+
char LstEC[12];
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){
 
     if(clrVars[0]){
 
     if(clrVars[0]){
 
     memset(temp, '\0', 19);
 
     memset(temp, '\0', 19);
Line 102: Line 102:
 
     memset(CBC, '\0', 5);  
 
     memset(CBC, '\0', 5);  
 
     }
 
     }
}
+
}
  
////clears char arrays////
+
////clears char arrays////
void LstcharBuffclr(bool clrVars[6] = defaultVars){
+
void LstcharBuffclr(bool clrVars[6] = defaultVars){
 
     if(clrVars[0]){
 
     if(clrVars[0]){
 
     memset(Lsttemp, '\0', 19);
 
     memset(Lsttemp, '\0', 19);
Line 121: Line 121:
 
     memset(LstNview, '\0', 3);
 
     memset(LstNview, '\0', 3);
 
     }
 
     }
}
+
}
  
////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;
 
      
 
      
Line 156: Line 156:
 
      
 
      
 
     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;
Line 209: Line 209:
 
     }
 
     }
  
}
+
}
  
void setup() {
+
void setup() {
 
          
 
          
 
   //clear buffers
 
   //clear buffers
Line 231: Line 231:
 
     netUnreg();
 
     netUnreg();
  
}
+
}
 
      
 
      
void loop() {
+
void loop() {
 
        
 
        
 
       charBuffclr();
 
       charBuffclr();
Line 261: Line 261:
 
     Sleepy(60);
 
     Sleepy(60);
 
   
 
   
}
+
}
  
  
void pressread(){
+
void pressread(){
 
   float pressvar;
 
   float pressvar;
 
   uint32_t pressint;
 
   uint32_t pressint;
Line 276: Line 276:
  
 
   ltoa(pressint, press, 10);
 
   ltoa(pressint, press, 10);
}
+
}
  
void tempread(){
+
void tempread(){
 
   float tempvar;
 
   float tempvar;
 
    
 
    
Line 287: Line 287:
 
    
 
    
 
   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
Line 317: Line 317:
 
         tsleep -= 8;
 
         tsleep -= 8;
 
     }
 
     }
}
+
}
  
////TRANSMITS DATA TO WEB////
+
////TRANSMITS DATA TO WEB////
void Transmit(){
+
void Transmit(){
  
 
      
 
      
Line 374: Line 374:
 
     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){
Line 385: Line 385:
 
     //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);
 
      
 
      
Line 405: Line 405:
 
     delay(2000);
 
     delay(2000);
 
     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;
Line 463: Line 463:
 
     a = 0;
 
     a = 0;
 
     return answer;
 
     return answer;
}
+
}
  
  
  
////initialises sim on arduino startup////
+
////initialises sim on arduino startup////
void simInit(){
+
void simInit(){
 
      
 
      
 
   simOn();
 
   simOn();
Line 478: Line 478:
 
       sendATcmd(F("AT&W0"),"OK",1000);
 
       sendATcmd(F("AT&W0"),"OK",1000);
 
    
 
    
}
+
}
  
  
////powers on SIM7000////
+
////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);
Line 496: Line 496:
 
digitalWrite(PWRKEY, HIGH);
 
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
  
Line 507: Line 507:
 
digitalWrite(PWRKEY, LOW);
 
digitalWrite(PWRKEY, LOW);
  
// See spec sheets for your particular module
+
// See spec sheets for your particular module
delay(3000); // For SIM7000
+
delay(3000); // For SIM7000
  
digitalWrite(PWRKEY, HIGH);
+
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();
 
          
 
          
    }
+
    }
}
+
}
  
  

Revision as of 05:31, 13 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.

#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]]