Team Walter / Mbed OS NonPingPong_PICO_LoRa

Dependencies:   SX1276GenericLib USBDevice

Fork of NonPingPong_PICO_LoRa by Walter Luu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 
00004 #include "lp.h"
00005 
00006 // Board pins related
00007 #include "PinMap.h"
00008 
00009 // LORA related
00010 #include "global_buffers.h"
00011 #include "GenericPingPong2.h"
00012 #define FEATURE_LORA
00013 #include "sx1276-mbed-hal.h"
00014 
00015 #include "main.h"
00016 
00017 // OT07 related
00018 #include "OT07_lib.h"      
00019 #define CONVERT_T_DELAY 30 
00020 
00021 // MAX44009 related
00022 #include "MAX44009_lib.h"    
00023 
00024 // AO32 related
00025 #include "AO32_lib.h" 
00026 
00027 // AO19 related
00028 #include "AO19_lib.h"
00029 
00030 // Virtual COM related
00031 #include "USBSerial.h"      // for virtual COM
00032 #define BS          8       // ASCII Back Space
00033 #define CR          13      // ASCII Carriage Return
00034 
00035 // Virtual serial port over USB TODO NEW VID PID NEEDED!!
00036 USBSerial pc(0x0B6A, 0x0042, 0x0001, false);
00037 
00038 /***************************************************************************
00039  * LEDs Instantiation
00040  **************************************************************************/
00041 DigitalOut myled(LED2);          // LED = LED2 green
00042 DigitalOut rLED(LED1);          // red LED
00043 DigitalOut bLED(LED3);          // blue LED
00044 //DigitalOut *led3;
00045 
00046 /***************************************************************************
00047  * I2C Bus Instantiation
00048  **************************************************************************/
00049 I2C i2cBus0(P1_6, P1_7);            // I2C of MAX32625PICO
00050 
00051 
00052 bool get_data_flag = false;         // used for data tramission frequency on the SENSOR side
00053 bool print_data_flag = false;       // used for data display on the GATEWAY side
00054 
00055 //Timer setup
00056 Ticker timer_1;                     // timer for data tramission frequency on the SENSOR side
00057 Ticker timer_M;                     // timer for data print out on the GATEWAY side
00058 
00059 void onTimerInterrupt(){
00060     get_data_flag = true;
00061 }
00062 
00063 void onGatewayInterrupt(){
00064     print_data_flag = true;
00065 }
00066 
00067 int main() {
00068 
00069 /***************************************************************************
00070 
00071  
00072  
00073     /* Setup begins here: */
00074     #if   MASTER == 1 // Master Device
00075         rLED = LED_ON;      // red LED on
00076         myled = LED_OFF;
00077         bLED = LED_OFF;
00078     #elif SLAVE == 1 // Slave Device
00079         rLED = LED_OFF;
00080         myled = LED_OFF;
00081         bLED = LED_ON;      // blue LED on
00082         
00083         sleep();            // put slave in sleep mode 
00084         
00085     #endif
00086     
00087      /***************************************************************************
00088      * Combined Payload Buffers for LoRa Communications
00089      **************************************************************************/
00090     uint8_t BufferTx[BufferSizeTx];             // more info in global_buffers.h
00091     uint8_t BufferRx[BufferSizeRx];             // more info in global_buffers.h
00092         
00093     /***************************************************************************
00094      * MAX30208 Data Buffers
00095      **************************************************************************/
00096     #if   MASTER == 1 // Master Device
00097         char curr_raw_temp_from_slave[size_of_MAX30208];   // to match data type
00098         char prev_raw_temp_from_slave[size_of_MAX30208];  
00099     #elif SLAVE == 1 // Slave Device
00100         uint8_t curr_raw_temp_to_master[size_of_MAX30208];
00101     #endif
00102     
00103     /***************************************************************************
00104      * MAX44009 Data Buffers
00105      **************************************************************************/
00106     #if   MASTER == 1 // Master Device
00107         char curr_raw_light_from_slave[size_of_MAX44009];    // to match data type
00108         char prev_raw_light_from_slave[size_of_MAX44009];
00109     #elif SLAVE == 1 // Slave Device
00110         uint8_t curr_raw_light_to_master[size_of_MAX44009];
00111     #endif
00112     
00113     /***************************************************************************
00114      * MAX20361 Data Buffers
00115      **************************************************************************/
00116     #if   MASTER == 1 // Master Device
00117         char curr_raw_AO32_from_slave[size_of_MAX20361];            // to match data type
00118         char prev_raw_AO32_from_slave[size_of_MAX20361];
00119     #elif SLAVE == 1 // Slave Device
00120         uint8_t curr_raw_AO32_to_master[size_of_MAX20361];
00121     #endif
00122     
00123     /***************************************************************************
00124      * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send
00125      **************************************************************************/
00126     SX1276PingPongSetup(BufferTx, BufferRx, &pc);
00127     
00128 //    Pointer Tutorials
00129 //    https://www.tutorialspoint.com/cprogramming/c_pointers.htm
00130     
00131     /***************************************************************************
00132      * Loop Counter
00133      **************************************************************************/
00134     int loopCnt = 0;
00135     
00136     #if MASTER == 1
00137      //************* init ticker timer callbacks  ****************
00138     timer_M.attach(&onGatewayInterrupt, 3);        //Gateway data print out frequency
00139     
00140     #endif
00141     
00142     #if SLAVE == 1
00143     //************* init ticker timer callbacks  ****************
00144     timer_1.attach(&onTimerInterrupt, 3);        //Sensor data transmission frequency
00145     
00146     //************* OT07 Variables  **************** 
00147     char rawtempdata[2];          
00148     char OT07_i2c_add = 0xA0;
00149     
00150     //************* MAX44009 Variables  ****************
00151     char rawluxdata[2];          
00152     char MAX44009_i2c_add = 0x94;      // 0b1001 010x
00153     
00154     //************* AO32 Variables  ****************
00155     char rawOCVdata[1];                 // only needs 1
00156     char rawCntdata[2];
00157     char AO32_i2c_add = 0x2A;      // 0b0010 101x
00158     
00159      //************* AO19 Settings  ****************
00160     // Enable AO19
00161     char AO19_i2c_add = 0xD0;      // 
00162     char AO19_BB_CFG[2];              // store result of AO19's register 0x01 BBstCfg0
00163     int AO19_read = AO19_read_register(&i2cBus0, AO19_i2c_add, AO19_BB_CFG0, AO19_BB_CFG);
00164     
00165 //    if (AO19_read) {
00166 //        pc.printf("AO19 Initial Read fail!\r\n");
00167 //    }
00168 //    else {
00169 //        pc.printf("AO19 Initial Read success!\r\n");
00170 //    } 
00171     
00172     int AO19_en = AO19_write_register(&i2cBus0, AO19_i2c_add, AO19_BB_CFG0, AO19_BB_CFG[0]|0x80);
00173     
00174    // if (AO19_en) {
00175 //        pc.printf("AO19 Enabled fail!\r\n");
00176 //    }
00177 //    else {
00178 //        pc.printf("AO19 Enabledsuccess!\r\n");
00179 //    } 
00180     
00181     
00182     #endif
00183     
00184     while (1) {
00185     
00186     #if SLAVE == 1          // only send sensor data
00187     
00188     // Slave sleeps before interrupt calls
00189     sleep();
00190     
00191     if(get_data_flag) {
00192         
00193         //reset the flag
00194         get_data_flag = false;
00195 //        pc.printf("Timer interval reached!\r\n");
00196         
00197         /***************************************************************************
00198         * Temperature Sensor Data Measurement
00199         **************************************************************************/
00200         // obtain register hex values
00201         convert_temperature(&i2cBus0, OT07_i2c_add);  //send OW convert selected device
00202         wait_ms(CONVERT_T_DELAY);  //wait 20 ms for convert temperature to complete
00203         int temp_error = OT07_read_register(&i2cBus0, OT07_i2c_add, OT07_FIFO_DATA, rawtempdata, 2); 
00204         double tempFinal = calc_temperature(rawtempdata);
00205         
00206         //fill raw temp data into the array
00207         curr_raw_temp_to_master[0] = rawtempdata[0];
00208         curr_raw_temp_to_master[1] = rawtempdata[1];      
00209         
00210         /***************************************************************************
00211         * Light Intensity Sensor Data Measurement
00212         **************************************************************************/
00213         // obtain register hex values
00214         int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
00215         int luxFinal = (int) (calc_lux(rawluxdata));
00216         
00217         //fill raw lux data into the array
00218         curr_raw_light_to_master[0] = rawluxdata[0];
00219         curr_raw_light_to_master[1] = rawluxdata[1];
00220         
00221         /***************************************************************************
00222         * Solar Harvester Data Measurement
00223         **************************************************************************/
00224         
00225         int ocv_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_VOC, rawOCVdata);
00226         int cnt_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_HARV_H, rawCntdata, 2); // burst read 2 bytes
00227         
00228         //calculate open circuit voltage from data
00229 //        double voltage = (double)(rawOCVdata[0]) / 100;
00230         double OCVFinal = calc_OCV(rawOCVdata);
00231         
00232         //calculate harvesting counts from data
00233         int countFinal = calc_Harvest(rawCntdata);
00234         
00235         //fill raw AO32 data into the array
00236         curr_raw_AO32_to_master[0] = rawOCVdata[0];         // Fill OCV hex first
00237         curr_raw_AO32_to_master[1] = rawCntdata[0];         // Fill Harvesting count high byte
00238         curr_raw_AO32_to_master[2] = rawCntdata[1];         // Fill Harvesting count low byte
00239         
00240         // print out sensor data
00241          pc.printf("SENSOR: [%.3f] [%d] [%.2f] [%d] [0]\r\n", tempFinal, luxFinal, OCVFinal, countFinal);
00242         
00243         /***************************************************************************
00244         * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
00245         **************************************************************************/
00246         memcpy(&BufferTx[tx_idx_MAX30208],  curr_raw_temp_to_master, size_of_MAX30208);
00247         memcpy(&BufferTx[tx_idx_MAX44009],  curr_raw_light_to_master, size_of_MAX44009);
00248         memcpy(&BufferTx[tx_idx_MAX20361],  curr_raw_AO32_to_master,  size_of_MAX20361);
00249 
00250         /***************************************************************************
00251         * LoRa Communication: Send Sensor Data
00252         **************************************************************************/
00253 //        SX1276PingPong(); 
00254         SX1276SlaveSendData();
00255         loopCnt = loopCnt + 1;
00256     } // end of transmission frequency for slave
00257     #endif    
00258     
00259     
00260     #if MASTER == 1         // only Receive Sensor Data
00261     
00262      /***************************************************************************
00263      * LoRa Communication: Gateway Receive Sensor Data
00264      **************************************************************************/
00265 //    SX1276PingPong();
00266     if(SX1276MasterCheckForNewData())
00267     {
00268     
00269     /***************************************************************************
00270      * Fill Main Program Buffers With Data From Received Payload Buffer
00271      **************************************************************************/
00272     memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208],  size_of_MAX30208);
00273     memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
00274     memcpy(curr_raw_AO32_from_slave,  &BufferRx[rx_idx_MAX20361],  size_of_MAX20361);
00275 
00276     /***************************************************************************
00277     * Master Device: Print out Slave Data
00278     **************************************************************************/
00279     
00280     double tempResult = calc_temperature(curr_raw_temp_from_slave);
00281     int luxResult = (int) calc_lux(curr_raw_light_from_slave);
00282     
00283     char OCVrawHex[1];
00284     OCVrawHex[0] = curr_raw_AO32_from_slave[0];
00285     char CntrawHex[2];
00286     CntrawHex[0] = curr_raw_AO32_from_slave[1];
00287     CntrawHex[1] = curr_raw_AO32_from_slave[2];
00288     
00289     double OCVResult = calc_OCV(OCVrawHex);
00290     int CntResult = calc_Harvest(CntrawHex);
00291         
00292     //reset the flag
00293     print_data_flag = false;        // no need
00294     pc.printf("MSG: [%.3f] [%d] [%.2f] [%d] [0]\r\n", tempResult, luxResult, OCVResult, CntResult);
00295     
00296     }   // end of SX1276MasterCheckForNewData
00297 
00298     loopCnt = loopCnt + 1;
00299     
00300     #endif
00301         
00302     sleep();        // going back to sleep mode    
00303         
00304     } // end of while(1) loop
00305         
00306 }  // end of main()