Hi all, I’m not a developer but I made this code that works for my PM uart sensor. Now I would like to convert this to an esphome sensor but how can I split the code into .cpp and .h? Or is there another way to integrate custom code into esphome sensors?
And yes, I could buy one off the standard already integrated sensors but I didn’t…
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D1, D2); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void printValues(byte data[]) {
// Print the values to the terminal
int pm1_standard = (data[4] << 8) | data[5];
int pm25_standard = (data[6] << 8) | data[7];
int pm10_standard = (data[8] << 8) | data[9];
Serial.print("PM1.0: ");
Serial.print(pm1_standard);
Serial.print(" ug/m3, PM2.5: ");
Serial.print(pm25_standard);
Serial.print(" ug/m3, PM10: ");
Serial.print(pm10_standard);
Serial.println(" ug/m3");
}
uint16_t calculateChecksum(byte data[]) {
uint16_t checksum = 0;
// Sum the bytes from index 0 to 29
for (int i = 0; i < 30; i++) {
checksum += data[i];
}
return checksum;
}
void loop() {
if (mySerial.available() >= 32) {
// Wait for the reception of at least 32 bytes (complete data frame)
if (mySerial.read() == 0x42 && mySerial.read() == 0x4D) {
// Check for the beginning of a data frame
byte data[32];
for (int i = 0; i < 32; i++) {
data[i] = mySerial.read();
}
// Check the received checksum
uint16_t receivedChecksum = (data[30] << 8) | data[31];
uint16_t calculatedChecksum = calculateChecksum(data);
if (receivedChecksum == 0xFFFF || receivedChecksum == calculatedChecksum) {
// Print the values
printValues(data);
}
}
}
}
I have read the documents a thousand times, but there is a discrepancy between the maturity of the documentation and my knowledge of the subject. I need some real developers view and knowledge at this point.
I have now created a folder structure, a .h, .cpp and a yaml. But I have an error message in the editor that d3_sensor cannot be found. The compiler can find the files but returns an error message.
When I compile I get the message;
/config/esphome/finedust-1.yaml: In lambda function:
/config/esphome/finestof-1.yaml:33:28: error: expected type-specifier before 'D3Sensor'
33 | auto d3_sensor = new D3Sensor();
| ^~~~~~~~