5G Academy / Mbed 2 deprecated Gruppo_Temperature

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 "string.h"
00003 #include "XNucleoIKS01A2.h"
00004 
00005 void modem_at_cmd(char*,int);
00006 void wait4join(void);
00007 void lora_conn(void);
00008 
00009 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00010 static HTS221Sensor *temperature = mems_expansion_board -> ht_sensor;
00011 
00012 char* msg1 = {"AT"};
00013 char* msg2 = {"AT+APPEUI=0000000000000001"};
00014 char* msg3 = {"AT+AK=00000000000000000000000000000001"};
00015 char* msg4 = {"AT+JOIN=1"};
00016 char msg5[64];
00017 char msg6[80];
00018 
00019 char c;
00020 float val_temperature;
00021 uint8_t id;
00022 
00023 Serial pc(D1, D0, 115200);
00024 Serial lora(PB_6, PA_10, 115200);
00025 
00026 int main()
00027 {
00028     temperature->enable();
00029     pc.printf("\r\n--- Starting new run ---\r\n");
00030     temperature->read_id(&id);
00031     pc.printf("HTS221 Sensor = 0x%X\r\n", id);
00032     lora_conn();
00033     while(1) {
00034         int i = 0;
00035         pc.printf("\r\n");
00036         temperature -> get_temperature(&val_temperature);
00037         pc.printf("temperatura [Celsius]:\t%f\n\r", val_temperature);
00038         memset(msg5, 0, 64*sizeof(char)); // fill msg5 of zeros
00039         sprintf(msg5, "{\"temp\": %f}", val_temperature); // converte in stringa c msg5
00040         pc.printf(msg5);
00041         pc.printf("\r\n");
00042         pc.printf("Lunghezza messaggio %d", (int)strlen(msg5));
00043         pc.printf("\r\n");
00044         sprintf(msg6, "AT+SEND=15,");
00045         for(i = 0; i<strlen(msg5); i++) {
00046             sprintf(msg6+11+2*i, "%X", *(msg5+i));
00047         }
00048         sprintf(msg6+11+2*i,",0");
00049         modem_at_cmd(msg6,(int)strlen(msg6));
00050         pc.printf("Inviato send\r\n");
00051         wait(3);
00052     }
00053 }
00054 
00055 void modem_at_cmd(char* buffer, int n) {
00056     for(uint8_t i=0; i<n; i++) {
00057         lora.putc(buffer[i]);
00058         pc.putc(buffer[i]);
00059     }
00060     lora.putc(13);
00061     pc.putc(13);
00062     pc.printf("\n");
00063     c = 0;
00064     do {
00065         if (lora.readable()) {
00066             c = lora.getc();
00067             pc.putc(c);
00068         }
00069     } while(c!=' ');
00070 }
00071 
00072 void wait4join() {
00073     c = 0;
00074     do {
00075         if (lora.readable()) {
00076             c = lora.getc();
00077             pc.putc(c);
00078         }
00079     } while(c != 'd');
00080 }
00081 
00082 void lora_conn() {
00083     pc.printf("Connessione al modem\r\n");
00084     modem_at_cmd(msg1,(int)strlen(msg1));
00085     pc.printf("Inviato AT\r\n");
00086     wait(1);
00087     modem_at_cmd(msg2,(int)strlen(msg2));
00088     pc.printf("Inviato EUI\r\n");
00089     wait(1);
00090     modem_at_cmd(msg3,(int)strlen(msg3));
00091     pc.printf("Inviato AK\r\n");
00092     wait(1);
00093     modem_at_cmd("AT+ADR=1",(int)strlen("AT+ADR=1"));
00094     pc.printf("Inviato AK\r\n");
00095     wait(1);
00096     modem_at_cmd("AT+DC=0",(int)strlen("AT+DC=0"));
00097     pc.printf("Inviato AK\r\n");
00098     wait(1);
00099     modem_at_cmd(msg4,(int)strlen(msg4));
00100     pc.printf("Inviato JOIN\r\n");
00101     wait4join();
00102 }