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
Diff: main.cpp
- Revision:
- 0:3494d33e3b04
- Child:
- 1:7b9e61c70708
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Apr 22 15:06:28 2020 +0000
@@ -0,0 +1,118 @@
+#include "mbed.h"
+#include "XNucleoIKS01A2.h"
+#include "string.h"
+#include <sstream>
+#include <iostream>
+using namespace std;
+
+//Sensore per la misura della pressione atmosferica + Comunicazione modem LoRa
+// Ambrosino-Rivellini-Salernitano
+
+// Prototipi funzioni
+void modem_at_cmd(char*,int);
+void wait4join(void);
+std::string ieee_float_to_hex( float f );
+
+// Variabili globali
+Serial pc(D1,D0,115200);
+Serial lora(PB_6,PA_10,115200);
+
+static XNucleoIKS01A2 *myBoard = XNucleoIKS01A2::instance(D14,D15,D4,D5);
+static LPS22HBSensor *pressure = myBoard->pt_sensor;
+uint8_t id;
+float val_pressure;
+
+char c;
+
+char* msg1 = {"AT"};
+char* msg2 = {"AT+APPEUI=0000000000000001"};
+char* msg3 = {"AT+AK=00000000000000000000000000000001"};
+char* msg4 = {"AT+JOIN=1"};
+char* msg5 = {"AT+SEND=15,3031323334,0"};
+char* msg6 = {"AT+DC=0"}; //modifica per disabilitare duty cycle
+char* msg7 = {"AT+ADR=1"}; //modifica per abilitare auto data rate
+char* msg_template_pressure = {"AT+SEND=5072657373696f6e65203a20"}; // template stringa in HEX "Pressione : "
+
+int main() {
+ char* msg_pressure ;
+ std::string hex_float ;
+
+ // Pressure Setup
+ pressure->enable();
+ pc.printf("Sensore per la misurazione di pressione abilitato\r\n");
+ pressure->read_id(&id);
+ pc.printf("ID pari a 0x%X\r\n",id);
+ pc.printf("Test seriale no pc\r\n");
+
+ // Modem Setup
+ modem_at_cmd(msg1,(int)strlen(msg1));
+ pc.printf("Inviato AT\r\n");
+ wait(1);
+ modem_at_cmd(msg2,(int)strlen(msg2));
+ pc.printf("Inviato EUI\r\n");
+ wait(1);
+ modem_at_cmd(msg3,(int)strlen(msg3));
+ pc.printf("Inviato AK\r\n");
+ wait(1);
+ modem_at_cmd(msg4,(int)strlen(msg4));
+ pc.printf("Inviato JOIN\r\n");
+ wait4join();
+ modem_at_cmd(msg5,(int)strlen(msg5));
+ pc.printf("Inviato send\r\n");
+ modem_at_cmd(msg6,(int)strlen(msg6));
+ pc.printf("Inviata richiesta disabilitazione duty cycl--e\r\n");
+ modem_at_cmd(msg7,(int)strlen(msg7));
+ pc.printf("Inviata richiesta data rate adattivo\r\n");
+
+ while(1) {
+ pressure->get_pressure(&val_pressure);
+ pc.printf("Valori pressione misurati [mbar]:\t%f\r\n",val_pressure);
+ // converte il float val_pressure in un char*
+ hex_float = ieee_float_to_hex(val_pressure);
+ char *cstr = new char[hex_float.length() + 1];
+ std::strcpy(cstr, hex_float.c_str());
+ msg_pressure = strcat(msg_template_pressure,cstr);
+ modem_at_cmd(msg_pressure,(int)strlen(msg_pressure));
+ pc.printf("Inviato Dato Pressione\r\n");
+ wait(1.0);
+ }
+}
+
+void modem_at_cmd(char* buffer, int n){
+ for(uint8_t i=0; i<n; i++) {
+ lora.putc(buffer[i]);
+ pc.putc(buffer[i]);
+ }
+ lora.putc(13);
+ pc.putc(13);
+ pc.printf("\n");
+ c=0;
+ do {
+ if (lora.readable()) {
+ c = lora.getc();
+ pc.putc(c);
+ }
+ } while(c!=' ');
+}
+
+void wait4join(){
+ c=0;
+ do {
+ if (lora.readable()) {
+ c = lora.getc();
+ pc.putc(c);
+ }
+ }while(c!='d');
+}
+
+std::string ieee_float_to_hex( float f )
+{
+
+ union { float fval ; std::uint32_t ival ; };
+ fval = f ;
+
+ std::ostringstream stm ;
+ stm << std::hex << std::uppercase << ival ;
+
+ return stm.str() ;
+}