Serial Example for hexiwear

main.cpp

Committer:
dewantkatare
Date:
11 months ago
Revision:
1:a758914c25d5
Parent:
0:79c0c4cd1ab3

File content as of revision 1:a758914c25d5:

#include "mbed.h"

Serial pc( USBTX, USBRX);
Serial uart(PTD3,PTD2); //Tx, Rx (D1, D0)

typedef unsigned char byte;

uint8_t cmd_get_sensor[] = {0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
int temperature;
int Co2PPM;

bool Received_data()
{
    return true;
}

void init()
{
    pc.printf("\r\nFirst Loop");
    uint8_t data[9];
    uint8_t cd[9];
    int i=0;
    for(i=0; i<sizeof(cmd_get_sensor); i++) {
        //cd[i] = uart.putc(cmd_get_sensor[i]);
        data[i] = uart.getc();
        pc.printf("\n\r val: ", data);
    }
    
   /* pc.printf("\r\nHere in first loop:");
    for(int i=0; i<9; i++) {
        //Printing uart values to confirm
        pc.printf("\r\nwritten value[%d]: %u\n",i, cd[i]);
    }*/
    
    pc.printf("\r\n");
    
    pc.printf("\r\n********************************************\r\n");
    pc.printf("\r\nHere reading values:");
    for(int i=0; i<9; i++) {
        pc.printf("\r\nRead value[%d]: %u\n",i, data[i]);
    }

   /* if((i !=9)||(1 + (0xff^(byte) (data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]))) != data[8]) {
        return false;
    }*/
    Co2PPM = data[2] * 256 + data[3];
    temperature = data[4] - 40;
        pc.printf("\r\nSecond Loop");
        pc.printf("\r\nTemperature: %d\n", temperature);
        pc.printf("\r\nCo2:  %d\n", Co2PPM);
        wait(1);
}

int main()
{
    while(1) {
        uart.baud(9600);
        pc.baud(115200);
        pc.printf("\r\nReading data from Co2 Sensor:\n");
        pc.printf("\r\n************************************\n");
        init();
        pc.printf("\r\nend");
        }
}