Project Digora IOT

Dependencies:   libmDot-dev-mbed5

Committer:
Niiippoooo
Date:
Fri May 19 09:26:49 2017 +0000
Revision:
0:522ad8e780f6
project Digora IOT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Niiippoooo 0:522ad8e780f6 1 //Envirennement mbed
Niiippoooo 0:522ad8e780f6 2 #include "mbed.h"
Niiippoooo 0:522ad8e780f6 3
Niiippoooo 0:522ad8e780f6 4 //Capteurs
Niiippoooo 0:522ad8e780f6 5 #include "Lum_sensor.h" //inclusion de la classe capteur de luminosité
Niiippoooo 0:522ad8e780f6 6 #include "Sound_sensor.h" //inclusion de la classe capteur de son
Niiippoooo 0:522ad8e780f6 7 #include "TempHum_sensor.h" //inclusion de la classe capteur de température et humidité
Niiippoooo 0:522ad8e780f6 8
Niiippoooo 0:522ad8e780f6 9
Niiippoooo 0:522ad8e780f6 10 //Librairie mDot
Niiippoooo 0:522ad8e780f6 11 #include "mDot.h"
Niiippoooo 0:522ad8e780f6 12 #include "MTSText.h"
Niiippoooo 0:522ad8e780f6 13 #include "MTSLog.h"
Niiippoooo 0:522ad8e780f6 14 #include <iostream>
Niiippoooo 0:522ad8e780f6 15 #include <fstream>
Niiippoooo 0:522ad8e780f6 16 #include <string>
Niiippoooo 0:522ad8e780f6 17 #include <cmath>
Niiippoooo 0:522ad8e780f6 18 #include <vector>
Niiippoooo 0:522ad8e780f6 19 #include <sstream>
Niiippoooo 0:522ad8e780f6 20 #include <cstdio>
Niiippoooo 0:522ad8e780f6 21
Niiippoooo 0:522ad8e780f6 22
Niiippoooo 0:522ad8e780f6 23 namespace patch
Niiippoooo 0:522ad8e780f6 24 {
Niiippoooo 0:522ad8e780f6 25 template < typename T > std::string to_string( const T& n )
Niiippoooo 0:522ad8e780f6 26 {
Niiippoooo 0:522ad8e780f6 27 std::ostringstream stm ;
Niiippoooo 0:522ad8e780f6 28 stm << n ;
Niiippoooo 0:522ad8e780f6 29 return stm.str() ;
Niiippoooo 0:522ad8e780f6 30 }
Niiippoooo 0:522ad8e780f6 31 }
Niiippoooo 0:522ad8e780f6 32
Niiippoooo 0:522ad8e780f6 33
Niiippoooo 0:522ad8e780f6 34
Niiippoooo 0:522ad8e780f6 35 void log_error(mDot* dot, const char* msg, int32_t retval);
Niiippoooo 0:522ad8e780f6 36
Niiippoooo 0:522ad8e780f6 37
Niiippoooo 0:522ad8e780f6 38
Niiippoooo 0:522ad8e780f6 39 // Configuration du Reseau de la Gateway
Niiippoooo 0:522ad8e780f6 40 // On peut aussi directement donner la clé, et l'ID du réseau
Niiippoooo 0:522ad8e780f6 41 static std::string config_network_name = "digora2017";
Niiippoooo 0:522ad8e780f6 42 static std::string config_network_pass = "digora2017";
Niiippoooo 0:522ad8e780f6 43
Niiippoooo 0:522ad8e780f6 44
Niiippoooo 0:522ad8e780f6 45 int main()
Niiippoooo 0:522ad8e780f6 46 {
Niiippoooo 0:522ad8e780f6 47
Niiippoooo 0:522ad8e780f6 48 float son = 0;
Niiippoooo 0:522ad8e780f6 49 float Temperature = 0;
Niiippoooo 0:522ad8e780f6 50 float Hum = 0;
Niiippoooo 0:522ad8e780f6 51 float Luminosity = 0;
Niiippoooo 0:522ad8e780f6 52
Niiippoooo 0:522ad8e780f6 53 Capt_Son SON; //initialisation capteur luminosité
Niiippoooo 0:522ad8e780f6 54 Capt_Lum LUM; //initialisation capteur son
Niiippoooo 0:522ad8e780f6 55 Capt_TH TH; //initialisation capteur température et humidité
Niiippoooo 0:522ad8e780f6 56 printf("\n\n\r#################################\n\rInitialisation capteurs OK\n\r#################################\n\r");
Niiippoooo 0:522ad8e780f6 57
Niiippoooo 0:522ad8e780f6 58
Niiippoooo 0:522ad8e780f6 59
Niiippoooo 0:522ad8e780f6 60
Niiippoooo 0:522ad8e780f6 61 //---------- CONNEXION A LA GATEWAY ----------
Niiippoooo 0:522ad8e780f6 62
Niiippoooo 0:522ad8e780f6 63
Niiippoooo 0:522ad8e780f6 64 int32_t ret;
Niiippoooo 0:522ad8e780f6 65 mDot* dot = NULL;
Niiippoooo 0:522ad8e780f6 66 std::vector<uint8_t> data;
Niiippoooo 0:522ad8e780f6 67 std::vector<uint8_t> data_recv;
Niiippoooo 0:522ad8e780f6 68 std::string data_str;
Niiippoooo 0:522ad8e780f6 69
Niiippoooo 0:522ad8e780f6 70 /** Get a handle to the singleton object
Niiippoooo 0:522ad8e780f6 71 * @returns pointer to mDot object
Niiippoooo 0:522ad8e780f6 72 */
Niiippoooo 0:522ad8e780f6 73
Niiippoooo 0:522ad8e780f6 74 dot = mDot::getInstance();
Niiippoooo 0:522ad8e780f6 75
Niiippoooo 0:522ad8e780f6 76 /** Reset config to factory default
Niiippoooo 0:522ad8e780f6 77 */
Niiippoooo 0:522ad8e780f6 78 dot->resetConfig();
Niiippoooo 0:522ad8e780f6 79
Niiippoooo 0:522ad8e780f6 80 /** Resets current network session, essentially disconnecting from the network
Niiippoooo 0:522ad8e780f6 81 * has no effect for MANUAL network join mode
Niiippoooo 0:522ad8e780f6 82 */
Niiippoooo 0:522ad8e780f6 83 dot->resetNetworkSession();
Niiippoooo 0:522ad8e780f6 84
Niiippoooo 0:522ad8e780f6 85 /** Get library version information
Niiippoooo 0:522ad8e780f6 86 * @returns string containing library version information
Niiippoooo 0:522ad8e780f6 87 */
Niiippoooo 0:522ad8e780f6 88 printf("version: %s\r\n", dot->getId().c_str());
Niiippoooo 0:522ad8e780f6 89
Niiippoooo 0:522ad8e780f6 90
Niiippoooo 0:522ad8e780f6 91 /** Set network join mode
Niiippoooo 0:522ad8e780f6 92 * MANUAL: set network address and session keys manually
Niiippoooo 0:522ad8e780f6 93 * OTA: User sets network name and passphrase, then attempts to join
Niiippoooo 0:522ad8e780f6 94 * AUTO_OTA: same as OTA, but network sessions can be saved and restored
Niiippoooo 0:522ad8e780f6 95 * @param mode MANUAL, OTA, or AUTO_OTA
Niiippoooo 0:522ad8e780f6 96 * @returns MDOT_OK if success
Niiippoooo 0:522ad8e780f6 97 */
Niiippoooo 0:522ad8e780f6 98 if (dot->getJoinMode() != mDot::OTA)
Niiippoooo 0:522ad8e780f6 99 {
Niiippoooo 0:522ad8e780f6 100 logInfo("changing network join mode to OTA");
Niiippoooo 0:522ad8e780f6 101 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 102 {
Niiippoooo 0:522ad8e780f6 103 logError("failed to set network join mode to OTA");
Niiippoooo 0:522ad8e780f6 104 }
Niiippoooo 0:522ad8e780f6 105 }
Niiippoooo 0:522ad8e780f6 106
Niiippoooo 0:522ad8e780f6 107
Niiippoooo 0:522ad8e780f6 108 /** Set network name
Niiippoooo 0:522ad8e780f6 109 * for use with OTA & AUTO_OTA network join modes
Niiippoooo 0:522ad8e780f6 110 * generates network ID (crc64 of name) automatically
Niiippoooo 0:522ad8e780f6 111 * @param name a string of of at least 8 bytes and no more than 128 bytes
Niiippoooo 0:522ad8e780f6 112 * @return MDOT_OK if success
Niiippoooo 0:522ad8e780f6 113 */
Niiippoooo 0:522ad8e780f6 114 printf("setting network name...\r\n");
Niiippoooo 0:522ad8e780f6 115 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 116 {
Niiippoooo 0:522ad8e780f6 117 log_error(dot, "failed to set network name", ret);
Niiippoooo 0:522ad8e780f6 118 }
Niiippoooo 0:522ad8e780f6 119
Niiippoooo 0:522ad8e780f6 120 /** Set network passphrase
Niiippoooo 0:522ad8e780f6 121 * for use with OTA & AUTO_OTA network join modes
Niiippoooo 0:522ad8e780f6 122 * generates network key (cmac of passphrase) automatically
Niiippoooo 0:522ad8e780f6 123 * @param name a string of of at least 8 bytes and no more than 128 bytes
Niiippoooo 0:522ad8e780f6 124 * @return MDOT_OK if success
Niiippoooo 0:522ad8e780f6 125 */
Niiippoooo 0:522ad8e780f6 126 printf("setting network password...\r\n");
Niiippoooo 0:522ad8e780f6 127 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 128 {
Niiippoooo 0:522ad8e780f6 129 log_error(dot, "failed to set network password", ret);
Niiippoooo 0:522ad8e780f6 130 }
Niiippoooo 0:522ad8e780f6 131
Niiippoooo 0:522ad8e780f6 132 /** Set the device class A, B or C
Niiippoooo 0:522ad8e780f6 133 * @returns MDOT_OK if success
Niiippoooo 0:522ad8e780f6 134 */
Niiippoooo 0:522ad8e780f6 135 logInfo("changing network mode to class C");
Niiippoooo 0:522ad8e780f6 136 if (dot->setClass("C") != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 137 {
Niiippoooo 0:522ad8e780f6 138 logError("failed to set network mode to class C");
Niiippoooo 0:522ad8e780f6 139 }
Niiippoooo 0:522ad8e780f6 140
Niiippoooo 0:522ad8e780f6 141
Niiippoooo 0:522ad8e780f6 142
Niiippoooo 0:522ad8e780f6 143 /** Save config data to non volatile memory
Niiippoooo 0:522ad8e780f6 144 * @returns true if success, false if failure
Niiippoooo 0:522ad8e780f6 145 */
Niiippoooo 0:522ad8e780f6 146 logInfo("saving configuration");
Niiippoooo 0:522ad8e780f6 147 if (!dot->saveConfig())
Niiippoooo 0:522ad8e780f6 148 {
Niiippoooo 0:522ad8e780f6 149 logError("failed to save configuration");
Niiippoooo 0:522ad8e780f6 150 }
Niiippoooo 0:522ad8e780f6 151
Niiippoooo 0:522ad8e780f6 152 /** Attempt to join network
Niiippoooo 0:522ad8e780f6 153 * each attempt will be made with a random datarate up to the configured datarate
Niiippoooo 0:522ad8e780f6 154 * JoinRequest backoff between tries is enforced to 1% for 1st hour, 0.1% for 1-10 hours and 0.01% after 10 hours
Niiippoooo 0:522ad8e780f6 155 * Check getNextTxMs() for time until next join attempt can be made
Niiippoooo 0:522ad8e780f6 156 * @returns MDOT_OK if success
Niiippoooo 0:522ad8e780f6 157 */
Niiippoooo 0:522ad8e780f6 158
Niiippoooo 0:522ad8e780f6 159 uint8_t freq = dot->getFrequencyBand();
Niiippoooo 0:522ad8e780f6 160 printf("freq : %u\n\r", freq); //Set frequency sub band
Niiippoooo 0:522ad8e780f6 161 printf("joining network\r\n");
Niiippoooo 0:522ad8e780f6 162
Niiippoooo 0:522ad8e780f6 163
Niiippoooo 0:522ad8e780f6 164 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 165 {
Niiippoooo 0:522ad8e780f6 166 log_error(dot, "failed to join network", ret);
Niiippoooo 0:522ad8e780f6 167 wait(2);
Niiippoooo 0:522ad8e780f6 168 }
Niiippoooo 0:522ad8e780f6 169 string type;
Niiippoooo 0:522ad8e780f6 170
Niiippoooo 0:522ad8e780f6 171 //############ RELEVE DE MESURES ############
Niiippoooo 0:522ad8e780f6 172
Niiippoooo 0:522ad8e780f6 173 while(true)
Niiippoooo 0:522ad8e780f6 174 {
Niiippoooo 0:522ad8e780f6 175 printf("\n\n");
Niiippoooo 0:522ad8e780f6 176 TH.getTempHum(); //mesure de la température et de l'humidité
Niiippoooo 0:522ad8e780f6 177 TH.obtenirTemperature(); //affichage de la température
Niiippoooo 0:522ad8e780f6 178 TH.obtenirHumidite(); //affichage de l'humidité
Niiippoooo 0:522ad8e780f6 179 LUM.obtenirLuminosite(); //mesure et affichage de la luminosité
Niiippoooo 0:522ad8e780f6 180 SON.obtenirSon(); //mesure et affichage du niveau sonore
Niiippoooo 0:522ad8e780f6 181
Niiippoooo 0:522ad8e780f6 182 //##################################################
Niiippoooo 0:522ad8e780f6 183
Niiippoooo 0:522ad8e780f6 184
Niiippoooo 0:522ad8e780f6 185 //-------- ENVOI SON --------
Niiippoooo 0:522ad8e780f6 186
Niiippoooo 0:522ad8e780f6 187 data_str = patch::to_string(son);
Niiippoooo 0:522ad8e780f6 188 /**
Niiippoooo 0:522ad8e780f6 189 * Fill the vector that we will send with the data
Niiippoooo 0:522ad8e780f6 190 */
Niiippoooo 0:522ad8e780f6 191 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
Niiippoooo 0:522ad8e780f6 192 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 193 type = ":dB";
Niiippoooo 0:522ad8e780f6 194 for (std::string::iterator it = type.begin(); it != type.end(); it++)
Niiippoooo 0:522ad8e780f6 195 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 196
Niiippoooo 0:522ad8e780f6 197 /** Send data to the gateway
Niiippoooo 0:522ad8e780f6 198 * validates data size (based on spreading factor)
Niiippoooo 0:522ad8e780f6 199 * @param data a vector of up to 242 bytes (may be less based on spreading factor)
Niiippoooo 0:522ad8e780f6 200 * @returns MDOT_OK if packet was sent successfully (ACKs disabled), or if an ACK was received (ACKs enabled)
Niiippoooo 0:522ad8e780f6 201 */
Niiippoooo 0:522ad8e780f6 202 if ((ret = dot->send(data)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 203 {
Niiippoooo 0:522ad8e780f6 204 log_error(dot, "failed to send", ret);
Niiippoooo 0:522ad8e780f6 205 }
Niiippoooo 0:522ad8e780f6 206
Niiippoooo 0:522ad8e780f6 207 else
Niiippoooo 0:522ad8e780f6 208 {
Niiippoooo 0:522ad8e780f6 209 printf("successfully sent data to gateway\r\n");
Niiippoooo 0:522ad8e780f6 210 }
Niiippoooo 0:522ad8e780f6 211
Niiippoooo 0:522ad8e780f6 212 data_str.clear();
Niiippoooo 0:522ad8e780f6 213 data.clear();
Niiippoooo 0:522ad8e780f6 214
Niiippoooo 0:522ad8e780f6 215 wait(10);
Niiippoooo 0:522ad8e780f6 216
Niiippoooo 0:522ad8e780f6 217 //-------- ENVOI TEMPERATURE --------
Niiippoooo 0:522ad8e780f6 218
Niiippoooo 0:522ad8e780f6 219 data_str = patch::to_string(Temperature);
Niiippoooo 0:522ad8e780f6 220 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
Niiippoooo 0:522ad8e780f6 221 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 222 type = ":*C";
Niiippoooo 0:522ad8e780f6 223 for (std::string::iterator it = type.begin(); it != type.end(); it++)
Niiippoooo 0:522ad8e780f6 224 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 225
Niiippoooo 0:522ad8e780f6 226 if ((ret = dot->send(data)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 227 {
Niiippoooo 0:522ad8e780f6 228 log_error(dot, "failed to send", ret);
Niiippoooo 0:522ad8e780f6 229 }
Niiippoooo 0:522ad8e780f6 230
Niiippoooo 0:522ad8e780f6 231 else
Niiippoooo 0:522ad8e780f6 232 {
Niiippoooo 0:522ad8e780f6 233 printf("successfully sent data to gateway\r\n");
Niiippoooo 0:522ad8e780f6 234 }
Niiippoooo 0:522ad8e780f6 235
Niiippoooo 0:522ad8e780f6 236 data_str.clear();
Niiippoooo 0:522ad8e780f6 237 data.clear();
Niiippoooo 0:522ad8e780f6 238
Niiippoooo 0:522ad8e780f6 239 wait(10);
Niiippoooo 0:522ad8e780f6 240
Niiippoooo 0:522ad8e780f6 241 //-------- ENVOI HUMIDITE --------
Niiippoooo 0:522ad8e780f6 242
Niiippoooo 0:522ad8e780f6 243 data_str = patch::to_string(Hum);
Niiippoooo 0:522ad8e780f6 244 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
Niiippoooo 0:522ad8e780f6 245 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 246 type = ":%";
Niiippoooo 0:522ad8e780f6 247 for (std::string::iterator it = type.begin(); it != type.end(); it++)
Niiippoooo 0:522ad8e780f6 248 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 249
Niiippoooo 0:522ad8e780f6 250 if ((ret = dot->send(data)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 251 {
Niiippoooo 0:522ad8e780f6 252 log_error(dot, "failed to send", ret);
Niiippoooo 0:522ad8e780f6 253 }
Niiippoooo 0:522ad8e780f6 254
Niiippoooo 0:522ad8e780f6 255 else
Niiippoooo 0:522ad8e780f6 256 {
Niiippoooo 0:522ad8e780f6 257 printf("successfully sent data to gateway\r\n");
Niiippoooo 0:522ad8e780f6 258 }
Niiippoooo 0:522ad8e780f6 259
Niiippoooo 0:522ad8e780f6 260 data_str.clear();
Niiippoooo 0:522ad8e780f6 261 data.clear();
Niiippoooo 0:522ad8e780f6 262
Niiippoooo 0:522ad8e780f6 263 wait(10);
Niiippoooo 0:522ad8e780f6 264
Niiippoooo 0:522ad8e780f6 265 //-------- ENVOI LUMINOSITE --------
Niiippoooo 0:522ad8e780f6 266
Niiippoooo 0:522ad8e780f6 267 data_str = patch::to_string(Luminosity);
Niiippoooo 0:522ad8e780f6 268 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
Niiippoooo 0:522ad8e780f6 269 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 270 type = ":Lux";
Niiippoooo 0:522ad8e780f6 271 for (std::string::iterator it = type.begin(); it != type.end(); it++)
Niiippoooo 0:522ad8e780f6 272 data.push_back((uint8_t) *it);
Niiippoooo 0:522ad8e780f6 273
Niiippoooo 0:522ad8e780f6 274 if ((ret = dot->send(data)) != mDot::MDOT_OK)
Niiippoooo 0:522ad8e780f6 275 {
Niiippoooo 0:522ad8e780f6 276 log_error(dot, "failed to send", ret);
Niiippoooo 0:522ad8e780f6 277 }
Niiippoooo 0:522ad8e780f6 278
Niiippoooo 0:522ad8e780f6 279 else
Niiippoooo 0:522ad8e780f6 280 {
Niiippoooo 0:522ad8e780f6 281 printf("successfully sent data to gateway\r\n");
Niiippoooo 0:522ad8e780f6 282 }
Niiippoooo 0:522ad8e780f6 283
Niiippoooo 0:522ad8e780f6 284 data_str.clear();
Niiippoooo 0:522ad8e780f6 285 data.clear();
Niiippoooo 0:522ad8e780f6 286
Niiippoooo 0:522ad8e780f6 287 wait(10);
Niiippoooo 0:522ad8e780f6 288 }
Niiippoooo 0:522ad8e780f6 289
Niiippoooo 0:522ad8e780f6 290
Niiippoooo 0:522ad8e780f6 291
Niiippoooo 0:522ad8e780f6 292 //return 0; Jamais atteint à cause du "while(true)", donc inutile...
Niiippoooo 0:522ad8e780f6 293 }
Niiippoooo 0:522ad8e780f6 294
Niiippoooo 0:522ad8e780f6 295
Niiippoooo 0:522ad8e780f6 296 void log_error(mDot* dot, const char* msg, int32_t retval) {
Niiippoooo 0:522ad8e780f6 297 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
Niiippoooo 0:522ad8e780f6 298 }