I have learned quite a bit reading through this. Thanks for all the contributions.
A question - I have a setup a multiple source file project consisting of
main.cpp
sensors.cpp
sensors.h
main.cpp contains setup() and loop()
sensors.cpp contains functions for getting data from sensors
I include sensors.h in the main.cpp
However, I am confused as to where I put the include for Arduino.h? If I put it in main.cpp but not sensors.h the program will not compile. However, if I put it in sensors.h but not in main.cpp it all works correctly.
My question is why? and should/could I put it in both places.
Here is some of my code for clarity:
sensors.h
/***************************************************************************
* Header file for functions used for connecting to the bme680 module and
* water level sensor
***************************************************************************/
#ifndef SENSORS_H
#define SENSORS_H
/* libraries */
#include <RH_RF95.h> /* Radiohead RFM95 Radio lib - needed for RH_RF95_MAX_MESSAGE_LEN */
#include <Wire.h> /* I2C interface library */
#include <Adafruit_Sensor.h> /* Adafruit Unified Sensor library */
#include <Adafruit_BME680.h> /* Adafruit BME680 library */
/* sensor paramaters */
#define LEVEL_PIN 13 /* Digital GPIO pin to read liquid level state */
#define BME680_I2C_ADDRESS 0x77 /* define device I2C address: 0x76 or 0x77 (0x77 is library default address) */
#define ALTITUDE 85.0 /* height abover sea level for pressure calculation */
/* defines */
#define MAXTRIES 20 /* maximum connection attempts for wifi and mqtt */
/* function declaration (prototype) */
bool setup_bme680(); /* setup bme680 sensor */
bool setup_levelsensor(); /* setup water level sensor */
bool getdatafromsensors(char message[RH_RF95_MAX_MESSAGE_LEN - 1]); /* grab data from the sensors */
/* variables */
extern Adafruit_BME680 bme; /* bmp280 object using I2C - declared in sensors.c */
#endif /* SENSORS_H */
main.cpp
/***************************************************************************
* This is a program to send temperature, humidity and pressure data
* from an ESP32-S
* based on rf95_reliable_datagram_client.pde which uses a simple addressed,
* reliable messaging client with the RHReliableDatagram class, using the
* RH_RF95 driver to control a RF95 radio.
***************************************************************************/
/* libraries */
#include <stdint.h>
/* user defined headers */
#include "rfm95.h" /* Radiohead RFM95 functions */
#include "sensors.h" /* BME680 and water level sensor functions */
#include "esp32sleep.h" /* ESP32 sleep functions */
#include "Arduino.h" /* Arduino library */
/* defines */
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 300 /* Time ESP32 will go to sleep (in seconds) */
#define DEBUG 1 /* set debug mode to output information to serial port */
/* Global variable declaration */
RTC_DATA_ATTR int bootCount = 0; /* The number of times that the system has rebooted - stored in writeable RTC memory */