I am really keen to move from the Arduino IDE to PlatformIO but have experienced nothing but trouble when trying to migrate multi-file .ino projects across. Starting from scratch to try and learn the more robust C++ method I’m struggling to even move the simple example below across. I have read numerous threads and applied them to the point of only two errors but still it’s clearly not the right way to do things.
People have asked for a tutorial before and I certainly take the point that there are many references on this topic. I am conscious however that if I finally get the below working that it may not be the “best way” of doing things and I want to learn to be as efficient as possible.
I have not been lazy, I have tried many ways of splitting the OneWire functions out into their own file (protoType.h declarations, 1Wire.cpp containing the necessary variables and functions) but was wondering if some kind soul could take the single file example below and split to multi-file so I can analyse the “best-practice” approach before I continue down a path that meanders too far??
The below works fine as a single “main.cpp”
#include <Arduino.h>
#include <DallasTemperature.h>
#include "protoTypes.h"
//Variable definitions
int numberOfSensors = 0; // 1 Wire number of Sensors
float tempProbeA = 0; // DS18B20 Temperature variable for probe 1 (Upper)
float tempProbeP = 0; // DS18B20 Temperature variable for probe 2 (Lower)
float tempProbeF = 0; // DS18B20 Temperature variable for probe 3 (Filter Pack)
int tempProbeARounded = 0; // Probe 1 Float value as integer for Google Charts
int tempProbePRounded = 0; // Probe 2 Float value as integer for Google Charts
int tempProbeFRounded = 0; // Probe 3 Float value as integer for Google Charts
//Definitions
#define ONE_WIRE_BUS 04
//Instances
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
discoverOneWireDevices();
readOneWireTemperatures();
}
void loop() {
}
int discoverOneWireDevices(void) {
byte i;
byte addr[8];
int count = 0;
Serial.print("Looking for 1-Wire devices...\n\r");
while(oneWire.search(addr)) {
Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return 0;
}
count ++;
}
Serial.print("\n\r\n\rThat's it.\r\n");
Serial.print("Number of Sensors ");
numberOfSensors = count;
Serial.println(count);
return numberOfSensors;
}
void readOneWireTemperatures(){
Serial.println("Requesting temperatures:");
sensors.requestTemperatures();
Serial.println("Done!");
// Read each of our sensors and print the value
for (int i = 0; i < numberOfSensors; i++) { // Request temperatures from the number of sensor we discovered (See discoverOneWireDevices)
Serial.print("Temperature for Device ");
Serial.print( i );
Serial.print(" is: ");
Serial.println(sensors.getTempCByIndex(i) ); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
if (i == 0) {
tempProbeA = sensors.getTempCByIndex(i); // Hardcoded to allow max 3 OneWire Temp sensors
tempProbeARounded = tempProbeA; // Converts Float to Int for Charts mainly
//dtostrf(tempProbeA,2,1,tempProbeAAr);
}
else if (i == 1) {
tempProbeP = sensors.getTempCByIndex(i);
tempProbePRounded = tempProbeP; // Converts Float to Int for Charts mainly
}
else if (i == 2) {
tempProbeF = sensors.getTempCByIndex(i);
tempProbeFRounded = tempProbeF; // Converts Float to Int for Charts mainly
}
}
return;
}
protoTypes.h
//Function Declarations
int discoverOneWireDevices();
void readOneWireTemperatures ();
Any help would be greatly appreciated to start me on the right path.