get humidity data and send to LoRa gateway

Dependencies:   mbed X_NUCLEO_IKS01A2

Committer:
reversi
Date:
Wed Apr 22 14:59:39 2020 +0000
Revision:
3:24204ec0bc36
Parent:
2:0b1cbb0060bc
Child:
4:4a012fc4ecc4
last

Who changed what in which revision?

UserRevisionLine numberNew contents of line
reversi 0:5b9fdd76a7d6 1 #include "mbed.h"
reversi 0:5b9fdd76a7d6 2 #include "XNucleoIKS01A2.h"
reversi 1:73299b677405 3 #include <string>
reversi 1:73299b677405 4 #include <sstream>
reversi 0:5b9fdd76a7d6 5
reversi 1:73299b677405 6 #define CR 0x0D
reversi 1:73299b677405 7
reversi 1:73299b677405 8 using namespace std;
reversi 1:73299b677405 9
reversi 1:73299b677405 10 void modem_at_cmd(string);
reversi 1:73299b677405 11 void wait4join(void);
reversi 1:73299b677405 12
reversi 1:73299b677405 13 Serial pc(D1, D0, 115200);
reversi 1:73299b677405 14 Serial lora(PB_6, PA_10, 115200);
reversi 1:73299b677405 15
reversi 1:73299b677405 16 static XNucleoIKS01A2* board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
reversi 1:73299b677405 17 static HTS221Sensor *ht_sensor = board->ht_sensor;
reversi 0:5b9fdd76a7d6 18
reversi 1:73299b677405 19 void join_lora_gateway(string eui = "0000000000000001", string ak = "00000000000000000000000000000001", string join_method = "1") {
reversi 1:73299b677405 20 pc.printf("Test seriale no pc\r\n");
reversi 1:73299b677405 21 modem_at_cmd("AT");
reversi 1:73299b677405 22 pc.printf("Inviato AT\r\n");
reversi 1:73299b677405 23 wait(1);
reversi 1:73299b677405 24 modem_at_cmd("AT+APPEUI="+eui);
reversi 1:73299b677405 25 pc.printf("Inviato EUI\r\n");
reversi 1:73299b677405 26 wait(1);
reversi 1:73299b677405 27 modem_at_cmd("AT+AK="+ak);
reversi 1:73299b677405 28 pc.printf("Inviato AK\r\n");
reversi 1:73299b677405 29 wait(1);
reversi 1:73299b677405 30 modem_at_cmd("AT+JOIN="+join_method);
reversi 1:73299b677405 31 pc.printf("Inviato JOIN\r\n");
reversi 1:73299b677405 32 wait4join();
reversi 1:73299b677405 33 modem_at_cmd("AT+DC=0");
reversi 1:73299b677405 34 pc.printf("Disabled DC\r\n");
reversi 1:73299b677405 35 wait(1);
reversi 1:73299b677405 36 modem_at_cmd("AT+ADR=1");
reversi 1:73299b677405 37 pc.printf("Enabled ADR\r\n");
reversi 1:73299b677405 38 wait(1);
reversi 1:73299b677405 39 }
reversi 1:73299b677405 40
reversi 2:0b1cbb0060bc 41 string string_to_hex(string input)
reversi 2:0b1cbb0060bc 42 {
reversi 2:0b1cbb0060bc 43 static const char hex_digits[] = "0123456789ABCDEF";
reversi 2:0b1cbb0060bc 44
reversi 2:0b1cbb0060bc 45 std::string output;
reversi 2:0b1cbb0060bc 46 output.reserve(input.length() * 2);
reversi 2:0b1cbb0060bc 47 for (int i = 0; i < input.length(); i++)
reversi 2:0b1cbb0060bc 48 {
reversi 2:0b1cbb0060bc 49 output[2*i] = hex_digits[input[i] >> 4];
reversi 2:0b1cbb0060bc 50 output[2*i+1] = (hex_digits[input[i] & 15]);
reversi 2:0b1cbb0060bc 51 }
reversi 2:0b1cbb0060bc 52 return output;
reversi 2:0b1cbb0060bc 53 }
reversi 1:73299b677405 54
reversi 1:73299b677405 55 int init() {
reversi 0:5b9fdd76a7d6 56 pc.printf("Initialization..\r\n");
reversi 0:5b9fdd76a7d6 57
reversi 0:5b9fdd76a7d6 58 uint8_t id;
reversi 0:5b9fdd76a7d6 59 int error_code = ht_sensor->read_id(&id);
reversi 0:5b9fdd76a7d6 60 if (error_code != 0) {
reversi 0:5b9fdd76a7d6 61 pc.printf("Error %d reading sensor ID\r\n", error_code);
reversi 0:5b9fdd76a7d6 62 return 1;
reversi 0:5b9fdd76a7d6 63 }
reversi 0:5b9fdd76a7d6 64 pc.printf("Connected to sensor with ID 0x%x\r\n", id);
reversi 0:5b9fdd76a7d6 65
reversi 0:5b9fdd76a7d6 66 if (ht_sensor->enable() != 0) {
reversi 0:5b9fdd76a7d6 67 pc.printf("Error enabling the ht sensor\r\n");
reversi 0:5b9fdd76a7d6 68 return 2;
reversi 0:5b9fdd76a7d6 69 }
reversi 0:5b9fdd76a7d6 70
reversi 0:5b9fdd76a7d6 71 if (ht_sensor->set_odr(1.0f) != 0) {
reversi 0:5b9fdd76a7d6 72 pc.printf("Error setting ODR\r\n");
reversi 0:5b9fdd76a7d6 73 return 3;
reversi 0:5b9fdd76a7d6 74 }
reversi 0:5b9fdd76a7d6 75
reversi 1:73299b677405 76 join_lora_gateway();
reversi 1:73299b677405 77
reversi 1:73299b677405 78 return 0;
reversi 1:73299b677405 79 }
reversi 1:73299b677405 80
reversi 1:73299b677405 81 void at_send(string port, string msg, string ack = "0") {
reversi 1:73299b677405 82 modem_at_cmd("AT+SEND=" +port+ "," +msg+ "," +ack);
reversi 1:73299b677405 83 }
reversi 1:73299b677405 84
reversi 1:73299b677405 85 int main() {
reversi 1:73299b677405 86
reversi 1:73299b677405 87 if (init() != 0) {
reversi 1:73299b677405 88 pc.printf("Init failed");
reversi 1:73299b677405 89 return 1;
reversi 1:73299b677405 90 }
reversi 1:73299b677405 91
reversi 0:5b9fdd76a7d6 92 while(1) {
reversi 1:73299b677405 93 float humidity = 0;
reversi 0:5b9fdd76a7d6 94
reversi 0:5b9fdd76a7d6 95 if (ht_sensor->get_humidity(&humidity) != 0)
reversi 0:5b9fdd76a7d6 96 pc.printf("Error reading humidity\r\n");
reversi 1:73299b677405 97 else {
reversi 1:73299b677405 98 pc.printf("Humidity [%%]\t%f\r\n", humidity);
reversi 1:73299b677405 99
reversi 1:73299b677405 100 std::ostringstream ss;
reversi 1:73299b677405 101 ss << humidity;
reversi 3:24204ec0bc36 102
reversi 3:24204ec0bc36 103 string val = ss.str();
reversi 3:24204ec0bc36 104 for(int i=0; i < val.length(); i++)
reversi 3:24204ec0bc36 105 {
reversi 3:24204ec0bc36 106 sprintf(val[2*i],"%X",*(val[i]));
reversi 3:24204ec0bc36 107 }
reversi 3:24204ec0bc36 108
reversi 3:24204ec0bc36 109 at_send("1515", val);
reversi 3:24204ec0bc36 110
reversi 1:73299b677405 111 }
reversi 2:0b1cbb0060bc 112 wait(5);
reversi 0:5b9fdd76a7d6 113 }
reversi 0:5b9fdd76a7d6 114 }
reversi 1:73299b677405 115
reversi 1:73299b677405 116
reversi 1:73299b677405 117 void modem_at_cmd(string buffer){
reversi 1:73299b677405 118 for(uint16_t i = 0; i < buffer.length(); i++) {
reversi 1:73299b677405 119 lora.putc(buffer[i]);
reversi 1:73299b677405 120 pc.putc(buffer[i]);
reversi 1:73299b677405 121 }
reversi 1:73299b677405 122 lora.putc(CR);
reversi 1:73299b677405 123 pc.putc(CR);
reversi 1:73299b677405 124 pc.printf("\n");
reversi 1:73299b677405 125
reversi 1:73299b677405 126 char c = 0;
reversi 1:73299b677405 127 do {
reversi 1:73299b677405 128 if (lora.readable()) {
reversi 1:73299b677405 129 c = lora.getc();
reversi 1:73299b677405 130 pc.putc(c);
reversi 1:73299b677405 131 }
reversi 1:73299b677405 132 } while(c != ' ');
reversi 1:73299b677405 133 }
reversi 1:73299b677405 134
reversi 1:73299b677405 135 void wait4join(){
reversi 1:73299b677405 136 char c = 0;
reversi 1:73299b677405 137 do {
reversi 1:73299b677405 138 if (lora.readable()) {
reversi 1:73299b677405 139 c = lora.getc();
reversi 1:73299b677405 140 pc.putc(c);
reversi 1:73299b677405 141 }
reversi 1:73299b677405 142 } while(c != 'd');
reversi 1:73299b677405 143 }