5G Academy / Mbed 2 deprecated Pressure_Sensor_Gruppo_Ultimo

Dependencies:   mbed X_NUCLEO_IKS01A2

Committer:
ambromar
Date:
Wed Apr 22 15:06:28 2020 +0000
Revision:
0:3494d33e3b04
Child:
1:7b9e61c70708
asdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ambromar 0:3494d33e3b04 1 #include "mbed.h"
ambromar 0:3494d33e3b04 2 #include "XNucleoIKS01A2.h"
ambromar 0:3494d33e3b04 3 #include "string.h"
ambromar 0:3494d33e3b04 4 #include <sstream>
ambromar 0:3494d33e3b04 5 #include <iostream>
ambromar 0:3494d33e3b04 6 using namespace std;
ambromar 0:3494d33e3b04 7
ambromar 0:3494d33e3b04 8 //Sensore per la misura della pressione atmosferica + Comunicazione modem LoRa
ambromar 0:3494d33e3b04 9 // Ambrosino-Rivellini-Salernitano
ambromar 0:3494d33e3b04 10
ambromar 0:3494d33e3b04 11 // Prototipi funzioni
ambromar 0:3494d33e3b04 12 void modem_at_cmd(char*,int);
ambromar 0:3494d33e3b04 13 void wait4join(void);
ambromar 0:3494d33e3b04 14 std::string ieee_float_to_hex( float f );
ambromar 0:3494d33e3b04 15
ambromar 0:3494d33e3b04 16 // Variabili globali
ambromar 0:3494d33e3b04 17 Serial pc(D1,D0,115200);
ambromar 0:3494d33e3b04 18 Serial lora(PB_6,PA_10,115200);
ambromar 0:3494d33e3b04 19
ambromar 0:3494d33e3b04 20 static XNucleoIKS01A2 *myBoard = XNucleoIKS01A2::instance(D14,D15,D4,D5);
ambromar 0:3494d33e3b04 21 static LPS22HBSensor *pressure = myBoard->pt_sensor;
ambromar 0:3494d33e3b04 22 uint8_t id;
ambromar 0:3494d33e3b04 23 float val_pressure;
ambromar 0:3494d33e3b04 24
ambromar 0:3494d33e3b04 25 char c;
ambromar 0:3494d33e3b04 26
ambromar 0:3494d33e3b04 27 char* msg1 = {"AT"};
ambromar 0:3494d33e3b04 28 char* msg2 = {"AT+APPEUI=0000000000000001"};
ambromar 0:3494d33e3b04 29 char* msg3 = {"AT+AK=00000000000000000000000000000001"};
ambromar 0:3494d33e3b04 30 char* msg4 = {"AT+JOIN=1"};
ambromar 0:3494d33e3b04 31 char* msg5 = {"AT+SEND=15,3031323334,0"};
ambromar 0:3494d33e3b04 32 char* msg6 = {"AT+DC=0"}; //modifica per disabilitare duty cycle
ambromar 0:3494d33e3b04 33 char* msg7 = {"AT+ADR=1"}; //modifica per abilitare auto data rate
ambromar 0:3494d33e3b04 34 char* msg_template_pressure = {"AT+SEND=5072657373696f6e65203a20"}; // template stringa in HEX "Pressione : "
ambromar 0:3494d33e3b04 35
ambromar 0:3494d33e3b04 36 int main() {
ambromar 0:3494d33e3b04 37 char* msg_pressure ;
ambromar 0:3494d33e3b04 38 std::string hex_float ;
ambromar 0:3494d33e3b04 39
ambromar 0:3494d33e3b04 40 // Pressure Setup
ambromar 0:3494d33e3b04 41 pressure->enable();
ambromar 0:3494d33e3b04 42 pc.printf("Sensore per la misurazione di pressione abilitato\r\n");
ambromar 0:3494d33e3b04 43 pressure->read_id(&id);
ambromar 0:3494d33e3b04 44 pc.printf("ID pari a 0x%X\r\n",id);
ambromar 0:3494d33e3b04 45 pc.printf("Test seriale no pc\r\n");
ambromar 0:3494d33e3b04 46
ambromar 0:3494d33e3b04 47 // Modem Setup
ambromar 0:3494d33e3b04 48 modem_at_cmd(msg1,(int)strlen(msg1));
ambromar 0:3494d33e3b04 49 pc.printf("Inviato AT\r\n");
ambromar 0:3494d33e3b04 50 wait(1);
ambromar 0:3494d33e3b04 51 modem_at_cmd(msg2,(int)strlen(msg2));
ambromar 0:3494d33e3b04 52 pc.printf("Inviato EUI\r\n");
ambromar 0:3494d33e3b04 53 wait(1);
ambromar 0:3494d33e3b04 54 modem_at_cmd(msg3,(int)strlen(msg3));
ambromar 0:3494d33e3b04 55 pc.printf("Inviato AK\r\n");
ambromar 0:3494d33e3b04 56 wait(1);
ambromar 0:3494d33e3b04 57 modem_at_cmd(msg4,(int)strlen(msg4));
ambromar 0:3494d33e3b04 58 pc.printf("Inviato JOIN\r\n");
ambromar 0:3494d33e3b04 59 wait4join();
ambromar 0:3494d33e3b04 60 modem_at_cmd(msg5,(int)strlen(msg5));
ambromar 0:3494d33e3b04 61 pc.printf("Inviato send\r\n");
ambromar 0:3494d33e3b04 62 modem_at_cmd(msg6,(int)strlen(msg6));
ambromar 0:3494d33e3b04 63 pc.printf("Inviata richiesta disabilitazione duty cycl--e\r\n");
ambromar 0:3494d33e3b04 64 modem_at_cmd(msg7,(int)strlen(msg7));
ambromar 0:3494d33e3b04 65 pc.printf("Inviata richiesta data rate adattivo\r\n");
ambromar 0:3494d33e3b04 66
ambromar 0:3494d33e3b04 67 while(1) {
ambromar 0:3494d33e3b04 68 pressure->get_pressure(&val_pressure);
ambromar 0:3494d33e3b04 69 pc.printf("Valori pressione misurati [mbar]:\t%f\r\n",val_pressure);
ambromar 0:3494d33e3b04 70 // converte il float val_pressure in un char*
ambromar 0:3494d33e3b04 71 hex_float = ieee_float_to_hex(val_pressure);
ambromar 0:3494d33e3b04 72 char *cstr = new char[hex_float.length() + 1];
ambromar 0:3494d33e3b04 73 std::strcpy(cstr, hex_float.c_str());
ambromar 0:3494d33e3b04 74 msg_pressure = strcat(msg_template_pressure,cstr);
ambromar 0:3494d33e3b04 75 modem_at_cmd(msg_pressure,(int)strlen(msg_pressure));
ambromar 0:3494d33e3b04 76 pc.printf("Inviato Dato Pressione\r\n");
ambromar 0:3494d33e3b04 77 wait(1.0);
ambromar 0:3494d33e3b04 78 }
ambromar 0:3494d33e3b04 79 }
ambromar 0:3494d33e3b04 80
ambromar 0:3494d33e3b04 81 void modem_at_cmd(char* buffer, int n){
ambromar 0:3494d33e3b04 82 for(uint8_t i=0; i<n; i++) {
ambromar 0:3494d33e3b04 83 lora.putc(buffer[i]);
ambromar 0:3494d33e3b04 84 pc.putc(buffer[i]);
ambromar 0:3494d33e3b04 85 }
ambromar 0:3494d33e3b04 86 lora.putc(13);
ambromar 0:3494d33e3b04 87 pc.putc(13);
ambromar 0:3494d33e3b04 88 pc.printf("\n");
ambromar 0:3494d33e3b04 89 c=0;
ambromar 0:3494d33e3b04 90 do {
ambromar 0:3494d33e3b04 91 if (lora.readable()) {
ambromar 0:3494d33e3b04 92 c = lora.getc();
ambromar 0:3494d33e3b04 93 pc.putc(c);
ambromar 0:3494d33e3b04 94 }
ambromar 0:3494d33e3b04 95 } while(c!=' ');
ambromar 0:3494d33e3b04 96 }
ambromar 0:3494d33e3b04 97
ambromar 0:3494d33e3b04 98 void wait4join(){
ambromar 0:3494d33e3b04 99 c=0;
ambromar 0:3494d33e3b04 100 do {
ambromar 0:3494d33e3b04 101 if (lora.readable()) {
ambromar 0:3494d33e3b04 102 c = lora.getc();
ambromar 0:3494d33e3b04 103 pc.putc(c);
ambromar 0:3494d33e3b04 104 }
ambromar 0:3494d33e3b04 105 }while(c!='d');
ambromar 0:3494d33e3b04 106 }
ambromar 0:3494d33e3b04 107
ambromar 0:3494d33e3b04 108 std::string ieee_float_to_hex( float f )
ambromar 0:3494d33e3b04 109 {
ambromar 0:3494d33e3b04 110
ambromar 0:3494d33e3b04 111 union { float fval ; std::uint32_t ival ; };
ambromar 0:3494d33e3b04 112 fval = f ;
ambromar 0:3494d33e3b04 113
ambromar 0:3494d33e3b04 114 std::ostringstream stm ;
ambromar 0:3494d33e3b04 115 stm << std::hex << std::uppercase << ival ;
ambromar 0:3494d33e3b04 116
ambromar 0:3494d33e3b04 117 return stm.str() ;
ambromar 0:3494d33e3b04 118 }