Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_IKS01A2
main.cpp@1:7b9e61c70708, 2020-04-22 (annotated)
- Committer:
- rschiano75
- Date:
- Wed Apr 22 16:27:48 2020 +0000
- Revision:
- 1:7b9e61c70708
- Parent:
- 0:3494d33e3b04
- Child:
- 2:03da1eb4ebc8
Invio Pressione con LoRaWAN
Who changed what in which revision?
User | Revision | Line number | New 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); |
rschiano75 | 1:7b9e61c70708 | 14 | void at_send(uint8_t port, float pres, uint8_t ack); |
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 |
rschiano75 | 1:7b9e61c70708 | 34 | char* msg_template_pressure = {"AT+SEND=143,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* |
rschiano75 | 1:7b9e61c70708 | 71 | at_send(10, val_pressure, 0); |
ambromar | 0:3494d33e3b04 | 72 | pc.printf("Inviato Dato Pressione\r\n"); |
rschiano75 | 1:7b9e61c70708 | 73 | wait(5.0); |
ambromar | 0:3494d33e3b04 | 74 | } |
ambromar | 0:3494d33e3b04 | 75 | } |
ambromar | 0:3494d33e3b04 | 76 | |
rschiano75 | 1:7b9e61c70708 | 77 | void at_send(uint8_t port, float pres, uint8_t ack) { |
rschiano75 | 1:7b9e61c70708 | 78 | char msg[30]; |
rschiano75 | 1:7b9e61c70708 | 79 | char send[64]; |
rschiano75 | 1:7b9e61c70708 | 80 | uint8_t i=0; |
rschiano75 | 1:7b9e61c70708 | 81 | sprintf(msg,"{\"pressure\":%.2f}", pres); |
rschiano75 | 1:7b9e61c70708 | 82 | pc.printf(msg); |
rschiano75 | 1:7b9e61c70708 | 83 | pc.printf("\r\n"); |
rschiano75 | 1:7b9e61c70708 | 84 | pc.printf("Lunghezza messaggio %d",(int)strlen(msg)); |
rschiano75 | 1:7b9e61c70708 | 85 | pc.printf("\r\n"); |
rschiano75 | 1:7b9e61c70708 | 86 | sprintf(send,"AT+SEND=%d,",port); |
rschiano75 | 1:7b9e61c70708 | 87 | for(i=0;i<strlen(msg);i++) |
rschiano75 | 1:7b9e61c70708 | 88 | { |
rschiano75 | 1:7b9e61c70708 | 89 | sprintf(send+11+2*i,"%X",*(msg+i)); |
rschiano75 | 1:7b9e61c70708 | 90 | } |
rschiano75 | 1:7b9e61c70708 | 91 | sprintf(send+11+2*i,",%d",ack); |
rschiano75 | 1:7b9e61c70708 | 92 | modem_at_cmd(send,(int)strlen(send)); |
rschiano75 | 1:7b9e61c70708 | 93 | pc.printf(send); |
rschiano75 | 1:7b9e61c70708 | 94 | pc.printf("\r\nInviato send\r\n"); |
rschiano75 | 1:7b9e61c70708 | 95 | } |
rschiano75 | 1:7b9e61c70708 | 96 | |
ambromar | 0:3494d33e3b04 | 97 | void modem_at_cmd(char* buffer, int n){ |
ambromar | 0:3494d33e3b04 | 98 | for(uint8_t i=0; i<n; i++) { |
ambromar | 0:3494d33e3b04 | 99 | lora.putc(buffer[i]); |
ambromar | 0:3494d33e3b04 | 100 | pc.putc(buffer[i]); |
ambromar | 0:3494d33e3b04 | 101 | } |
ambromar | 0:3494d33e3b04 | 102 | lora.putc(13); |
ambromar | 0:3494d33e3b04 | 103 | pc.putc(13); |
ambromar | 0:3494d33e3b04 | 104 | pc.printf("\n"); |
ambromar | 0:3494d33e3b04 | 105 | c=0; |
ambromar | 0:3494d33e3b04 | 106 | do { |
ambromar | 0:3494d33e3b04 | 107 | if (lora.readable()) { |
ambromar | 0:3494d33e3b04 | 108 | c = lora.getc(); |
ambromar | 0:3494d33e3b04 | 109 | pc.putc(c); |
ambromar | 0:3494d33e3b04 | 110 | } |
ambromar | 0:3494d33e3b04 | 111 | } while(c!=' '); |
ambromar | 0:3494d33e3b04 | 112 | } |
ambromar | 0:3494d33e3b04 | 113 | |
ambromar | 0:3494d33e3b04 | 114 | void wait4join(){ |
ambromar | 0:3494d33e3b04 | 115 | c=0; |
ambromar | 0:3494d33e3b04 | 116 | do { |
ambromar | 0:3494d33e3b04 | 117 | if (lora.readable()) { |
ambromar | 0:3494d33e3b04 | 118 | c = lora.getc(); |
ambromar | 0:3494d33e3b04 | 119 | pc.putc(c); |
ambromar | 0:3494d33e3b04 | 120 | } |
ambromar | 0:3494d33e3b04 | 121 | }while(c!='d'); |
ambromar | 0:3494d33e3b04 | 122 | } |