get humidity data and send to LoRa gateway

Dependencies:   mbed X_NUCLEO_IKS01A2

main.cpp

Committer:
reversi
Date:
2020-04-22
Revision:
4:4a012fc4ecc4
Parent:
3:24204ec0bc36
Child:
5:90900ae27136

File content as of revision 4:4a012fc4ecc4:

#include "mbed.h"
#include "XNucleoIKS01A2.h"
#include <string>
#include <sstream>

#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);
}

string string_to_hex(string input)
{
    static const char hex_digits[] = "0123456789ABCDEF";

    std::string output;
    output.reserve(input.length() * 2);
    for (int i = 0; i < input.length(); i++)
    {
        output[2*i] = hex_digits[input[i] >> 4];
        output[2*i+1] = (hex_digits[input[i] & 15]);
    }
    return output;
}

int init() {
    pc.printf("Initialization..\r\n");
    
    uint8_t id;
    int error_code = ht_sensor->read_id(&id);
    if (error_code != 0) {
        pc.printf("Error %d reading sensor ID\r\n", error_code);
        return 1;
    }
    pc.printf("Connected to sensor with ID 0x%x\r\n", id);
    
    if (ht_sensor->enable() != 0) {
        pc.printf("Error enabling the ht sensor\r\n");
        return 2;
    }
    
    if (ht_sensor->set_odr(1.0f) != 0) {
        pc.printf("Error setting ODR\r\n");
        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;
        
        if (ht_sensor->get_humidity(&humidity) != 0)
            pc.printf("Error reading humidity\r\n");
        else {
            pc.printf("Humidity [%%]\t%f\r\n", humidity);

            std::ostringstream ss;
            ss << humidity;
        
            string val = ss.str();
            for(int i=0; i < val.length(); i++)
            {
                sprintf(&val[2*i],"%X", val[i]);
            }
        
            at_send("1515", val);
        
        }
        wait(5);
    }
}


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');
}