getting location and activity

Dependencies:   Cayenne-LPP GPS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbed_trace.h"
00003 #include "mbed_events.h"
00004 #include "LoRaWANInterface.h"
00005 #include "SX1276_LoRaRadio.h"
00006 #include "CayenneLPP.h"
00007 #include "lora_radio_helper.h"
00008 #include "standby.h"
00009 #include "GPS.h"
00010 #include "ADXL345_I2C.h"
00011 
00012 
00013 #define GPSBaud 9600
00014 #define     STANDBY_TIME_S        1 * 60
00015 #define     SENSOR_READ_ATTEMPTS  3
00016 #define     SENSOR_WAIT_TIME      3000 //slow sensor, no more than once per 2 seconds
00017   
00018 
00019 
00020 static uint32_t DEVADDR_1 = 0x260118F6;
00021 static uint8_t NWKSKEY_1[] = { 0x90, 0xF9, 0x6E, 0x7E, 0x9C, 0x38, 0x75, 0xD5, 0x59, 0x75, 0x6D, 0x91, 0x62, 0x22, 0x73, 0x1C };
00022 static uint8_t APPSKEY_1[] = { 0x73, 0x0B, 0x09, 0xB7, 0x93, 0xBA, 0x69, 0xEF, 0x69, 0xC4, 0x0E, 0xEF, 0xBD, 0x43, 0x19, 0x61 };
00023 
00024 
00025 static Serial pc(SERIAL_TX, SERIAL_RX, 9600);
00026 static GPS gps(PA_9, PB_7);
00027 ADXL345_I2C accelerometer(D14, D15);
00028 
00029 
00030 // The port we're sending and receiving on
00031 #define MBED_CONF_LORA_APP_PORT     15
00032 
00033 
00034 
00035 // EventQueue is required to dispatch events around
00036 static EventQueue ev_queue;
00037 
00038 // Constructing Mbed LoRaWANInterface and passing it down the radio object.
00039 static LoRaWANInterface lorawan(radio);
00040 
00041 // Application specific callbacks
00042 static lorawan_app_callbacks_t callbacks;
00043 
00044 // LoRaWAN stack event handler
00045 static void lora_event_handler(lorawan_event_t event);
00046 
00047 // Send a message over LoRaWAN
00048 static void send_message() {
00049     CayenneLPP payload(50);
00050       
00051         if( gps.sample() == 1) {
00052            
00053           float latitude = gps.latitude;
00054            float longitude = gps.longitude;
00055             float altitude= (gps.utc/100)+465;
00056             
00057             
00058             printf("latitude: %0.4f, longitude: %0.4f, altitude: %f\r\n",latitude,longitude,altitude);
00059             
00060            payload.addGPS(2, latitude,longitude,altitude);  
00061              
00062             
00063             wait(1);
00064             
00065              } 
00066              else
00067              {
00068                  printf("no gps detected");
00069                  }
00070        
00071        int readings[3] = {0, 0, 0};
00072     
00073     printf("\n Animal activity \n");
00074     
00075      printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
00076     wait(.001);
00077     
00078     if (accelerometer.setPowerControl(0x00)){
00079          printf("didn't intitialize power control\n"); 
00080          }
00081      //Full resolution, +/-16g, 4mg/LSB.
00082      wait(.001);
00083      
00084      if(accelerometer.setDataFormatControl(0x0B)){
00085         printf("didn't set data format\n");
00086          }
00087      wait(.001);
00088      
00089      //3.2kHz data rate.
00090      if(accelerometer.setDataRate(ADXL345_3200HZ)){
00091         printf("didn't set data rate\n");
00092             }
00093      wait(.001);
00094      
00095      //Measurement mode.
00096      
00097      if(accelerometer.setPowerControl(MeasurementMode)) {
00098         printf("didn't set the power control to measurement\n"); 
00099           } 
00100          wait(0.01);
00101          
00102          accelerometer.getOutput(readings);
00103          
00104 
00105         printf("X-axis= %i, Y-axis= %i, Z-axis= %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
00106         
00107         payload.addAccelerometer(3,  readings[0], readings[1], readings[2]);
00108         
00109         wait(2);
00110 
00111        
00112     
00113        
00114        
00115 
00116     if (payload.getSize() > 0) {
00117       printf("Sending %d bytes\n", payload.getSize());
00118 
00119       int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload.getBuffer(), payload.getSize(), MSG_UNCONFIRMED_FLAG);
00120 
00121     // for some reason send() ret\urns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk
00122       if (retcode < 0) {
00123         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n")
00124             : printf("send() - Error code %d\n", retcode);
00125 
00126         standby(STANDBY_TIME_S);
00127       }
00128 
00129       printf("%d bytes scheduled for transmission\n", retcode);
00130     }
00131 
00132     else
00133       standby(STANDBY_TIME_S);
00134 }
00135 
00136 int main() {
00137     set_time(0);
00138 
00139     printf("\r==========================\n");
00140     printf("\r  cattle monitor     \n");
00141     printf("\r==========================\n");
00142 
00143     printf("Sending every %d seconds\n", STANDBY_TIME_S);
00144 
00145     // Enable trace output for this demo, so we can see what the LoRaWAN stack does
00146     mbed_trace_init();
00147 
00148     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
00149         printf("LoRa initialization failed!\n");
00150         return -1;
00151     }
00152 
00153     // prepare application callbacks
00154     callbacks.events = mbed::callback(lora_event_handler);
00155     lorawan.add_app_callbacks(&callbacks);
00156 
00157     // Disable adaptive data rating
00158     if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
00159         printf("\rdisable_adaptive_datarate failed!\n");
00160         return -1;
00161     }
00162 
00163     lorawan.set_datarate(0); // SF12BW125
00164 
00165     lorawan_connect_t connect_params;
00166     connect_params.connect_type = LORAWAN_CONNECTION_ABP;
00167 
00168     connect_params.connection_u.abp.dev_addr = DEVADDR_1;
00169     connect_params.connection_u.abp.nwk_skey = NWKSKEY_1;
00170     connect_params.connection_u.abp.app_skey = APPSKEY_1;
00171 
00172     lorawan_status_t retcode = lorawan.connect(connect_params);
00173 
00174     if (retcode == LORAWAN_STATUS_OK ||
00175         retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
00176     } else {
00177         printf("Connection error, code = %d\n", retcode);
00178         return -1;
00179     }
00180 
00181     printf("Connection - In Progress ...\r\n");
00182 
00183     // make your event queue dispatching events forever
00184     ev_queue.dispatch_forever();
00185 }
00186 
00187 // This is called from RX_DONE, so whenever a message came in
00188 static void receive_message()
00189 {
00190     uint8_t rx_buffer[50] = { 0 };
00191     int16_t retcode;
00192     retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
00193                               sizeof(rx_buffer),
00194                               MSG_UNCONFIRMED_FLAG);
00195 
00196     if (retcode < 0) {
00197         printf("receive() - Error code %d\n", retcode);
00198         return;
00199     }
00200 
00201     printf("Data received on port %d (length %d): ", MBED_CONF_LORA_APP_PORT, retcode);
00202 
00203     for (uint8_t i = 0; i < retcode; i++) {
00204         printf("%02x ", rx_buffer[i]);
00205     }
00206     printf("\n");
00207 }
00208 
00209 // Event handler
00210 static void lora_event_handler(lorawan_event_t event) {
00211     switch (event) {
00212         case CONNECTED:
00213             printf("Connection - Successful\n");
00214             ev_queue.call_in(1000, &send_message);
00215             break;
00216         case DISCONNECTED:
00217             ev_queue.break_dispatch();
00218             printf("Disconnected Successfully\n");
00219             break;
00220         case TX_DONE:
00221             printf("Message Sent to Network Server\n");
00222             standby(STANDBY_TIME_S);
00223             break;
00224         case TX_TIMEOUT:
00225         case TX_ERROR:
00226         case TX_CRYPTO_ERROR:
00227         case TX_SCHEDULING_ERROR:
00228             printf("Transmission Error - EventCode = %d\n", event);
00229             standby(STANDBY_TIME_S);
00230             break;
00231         case RX_DONE:
00232             printf("Received message from Network Server\n");
00233             receive_message();
00234             break;
00235         case RX_TIMEOUT:
00236         case RX_ERROR:
00237             printf("Error in reception - Code = %d\n", event);
00238             break;
00239         case JOIN_FAILURE:
00240             printf("OTAA Failed - Check Keys\n");
00241             break;
00242         default:
00243             MBED_ASSERT("Unknown Event");
00244     }
00245 }