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 ![]()
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);
}