5G Academy / Mbed 2 deprecated humidity

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 "XNucleoIKS01A2.h"
00003 #include <string>
00004 #include <sstream>
00005 
00006 #define CR 0x0D
00007 
00008 using namespace std;
00009 
00010 void modem_at_cmd(string);
00011 void wait4join(void);
00012 
00013 Serial pc(D1, D0, 115200);
00014 Serial lora(PB_6, PA_10, 115200);
00015 
00016 static XNucleoIKS01A2* board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00017 static HTS221Sensor *ht_sensor = board->ht_sensor;
00018 char* msgSend[64];
00019 
00020 void join_lora_gateway(string eui = "0000000000000001", string ak = "00000000000000000000000000000001", string join_method = "1") {
00021     pc.printf("Test seriale no pc\r\n");
00022     modem_at_cmd("AT");
00023     pc.printf("Inviato AT\r\n");
00024     wait(1);
00025     modem_at_cmd("AT+APPEUI="+eui);
00026     pc.printf("Inviato EUI\r\n");
00027     wait(1);
00028     modem_at_cmd("AT+AK="+ak);
00029     pc.printf("Inviato AK\r\n");
00030     wait(1);
00031     modem_at_cmd("AT+JOIN="+join_method);
00032     pc.printf("Inviato JOIN\r\n");
00033     wait4join();
00034     modem_at_cmd("AT+DC=0");
00035     pc.printf("Disabled DC\r\n");
00036     wait(1);
00037     modem_at_cmd("AT+ADR=1");
00038     pc.printf("Enabled ADR\r\n");
00039     wait(1);
00040 }
00041 
00042 string string_to_hex(string input)
00043 {
00044     static const char hex_digits[] = "0123456789ABCDEF";
00045 
00046     std::string output;
00047     output.reserve(input.length() * 2);
00048     for (int i = 0; i < input.length(); i++)
00049     {
00050         output[2*i] = hex_digits[input[i] >> 4];
00051         output[2*i+1] = (hex_digits[input[i] & 15]);
00052     }
00053     return output;
00054 }
00055 
00056 int init() {
00057     pc.printf("Initialization..\r\n");
00058     
00059     uint8_t id;
00060     int error_code = ht_sensor->read_id(&id);
00061     if (error_code != 0) {
00062         pc.printf("Error %d reading sensor ID\r\n", error_code);
00063         return 1;
00064     }
00065     pc.printf("Connected to sensor with ID 0x%x\r\n", id);
00066     
00067     if (ht_sensor->enable() != 0) {
00068         pc.printf("Error enabling the ht sensor\r\n");
00069         return 2;
00070     }
00071     
00072     if (ht_sensor->set_odr(1.0f) != 0) {
00073         pc.printf("Error setting ODR\r\n");
00074         return 3;
00075     }
00076     
00077     join_lora_gateway();
00078     
00079     return 0;
00080 }
00081 
00082 void at_send(uint8_t port, float hum, uint8_t ack = 0) {
00083     char msg[30];
00084     char send[64];
00085     uint8_t i=0;
00086     sprintf(msg,"{\"humidity\":%.2f}", hum);
00087     pc.printf(msg);
00088     pc.printf("\r\n");
00089     pc.printf("Lunghezza messaggio %d",(int)strlen(msg));
00090     pc.printf("\r\n");
00091     sprintf(send,"AT+SEND=%d,",port);
00092     for(i=0;i<strlen(msg);i++)
00093     {
00094         sprintf(send+11+2*i,"%X",*(msg+i));
00095     }
00096     sprintf(send+11+2*i,",%d",ack);
00097     modem_at_cmd(send);
00098     pc.printf(send);
00099     pc.printf("\r\nInviato send\r\n");
00100 }
00101 
00102 int main() {
00103     
00104     if (init() != 0) {
00105         pc.printf("Init failed");
00106         return 1;
00107     }
00108         
00109     while(1) {
00110         float humidity = 0;
00111         
00112         if (ht_sensor->get_humidity(&humidity) != 0)
00113             pc.printf("Error reading humidity\r\n");
00114         else {
00115             pc.printf("Humidity [%%]\t%f\r\n", humidity);
00116             at_send(10, humidity, 0);
00117         }
00118         wait(5);
00119     }
00120 }
00121 
00122 
00123 void modem_at_cmd(string buffer){
00124     for(uint16_t i = 0; i < buffer.length(); i++) {
00125         lora.putc(buffer[i]);
00126         pc.putc(buffer[i]);
00127     }
00128     lora.putc(CR);
00129     pc.putc(CR);
00130     pc.printf("\n");
00131     
00132     char c = 0;
00133     do {
00134         if (lora.readable()) {
00135             c = lora.getc();
00136             pc.putc(c);
00137         }
00138     } while(c != ' ');
00139 }
00140 
00141 void wait4join(){
00142     char c = 0;
00143     do {
00144         if (lora.readable()) {
00145             c = lora.getc();
00146             pc.putc(c);
00147         }
00148     } while(c != 'd');
00149 }