0

I'm a bit of a noob, so excuse if this is more obvious to me than to you.

I want to convert a u16 to a string. The u16 is the data coming in from the sensor. I need it as a string, or anything else you can recommend that I can transmit via I2C to the slave.

Here's the code:

err_t print_result(const char* str,u16 value)
{
    if(NULL==str)
        return ERROR_PARAM;
    Serial.print(str);
    Serial.println(value);


    return NO_ERROR;
}

u16 value is what I cant to convert.

The output from "value" is being combined with the previous Serial.Print from str, which is:

const char *str[]={"sensor num: ","PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
                    "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
                    "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
                    };

So when both serial.print statements are combined we get the nicely formatted output:

PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): 18
PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): 26
PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): 30
PM1.0 concentration(Atmospheric environment,unit:ug/m3): 18
PM2.5 concentration(Atmospheric environment,unit:ug/m3): 26
PM10 concentration(Atmospheric environment,unit:ug/m3): 30

But, I would like to get each value of 18, 26 and 30 separately, so I can send them using the wire library.

Any help appreciated.

Here's the complete code:

#include "Seeed_HM330X.h"
#ifdef  ARDUINO_SAMD_VARIANT_COMPLIANCE
  #define SERIAL SerialUSB
#else
  #define SERIAL Serial
#endif


HM330X sensor;
u8 buf[30];


const char *str[]={"sensor num: ","PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
                    "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
                    "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
                    "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
                    };

err_t print_result(const char* str,u16 value)
{
    if(NULL==str)
        return ERROR_PARAM;
    SERIAL.print(str);
    SERIAL.println(value);

    return NO_ERROR;
}

/*parse buf with 29 u8-data*/
err_t parse_result(u8 *data)
{
    u16 value=0;
    err_t NO_ERROR;
    if(NULL==data)
        return ERROR_PARAM;
    for(int i=1;i<8;i++)
    {
         value = (u16)data[i*2]<<8|data[i*2+1];
         print_result(str[i-1],value);

    }
}

err_t parse_result_value(u8 *data)
{
    if(NULL==data)
        return ERROR_PARAM;
    for(int i=0;i<28;i++)
    {
        SERIAL.print(data[i],HEX);
        SERIAL.print("  ");
        if((0==(i)%5)||(0==i))
        {
            SERIAL.println(" ");
        }
    }
    u8 sum=0;
    for(int i=0;i<28;i++)
    {
        sum+=data[i];
    }
    if(sum!=data[28])
    {
        SERIAL.println("wrong checkSum!!!!");
    }
    SERIAL.println(" ");
    SERIAL.println(" ");
    return NO_ERROR;
}

void setup()
{

    SERIAL.begin(115200);
    delay(100);
    SERIAL.println("Serial start");
    if(sensor.init())
    {
        SERIAL.println("HM330X init failed!!!");
        while(1);
    }

}


void loop()
{ 
    if(sensor.read_sensor_value(buf,29))
    {
        SERIAL.println("HM330X read result failed!!!");
    }
    parse_result_value(buf);
    parse_result(buf);
    SERIAL.println(" ");
    SERIAL.println(" ");
    SERIAL.println(" ");
    delay(5000);
}
  • 1
  • 1
    The values are in the buffer (big-endian order); (u16)data[i*2]<<8|data[i*2+1]. – Mikael Patel Feb 17 at 17:06
  • @MikaelPatel yes, that's the original – R0b0tn1k Feb 17 at 19:37
  • @MikaelPatel It's doing some sort of calculation to it, multiplying and things, not understanding that line. Btw, what type is the u16, byte? – R0b0tn1k Feb 17 at 19:38
  • @jsotola I need each value separately so I can send it via I2C to the slave. – R0b0tn1k Feb 17 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.