5G Academy / Mbed 2 deprecated LoRaWAN

Dependencies:   mbed X_NUCLEO_IKS01A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "XNucleoIKS01A2.h"
00003 #include "string"
00004 
00005 #define CR 0x0D
00006 
00007 using namespace std;
00008 
00009 Serial pc(PA_2, PA_3, 115200); // seriale pc
00010 Serial lora(PB_6,PA_10,115200);
00011 
00012 static XNucleoIKS01A2 *board = XNucleoIKS01A2::instance(D14,D15,D4,D5); // scheda
00013 static LSM303AGRMagSensor *magSensor = board->magnetometer; // magnetoscopio
00014 static LPS22HBSensor *presSensor = board->pt_sensor;  // pressione
00015 static HTS221Sensor *tempSensor= board->ht_sensor; // temperatura e umidita'
00016 static LSM6DSLSensor *iNemo = board->acc_gyro; // giroscopio
00017 
00018 enum DataType {PRESSURE = 0, MAG_FIELD, TEMPERATURE, HUMIDITY, ACCELERATION, ANG_VELOCITY, N_DATA_TYPES};
00019 uint16_t ports[N_DATA_TYPES] = {15, 16, 17, 18, 19, 20};
00020 
00021 
00022 void connect_to_lora();
00023 void wait4join();
00024 void modem_at_cmd(const string&);
00025 void at_send(uint16_t, const string&, bool);
00026 
00027 string hex_encode(const string&);
00028 
00029 void enable_all_sensors();
00030 void get_pressure();
00031 void get_magnetic_field();
00032 void get_temperature();
00033 void get_humidity();
00034 void get_acceleration();
00035 void get_gyroscope();
00036 
00037 
00038 float pressure = 0;
00039 float temperature = 0;
00040 float humidity = 0;
00041 int assi_giroscopio[3];
00042 int assi_accelerazione[3];
00043 int assi_magnetometro[3];
00044 
00045 int main() {
00046 
00047     enable_all_sensors();
00048     connect_to_lora();
00049     
00050     size_t counter = 0;
00051     int data_type = PRESSURE;
00052     
00053     char payload[256];
00054     while (1) {
00055         
00056         memset(payload, 0, 256);
00057         
00058         switch (data_type) {
00059             case PRESSURE:
00060                 get_pressure();
00061                 sprintf(payload, "{\"pressure\":%.6f}", pressure);
00062                 break;
00063             case MAG_FIELD:
00064                 get_magnetic_field();
00065                 sprintf(payload, "{\"magx\":%d, \"magy\":%d, \"magz\":%d}", assi_magnetometro[0] , assi_magnetometro[1] , assi_magnetometro[2]);
00066                 break;
00067             case TEMPERATURE:
00068                 get_temperature();
00069                 sprintf(payload, "{\"temperature\":%.6f}", temperature);
00070                 break;
00071             case HUMIDITY:
00072                 get_humidity();
00073                 sprintf(payload, "{\"humidity\":%.6f}", humidity);
00074                 break;
00075             case ACCELERATION:
00076                 get_acceleration();
00077                 sprintf(payload, "{\"accx\":%d, \"accy\":%d, \"accz\":%d}", assi_accelerazione[0] , assi_accelerazione[1] , assi_accelerazione[2]);
00078                 break;
00079             case ANG_VELOCITY:
00080                 get_gyroscope();
00081                 sprintf(payload, "{\"angvelx\":%d, \"angvely\":%d, \"angvelz\":%d}", assi_giroscopio[0] , assi_giroscopio[1] , assi_giroscopio[2]);
00082                 break;    
00083         }
00084         
00085         at_send(ports[data_type], payload, false);
00086         
00087         data_type = (data_type + 1) % N_DATA_TYPES;
00088         ++counter;
00089         
00090         wait(5.0);
00091     }
00092 }
00093 
00094 
00095 string hex_encode(const string& input) {
00096     static const char hex_digits[] = "0123456789ABCDEF";
00097     string output;
00098     output.reserve(input.length() * 2);
00099     for (int i = 0; i < input.length(); i++)
00100     {
00101         output += hex_digits[input[i] >> 4];
00102         output += hex_digits[input[i] & 15];
00103     }
00104     return output;
00105 }
00106 
00107 void at_send(uint16_t port, const string& payload, bool ack = false) {
00108     //string port_msg;
00109     char port_pref[12] = {"AT+SEND="};
00110     //port_msg.reserve(5); // max port 2^16-1 = 65545
00111     sprintf(port_pref + 8, "%d", port);
00112     //pc.printf(port_pref);
00113     string hex_payload = hex_encode(payload);
00114     //string test = (string)port_pref + "," + hex_payload + "," + (ack ? '1' : '0');
00115     //pc.printf(&test[0]);
00116     modem_at_cmd((string)port_pref + "," + hex_payload + "," + (ack ? '1' : '0'));
00117 }
00118 
00119 void modem_at_cmd(const string& buffer) {
00120     for(uint16_t i = 0; i < buffer.length(); i++) {
00121         lora.putc(buffer[i]);
00122         pc.putc(buffer[i]);
00123     }
00124     lora.putc(CR);
00125     
00126     pc.putc(CR);
00127     pc.printf("\n");
00128     
00129     char c = 0;
00130     do {
00131         if (lora.readable()) {
00132             c = lora.getc();
00133             pc.putc(c);
00134         }
00135     } while(c != ' ');
00136 }
00137 
00138 void wait4join(){
00139     char c = 0;
00140     do {
00141         if (lora.readable()) {
00142             c = lora.getc();
00143             pc.putc(c);
00144         }
00145     } while(c != 'd');
00146 }
00147 
00148 void connect_to_lora() {
00149     modem_at_cmd("AT");
00150     pc.printf("Establishing connection to LoRa\r\n");
00151     wait(1);
00152     
00153     modem_at_cmd("AT+APPEUI=0000000000000001");
00154     pc.printf("Inviato EUI\r\n");
00155     wait(1);
00156     
00157     modem_at_cmd("AT+AK=00000000000000000000000000000001");
00158     pc.printf("Inviato AK\r\n");
00159     wait(1);
00160     
00161     modem_at_cmd("AT+JOIN=1");
00162     pc.printf("Inviato JOIN\r\n");
00163     wait4join();
00164     
00165     modem_at_cmd("AT+DC=0");
00166     pc.printf("Disabled DC\r\n");
00167     wait(1);
00168     
00169     modem_at_cmd("AT+ADR=1");
00170     pc.printf("Enabled ADR\r\n");
00171     wait(1);
00172 }
00173 
00174 void enable_all_sensors() {
00175     presSensor->enable();
00176     magSensor->enable();
00177     tempSensor->enable();
00178     iNemo->enable_g();
00179     iNemo->enable_x();
00180 }
00181 
00182 void get_pressure() {
00183     presSensor->get_pressure(&pressure);
00184     pc.printf("pressure [mbar]:\t%f\r\n", pressure);
00185 }
00186 
00187 void get_magnetic_field() {
00188     magSensor->get_m_axes(assi_magnetometro);
00189     pc.printf("mag field [mgauss]:\t%d\t%d\t%d\n\r", assi_magnetometro[0] , assi_magnetometro[1] , assi_magnetometro[2]);
00190 }
00191 
00192 void get_temperature() {
00193     tempSensor->get_temperature(&temperature);
00194     pc.printf("temperature ['C]:\t%f\n\r", temperature);
00195 }
00196 
00197 void get_humidity() {
00198     tempSensor->get_humidity(&humidity);
00199     pc.printf("humidity [%%]\t\t%f\r\n", humidity);
00200 }
00201 
00202 void get_acceleration() {
00203     iNemo->get_x_axes(assi_accelerazione);
00204     pc.printf("acceleration [mg]:\t%d\t%d\t%d\n\r", assi_accelerazione[0] , assi_accelerazione[1] , assi_accelerazione[2]);
00205 }
00206 
00207 void get_gyroscope() {
00208     iNemo->get_g_axes(assi_giroscopio);
00209     pc.printf("ang velocity [rad/s]:\t%d\t%d\t%d\n\r", assi_giroscopio[0] , assi_giroscopio[1] , assi_giroscopio[2]);
00210 }