I tried to code a CO2 sensor with the ESP 01, that publishes the ppm value to a MQTT Broker every 10 seconds. Everything works except for the publishing part. I always get an error that tells me, that I can't convert an int to char*. I tried many things but nothing worked.
Can you help me? If you need any extra info, just ask.
This is the error get:
Arduino: 1.8.13 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
C:\Users\sevis\Desktop\co2 - Kopie\co2\co2.ino: In function 'void loop()':
co2:53:26: error: invalid conversion from 'uint16_t {aka short unsigned int}' to 'char*' [-fpermissive]
sCO2 = sensor.getCO2();
^
exit status 1
invalid conversion from 'uint16_t {aka short unsigned int}' to 'char*' [-fpermissive]
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Here is the code:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_MQTT_FONA.h>
#include <Wire.h>
#include "SparkFunCCS811.h"
#define CCS_ADDR 0x5B
#define AIO_SERVER "192.168.1.123"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "****"
#define AIO_KEY "**********"
char* sCO2;
int timer;
CCS811 sensor(CCS_ADDR);
WiFiManager wifiManager;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
void setup() {
Wire.begin(0, 2);
Serial.begin(9600);
wifiManager.autoConnect("AutoConnectAP");
Serial.println("Wifi connected!");
MQTT_connect();
while(sensor.begin() == false) {
Serial.println("No Sensor");
mqtt.publish("Raum1_co2", "No Sensor");
delay(5000);
}
}
void loop() {
MQTT_connect();
//mqtt.publish("Raum1_co2", "1050");
if (sensor.dataAvailable()) {
sensor.readAlgorithmResults();
sCO2 = sensor.getCO2();
mqtt.publish("Raum1/CO2", sCO2);
Serial.print(sCO2);
Serial.println("ppm : Data published");
}
delay(10000);
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
while (mqtt.connect()) {
mqtt.disconnect();
delay(5000);
}
}
There is a number returned by sensor, and there is a pointer to characters you want to use to store it. Pointer is uninitialized and pointing nowhere (well, it's a global variable, so it point to the address 0, but there can be anything).
You are trying to change that location using some "almost" random number returned by sensor. This is not allowed (not without explicit typecast - in this case "yes, I want to shoot myself into the head").
Quick fix would be change char* sCO2;
to String sCO2;
as the String class implements assigment from uint16_t. And the print functon can handle printing the String type variable.
Slow fix is using char sCO2[6];
// should be big enough to store 0..65535 + zero termination character and you have to convert that unsigned integer into that space in your char array. It's basically using function utoa(value, sCO2, 10);
(10 is base of resulting string - aka decadic)