clean

Dependencies:   GroveGPS libmDot-mbed5 ISL29011

Committer:
meachwill
Date:
Mon Jan 06 04:48:55 2020 +0000
Revision:
40:6dea2ced2c87
clean

Who changed what in which revision?

UserRevisionLine numberNew contents of line
meachwill 40:6dea2ced2c87 1 #include "dot_util.h"
meachwill 40:6dea2ced2c87 2 #include "RadioEvent.h"
meachwill 40:6dea2ced2c87 3 #include "DS3231.h"
meachwill 40:6dea2ced2c87 4 #include <string>
meachwill 40:6dea2ced2c87 5 #include <vector>
meachwill 40:6dea2ced2c87 6 #include <algorithm>
meachwill 40:6dea2ced2c87 7 #include <iostream>
meachwill 40:6dea2ced2c87 8 #include <sstream>
meachwill 40:6dea2ced2c87 9 #include <time.h>
meachwill 40:6dea2ced2c87 10 #include "GroveGPS.h"
meachwill 40:6dea2ced2c87 11 void read_gps();
meachwill 40:6dea2ced2c87 12 using namespace std;
meachwill 40:6dea2ced2c87 13 static uint8_t config_app_eui[] = {0x86,0xe4,0xef,0xc7,0x10,0x4f,0x68,0x29 };
meachwill 40:6dea2ced2c87 14 static uint8_t config_app_key[] = { 0xa3,0x46,0xb6,0xfa,0xef,0x2b,0xd3,0x3c,0x16,0xfe,0x9b,0x1d,0x8d,0x47,0xa1,0x1d};
meachwill 40:6dea2ced2c87 15 //USER ADD
meachwill 40:6dea2ced2c87 16 char clock_str[80] = {0};
meachwill 40:6dea2ced2c87 17 GroveGPS gps(PA_2,PA_3);
meachwill 40:6dea2ced2c87 18 char latBuffer[16], lonBuffer[16];
meachwill 40:6dea2ced2c87 19 rtl hrtl = {0};
meachwill 40:6dea2ced2c87 20 // use the same subband as gateway that gateway can listen to you
meachwill 40:6dea2ced2c87 21 static uint8_t config_frequency_sub_band = 1;
meachwill 40:6dea2ced2c87 22 mDot *dot;
meachwill 40:6dea2ced2c87 23 lora::ChannelPlan* plan = NULL;
meachwill 40:6dea2ced2c87 24 void Json_string(stringstream *s,char *lon,char *lat,char *clock);
meachwill 40:6dea2ced2c87 25 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
meachwill 40:6dea2ced2c87 26 void config(mDot *dot,lora::ChannelPlan *plan)
meachwill 40:6dea2ced2c87 27 {
meachwill 40:6dea2ced2c87 28 uint8_t ret;
meachwill 40:6dea2ced2c87 29 // reset to default config so we know what state we're in
meachwill 40:6dea2ced2c87 30 dot->resetConfig();
meachwill 40:6dea2ced2c87 31 // set how many log info will be show
meachwill 40:6dea2ced2c87 32 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
meachwill 40:6dea2ced2c87 33 // set subband frequency the same as gateway so gateway can listen to you
meachwill 40:6dea2ced2c87 34 logInfo("setting frequency sub band\r\n");
meachwill 40:6dea2ced2c87 35 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 36 logError("failed to set frequency sub band %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 37 }
meachwill 40:6dea2ced2c87 38 // lora has private network and public network here we use public network
meachwill 40:6dea2ced2c87 39 logInfo("setting public network mode");
meachwill 40:6dea2ced2c87 40 if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 41 logError("failed to public network mode");
meachwill 40:6dea2ced2c87 42 }
meachwill 40:6dea2ced2c87 43 std::vector<uint8_t> temp;
meachwill 40:6dea2ced2c87 44
meachwill 40:6dea2ced2c87 45 for (int i = 0; i < 8; i++) {
meachwill 40:6dea2ced2c87 46 temp.push_back(config_app_eui[i]);
meachwill 40:6dea2ced2c87 47 }
meachwill 40:6dea2ced2c87 48 // set network id
meachwill 40:6dea2ced2c87 49 logInfo("setting app eui\r\n");
meachwill 40:6dea2ced2c87 50 if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 51 logError("failed to set app eui %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 52 }
meachwill 40:6dea2ced2c87 53 temp.clear();
meachwill 40:6dea2ced2c87 54 for (int i = 0; i < 16; i++) {
meachwill 40:6dea2ced2c87 55 temp.push_back(config_app_key[i]);
meachwill 40:6dea2ced2c87 56 }
meachwill 40:6dea2ced2c87 57 // set network key
meachwill 40:6dea2ced2c87 58 logInfo("setting app key\r\n");
meachwill 40:6dea2ced2c87 59 if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 60 logError("failed to set app key %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 61 }
meachwill 40:6dea2ced2c87 62 // a higher spreading factor allows for longer range but lower throughput
meachwill 40:6dea2ced2c87 63 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
meachwill 40:6dea2ced2c87 64 logInfo("setting TX spreading factor\r\n");
meachwill 40:6dea2ced2c87 65 if ((ret = dot->setTxDataRate(mDot::DR4)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 66 logError("failed to set TX datarate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 67 }
meachwill 40:6dea2ced2c87 68 // request receive confirmation of packets from the gateway
meachwill 40:6dea2ced2c87 69 logInfo("enabling ACKs\r\n");
meachwill 40:6dea2ced2c87 70 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 71 logError("failed to enable ACKs %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 72 }
meachwill 40:6dea2ced2c87 73 // Set Tx Power
meachwill 40:6dea2ced2c87 74 logInfo("enabling Tx Power\r\n");
meachwill 40:6dea2ced2c87 75 if ((ret = dot->setTxPower(20)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 76 logError("failed to enable Tx Power %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 77 }
meachwill 40:6dea2ced2c87 78 // request receive confirmation of packets from the gateway
meachwill 40:6dea2ced2c87 79 logInfo("enabling Tx Data Rate\r\n");
meachwill 40:6dea2ced2c87 80 if ((ret = dot->setTxDataRate(3)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 81 logError("failed to enable Tx Data Rate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 82 }
meachwill 40:6dea2ced2c87 83 // save this configuration to the mDot's NVM
meachwill 40:6dea2ced2c87 84 logInfo("saving config\r\n");
meachwill 40:6dea2ced2c87 85 if (! dot->saveConfig()) {
meachwill 40:6dea2ced2c87 86 logError("failed to save configuration\r\n");
meachwill 40:6dea2ced2c87 87 }
meachwill 40:6dea2ced2c87 88 logInfo("joining network\r\n");
meachwill 40:6dea2ced2c87 89 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 90 logError("failed to join network %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 91 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
meachwill 40:6dea2ced2c87 92 }
meachwill 40:6dea2ced2c87 93 return;
meachwill 40:6dea2ced2c87 94 }
meachwill 40:6dea2ced2c87 95 int main()
meachwill 40:6dea2ced2c87 96 {
meachwill 40:6dea2ced2c87 97 plan= new lora::ChannelPlan_AS923();
meachwill 40:6dea2ced2c87 98
meachwill 40:6dea2ced2c87 99 dot = mDot::getInstance(plan);
meachwill 40:6dea2ced2c87 100
meachwill 40:6dea2ced2c87 101 // set network
meachwill 40:6dea2ced2c87 102 config(dot,plan);
meachwill 40:6dea2ced2c87 103
meachwill 40:6dea2ced2c87 104 while (true) {
meachwill 40:6dea2ced2c87 105 int ret;
meachwill 40:6dea2ced2c87 106 std::vector<uint8_t> data;
meachwill 40:6dea2ced2c87 107 stringstream ss;
meachwill 40:6dea2ced2c87 108 std::string data_str ;
meachwill 40:6dea2ced2c87 109 DS3231_get(&hrtl);
meachwill 40:6dea2ced2c87 110 struct tm clock_t;
meachwill 40:6dea2ced2c87 111 clock_t.tm_sec = hrtl.sec;
meachwill 40:6dea2ced2c87 112 clock_t.tm_min = hrtl.min;
meachwill 40:6dea2ced2c87 113 clock_t.tm_hour = hrtl.hour;
meachwill 40:6dea2ced2c87 114 clock_t.tm_mon = hrtl.mon-1;
meachwill 40:6dea2ced2c87 115 clock_t.tm_mday = hrtl.day;
meachwill 40:6dea2ced2c87 116 clock_t.tm_year = (hrtl.year)+100;
meachwill 40:6dea2ced2c87 117 // printf("%d/%d/%d %d:%d:%d\r\n",hrtl.year,hrtl.mon,hrtl.day,hrtl.hour,hrtl.min,hrtl.sec);
meachwill 40:6dea2ced2c87 118 strftime(clock_str,80,"%m/%d/%Y,%H:%M:%S",&clock_t);
meachwill 40:6dea2ced2c87 119 read_gps();
meachwill 40:6dea2ced2c87 120 Json_string(&ss,lonBuffer,latBuffer,clock_str);
meachwill 40:6dea2ced2c87 121 ss >> data_str;
meachwill 40:6dea2ced2c87 122 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
meachwill 40:6dea2ced2c87 123 data.push_back((uint8_t) *it);
meachwill 40:6dea2ced2c87 124
meachwill 40:6dea2ced2c87 125 // send the data to the gateway
meachwill 40:6dea2ced2c87 126 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
meachwill 40:6dea2ced2c87 127 logError("failed to send\r\n", ret, mDot::getReturnCodeString(ret).c_str());
meachwill 40:6dea2ced2c87 128 } else {
meachwill 40:6dea2ced2c87 129 logInfo("successfully sent data to gateway\r\n");
meachwill 40:6dea2ced2c87 130 }
meachwill 40:6dea2ced2c87 131 osDelay((uint32_t)30000);
meachwill 40:6dea2ced2c87 132 }
meachwill 40:6dea2ced2c87 133
meachwill 40:6dea2ced2c87 134 return 0;
meachwill 40:6dea2ced2c87 135
meachwill 40:6dea2ced2c87 136 }
meachwill 40:6dea2ced2c87 137
meachwill 40:6dea2ced2c87 138 void read_gps() {
meachwill 40:6dea2ced2c87 139 gps.getLatitude(latBuffer);
meachwill 40:6dea2ced2c87 140 gps.getLongitude(lonBuffer);
meachwill 40:6dea2ced2c87 141
meachwill 40:6dea2ced2c87 142 // Utilize latitude and longitude values here
meachwill 40:6dea2ced2c87 143 printf("\r\nLatitude: %s\r\nLongitude: %s\r\n", latBuffer, lonBuffer);
meachwill 40:6dea2ced2c87 144 }
meachwill 40:6dea2ced2c87 145
meachwill 40:6dea2ced2c87 146 void Json_string(stringstream *s,char *lon,char *lat,char *clock){
meachwill 40:6dea2ced2c87 147 *s << "{\"lat\":";
meachwill 40:6dea2ced2c87 148 *s << "\"";
meachwill 40:6dea2ced2c87 149 *s << lat;
meachwill 40:6dea2ced2c87 150 *s << "\"";
meachwill 40:6dea2ced2c87 151 *s << ",";
meachwill 40:6dea2ced2c87 152 *s << "\"lon\":";
meachwill 40:6dea2ced2c87 153 *s << "\"";
meachwill 40:6dea2ced2c87 154 *s << lon;
meachwill 40:6dea2ced2c87 155 *s << "\"";
meachwill 40:6dea2ced2c87 156 *s << ",";
meachwill 40:6dea2ced2c87 157 *s << "\"timetag\":";
meachwill 40:6dea2ced2c87 158 *s << "\"";
meachwill 40:6dea2ced2c87 159 *s << clock;
meachwill 40:6dea2ced2c87 160 *s << "\"";
meachwill 40:6dea2ced2c87 161 *s << "}";
meachwill 40:6dea2ced2c87 162 }
meachwill 40:6dea2ced2c87 163
meachwill 40:6dea2ced2c87 164 #endif
meachwill 40:6dea2ced2c87 165