updated

Dependencies:   MTS-Serial libmDot mbed-rtos mbed

Fork of mDot_LoRa_Street_light by HM_IOT_Demo

Committer:
venubk
Date:
Tue Oct 17 11:52:13 2017 +0000
Revision:
9:531bddbe36a9
Parent:
6:db60638d7f9a
energy meter integrated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ivaneco 0:451bc0b1d5ee 1 #include "mbed.h"
ivaneco 0:451bc0b1d5ee 2 #include "mDot.h"
hariom 2:20a5d4681abf 3 #include "wrapper.h"
ivaneco 0:451bc0b1d5ee 4 #include "MTSLog.h"
ivaneco 0:451bc0b1d5ee 5 #include <string>
hariom 2:20a5d4681abf 6 #include"Thread.h"
hariom 2:20a5d4681abf 7 //#include <vector>
hariom 2:20a5d4681abf 8 //#include <algorithm>
ivaneco 0:451bc0b1d5ee 9
ivaneco 0:451bc0b1d5ee 10 // these options must match the settings on your Conduit
ivaneco 0:451bc0b1d5ee 11 // uncomment the following lines and edit their values to match your configuration
hariom 2:20a5d4681abf 12 char *config_network_name = "pathfinder";
hariom 2:20a5d4681abf 13 char *config_network_pass = "pathfinder";
venubk 9:531bddbe36a9 14 static int config_frequency_sub_band = 7;
hariom 2:20a5d4681abf 15
hariom 2:20a5d4681abf 16 //void startLoraTask(void);
hariom 2:20a5d4681abf 17 //void startZigbeeTask(void);
hariom 2:20a5d4681abf 18 //void startBLETask(void);
ivaneco 0:451bc0b1d5ee 19
hariom 2:20a5d4681abf 20 //void loraTask(void const *args);
hariom 2:20a5d4681abf 21 //void zigbeeTask(void const *args);
hariom 2:20a5d4681abf 22 //void bleTask(void const *args);
hariom 2:20a5d4681abf 23
hariom 2:20a5d4681abf 24 void receive_data_from_gateway(void const *args);
hariom 2:20a5d4681abf 25 void send_data_to_gateway(void const *args);
venubk 6:db60638d7f9a 26 void SPI_Init();
hariom 2:20a5d4681abf 27
hariom 2:20a5d4681abf 28 int main()
hariom 2:20a5d4681abf 29 {
hariom 2:20a5d4681abf 30 printf("starting main thread\r\n");
ivaneco 0:451bc0b1d5ee 31
hariom 2:20a5d4681abf 32 int ret;
hariom 2:20a5d4681abf 33 //printf("lora task started\r\n");
hariom 2:20a5d4681abf 34 printf("Initializing Lora mdot lib\r\n");
hariom 2:20a5d4681abf 35 init_mdot();
ivaneco 0:451bc0b1d5ee 36
hariom 2:20a5d4681abf 37 printf("Resetting Lora mdot config\r\n");
hariom 2:20a5d4681abf 38 reset_config();
ivaneco 0:451bc0b1d5ee 39
hariom 2:20a5d4681abf 40 printf("Setting Log info lavel\r\n");
hariom 2:20a5d4681abf 41 set_log_lavel(4);
hariom 2:20a5d4681abf 42
hariom 2:20a5d4681abf 43 //set_public_network(false);
hariom 2:20a5d4681abf 44
ivaneco 0:451bc0b1d5ee 45 // set up the mDot with our network information: frequency sub band, network name, and network password
ivaneco 0:451bc0b1d5ee 46 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
ivaneco 0:451bc0b1d5ee 47
ivaneco 0:451bc0b1d5ee 48 // frequency sub band is only applicable in the 915 (US) frequency band
ivaneco 0:451bc0b1d5ee 49 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
ivaneco 0:451bc0b1d5ee 50 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
hariom 2:20a5d4681abf 51
hariom 2:20a5d4681abf 52 printf("setting frequency sub band %d\r\n", config_frequency_sub_band);
hariom 2:20a5d4681abf 53 ret = set_frequency_sub_band(config_frequency_sub_band);
hariom 2:20a5d4681abf 54 if (ret < 0) {
hariom 2:20a5d4681abf 55 logError("failed to set frequency sub band");
ivaneco 0:451bc0b1d5ee 56 }
ivaneco 0:451bc0b1d5ee 57
hariom 2:20a5d4681abf 58 printf("setting network name: %s\r\n", config_network_name);
hariom 2:20a5d4681abf 59 ret = set_network_name(config_network_name);
hariom 2:20a5d4681abf 60 if (ret < 0) {
hariom 2:20a5d4681abf 61 logError("failed to set network name");
ivaneco 0:451bc0b1d5ee 62 }
ivaneco 0:451bc0b1d5ee 63
hariom 2:20a5d4681abf 64 printf("setting network password: %s\r\n", config_network_pass);
hariom 2:20a5d4681abf 65 ret = set_network_passphrase(config_network_pass);
hariom 2:20a5d4681abf 66 if (ret < 0) {
hariom 2:20a5d4681abf 67 logError("failed to set network password");
ivaneco 0:451bc0b1d5ee 68 }
ivaneco 0:451bc0b1d5ee 69
hariom 2:20a5d4681abf 70 set_txpower(14);
hariom 2:20a5d4681abf 71
ivaneco 0:451bc0b1d5ee 72 // a higher spreading factor allows for longer range but lower throughput
ivaneco 0:451bc0b1d5ee 73 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
ivaneco 0:451bc0b1d5ee 74 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
hariom 2:20a5d4681abf 75 printf("setting TX spreading factor\r\n");
hariom 2:20a5d4681abf 76 ret = set_tx_datarate();
hariom 2:20a5d4681abf 77 if (ret < 0) {
hariom 2:20a5d4681abf 78 logError("failed to set TX datarate");
ivaneco 0:451bc0b1d5ee 79 }
ivaneco 0:451bc0b1d5ee 80
ivaneco 0:451bc0b1d5ee 81 // request receive confirmation of packets from the gateway
hariom 2:20a5d4681abf 82 printf("enabling ACKs\r\n");
hariom 2:20a5d4681abf 83 ret = set_ack(true);
hariom 2:20a5d4681abf 84 if (ret < 0) {
hariom 2:20a5d4681abf 85 logError("failed to enable ACKs");
ivaneco 0:451bc0b1d5ee 86 }
ivaneco 0:451bc0b1d5ee 87
ivaneco 0:451bc0b1d5ee 88 // save this configuration to the mDot's NVM
hariom 2:20a5d4681abf 89 printf("saving config\r\n");
hariom 2:20a5d4681abf 90 ret = save_config();
hariom 2:20a5d4681abf 91 if (ret < 0) {
ivaneco 0:451bc0b1d5ee 92 logError("failed to save configuration");
ivaneco 0:451bc0b1d5ee 93 }
hariom 2:20a5d4681abf 94
ivaneco 0:451bc0b1d5ee 95 //*******************************************
ivaneco 0:451bc0b1d5ee 96 // end of configuration
ivaneco 0:451bc0b1d5ee 97 //*******************************************
ivaneco 0:451bc0b1d5ee 98
ivaneco 0:451bc0b1d5ee 99 // attempt to join the network
hariom 2:20a5d4681abf 100 printf("joining network\r\n");
hariom 2:20a5d4681abf 101 ret = join_network();
hariom 2:20a5d4681abf 102 /*if (ret < 0) {
hariom 2:20a5d4681abf 103 printf("error in join network\r\n");
hariom 2:20a5d4681abf 104 }*/
hariom 2:20a5d4681abf 105 logInfo("creating thread for sending and receving data from gateway");
venubk 6:db60638d7f9a 106 SPI_Init();
hariom 2:20a5d4681abf 107 Thread send_lora_thread(send_data_to_gateway);
hariom 2:20a5d4681abf 108 Thread receive_lora_thread(receive_data_from_gateway);
hariom 2:20a5d4681abf 109
hariom 2:20a5d4681abf 110 while(1) {
hariom 2:20a5d4681abf 111 osDelay(5000);
ivaneco 0:451bc0b1d5ee 112 }
ivaneco 0:451bc0b1d5ee 113 return 0;
ivaneco 0:451bc0b1d5ee 114 }