get humidity data and send to LoRa gateway

Dependencies:   mbed X_NUCLEO_IKS01A2

Revision:
1:73299b677405
Parent:
0:5b9fdd76a7d6
Child:
2:0b1cbb0060bc
--- a/main.cpp	Wed Apr 22 13:47:09 2020 +0000
+++ b/main.cpp	Wed Apr 22 14:20:38 2020 +0000
@@ -1,12 +1,45 @@
 #include "mbed.h"
 #include "XNucleoIKS01A2.h"
+#include <string>
+#include <sstream>
 
-int main() {
-    
-    Serial pc(PA_2, PA_3, 115200);
-    XNucleoIKS01A2* board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
-    HTS221Sensor *ht_sensor = board->ht_sensor;
+#define CR 0x0D
+
+using namespace std;
+
+void modem_at_cmd(string);
+void wait4join(void);
+
+Serial pc(D1, D0, 115200);
+Serial lora(PB_6, PA_10, 115200);
+
+static XNucleoIKS01A2* board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+static HTS221Sensor *ht_sensor = board->ht_sensor;
 
+void join_lora_gateway(string eui = "0000000000000001", string ak = "00000000000000000000000000000001", string join_method = "1") {
+    pc.printf("Test seriale no pc\r\n");
+    modem_at_cmd("AT");
+    pc.printf("Inviato AT\r\n");
+    wait(1);
+    modem_at_cmd("AT+APPEUI="+eui);
+    pc.printf("Inviato EUI\r\n");
+    wait(1);
+    modem_at_cmd("AT+AK="+ak);
+    pc.printf("Inviato AK\r\n");
+    wait(1);
+    modem_at_cmd("AT+JOIN="+join_method);
+    pc.printf("Inviato JOIN\r\n");
+    wait4join();
+    modem_at_cmd("AT+DC=0");
+    pc.printf("Disabled DC\r\n");
+    wait(1);
+    modem_at_cmd("AT+ADR=1");
+    pc.printf("Enabled ADR\r\n");
+    wait(1);
+}
+
+
+int init() {
     pc.printf("Initialization..\r\n");
     
     uint8_t id;
@@ -27,20 +60,64 @@
         return 3;
     }
     
+    join_lora_gateway();
+    
+    return 0;
+}
+
+void at_send(string port, string msg, string ack = "0") {
+    modem_at_cmd("AT+SEND=" +port+ "," +msg+ "," +ack);
+}
+
+int main() {
+    
+    if (init() != 0) {
+        pc.printf("Init failed");
+        return 1;
+    }
+        
     while(1) {
-        float humidity = 0, temperature = 0;
+        float humidity = 0;
         
         if (ht_sensor->get_humidity(&humidity) != 0)
             pc.printf("Error reading humidity\r\n");
-        else
-            pc.printf("Humidity [%%]\t\t%f\r\n", humidity);
-        
-        if (ht_sensor->get_temperature(&temperature) != 0)
-            pc.printf("Error reading temperature\r\n");
-        else
-            pc.printf("Temperature ['C]\t%f\r\n", temperature);
-        
-        pc.printf("\r\n");
+        else {
+            pc.printf("Humidity [%%]\t%f\r\n", humidity);
+
+            std::ostringstream ss;
+            ss << humidity;
+            
+            at_send("1515", ss.str());
+        }
         wait(1);
     }
 }
+
+
+void modem_at_cmd(string buffer){
+    for(uint16_t i = 0; i < buffer.length(); i++) {
+        lora.putc(buffer[i]);
+        pc.putc(buffer[i]);
+    }
+    lora.putc(CR);
+    pc.putc(CR);
+    pc.printf("\n");
+    
+    char c = 0;
+    do {
+        if (lora.readable()) {
+            c = lora.getc();
+            pc.putc(c);
+        }
+    } while(c != ' ');
+}
+
+void wait4join(){
+    char c = 0;
+    do {
+        if (lora.readable()) {
+            c = lora.getc();
+            pc.putc(c);
+        }
+    } while(c != 'd');
+}
\ No newline at end of file