MQTT publish - uint16

Hello,

I've question about variable conversion. I have sensor SDC30 + lib, where function getCO2 return uint16_t.

I want to send the value to MQTT because I read values in NODE-RED. MQTT is possible to send const char *.

Any idea how to convert? I tried some examples but no chance :frowning:

Thanks

/*
Reading CO2, humidity and temperature from the SCD30
By: Nathan Seidle
SparkFun Electronics
Date: May 22nd, 2018
License: MIT. See license file for more information but you can
basically do whatever you want with this code.

Feel like supporting open source hardware?
Buy a board from SparkFun! CO₂ Humidity and Temperature Sensor - SCD30 - SEN-15112 - SparkFun Electronics

This example prints the current CO2 level, relative humidity, and temperature in C.

Hardware Connections:
If needed, attach a Qwiic Shield to your Arduino/Photon/ESP32 or other
Plug the device into an available Qwiic port
Open the serial monitor at 9600 baud to see the output
*/

#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 3);
IPAddress server(192, 168, 1, 55);

EthernetClient ethClient;
PubSubClient client(ethClient);

//Click here to get the library: http://librarymanager/All#SparkFun_SCD30
#include "SparkFun_SCD30_Arduino_Library.h"

SCD30 airSensor;

void setup()
{
Wire.begin();

Serial.begin(9600);
Serial.println("SCD30 Example");
Ethernet.begin(mac, ip);
delay(1500);
client.setServer(server, 1883);
airSensor.begin(); //This will cause readings to occur every two seconds
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic","hello world");
// ... and resubscribe
//client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void loop()
{
if (!client.connected()) {
reconnect();
}

if (airSensor.dataAvailable())
{
Serial.print("co2(ppm):");
Serial.print(airSensor.getCO2());
//uint16_t co2 = airSensor.getCO2();
uint16_t co2 = airSensor.getCO2();
//char* string = (char*) &co2;
client.publish("co2", co2);
//client.subscribe("co2/loznice");

Serial.print(" temp(C):");
Serial.print(airSensor.getTemperature(), 1);

Serial.print(" humidity(%):");
Serial.print(airSensor.getHumidity(), 1);

Serial.println();
}
else
//Serial.println("No data");

delay(1000);
}

Any idea how to convert?

https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1gad50d835af7e358518fd100179732e948.html

When I will come home, I will try this:

uint16_t co2 = airSensor.getCO2();
char buffer [sizeof(unsigned int)*8+1];
char * s = (char *) utoa(co2,buffer,10);
client.publish("co2", s);

I want output in decimal.

output from sensor is 1-10000.

Thanks

That Sparkfun device looks like a cool sensor Keany! I have a bunch of CO2 sensors from Vernier, Gravity and CO2meter.com. The onboard temp and humidity sensors (temperature will attenuate ndir readings and humidity will elevate it with an accompanying loss of precision) will do wonders for the accuracy of the output. I have a people counter in my office that is right ± 2 people. I'd really like to get that fixed, so I will be following this thread, as well as sending more of my hard earned money to Colorado!

ps, the spec sheet says the range is 400-10000, not 1-10K. 400 is what the CO2 level of fresh air was in 1963. It's a bit higher now. ... thanks humanity!