Created
July 26, 2022 01:23
-
-
Save nkgilley/4b768638b06c4904fc86d1f1f05be240 to your computer and use it in GitHub Desktop.
Custom dfrobot ph sensor with ads1115 for esphome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "esphome.h" | |
#include "DFRobot_ESP_PH_WITH_ADC.h" | |
#include "EEPROM.h" | |
// ADS 1115 | |
#include "Adafruit_ADS1X15.h" | |
// DALLAS temperature sensor | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
// Data wire is plugged into port 2 on the Arduino | |
#define ONE_WIRE_BUS 2 | |
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) | |
OneWire oneWire(ONE_WIRE_BUS); | |
// Pass our oneWire reference to Dallas Temperature. | |
DallasTemperature sensors(&oneWire); | |
float voltage, phValue, temperature = 25; | |
float readTemperature() | |
{ | |
//add your code here to get the temperature from your temperature sensor | |
sensors.requestTemperatures(); | |
return sensors.getTempCByIndex(0); | |
} | |
class CustomDFRobotPhSensor : public PollingComponent, public Sensor, public CustomAPIDevice { | |
public: | |
DFRobot_ESP_PH_WITH_ADC ph; | |
Adafruit_ADS1115 ads; | |
Sensor *ph_sensor = new Sensor(); | |
Sensor *temperature_sensor = new Sensor(); | |
Sensor *voltage_sensor = new Sensor(); | |
// constructor | |
CustomDFRobotPhSensor(uint32_t update_interval) : PollingComponent(update_interval) {} | |
float get_setup_priority() const override { return esphome::setup_priority::IO; } | |
void setup() override { | |
// This will be called by App.setup() | |
EEPROM.begin(32);//needed EEPROM.begin to store calibration k in eeprom | |
ph.begin(); | |
sensors.begin(); | |
ads.setGain(GAIN_ONE); | |
ads.begin(); | |
// services | |
register_service(&CustomDFRobotPhSensor::enter_ph_calibration_mode, "enter_ph"); | |
register_service(&CustomDFRobotPhSensor::exit_ph_calibration_mode, "exit_ph"); | |
register_service(&CustomDFRobotPhSensor::calibrate_ph, "calibrate_ph"); | |
} | |
void update() override { | |
// This will be called every "update_interval" milliseconds. | |
voltage = ads.readADC_SingleEnded(0) / 10; // read the voltage | |
voltage_sensor->publish_state(voltage); | |
temperature = readTemperature(); | |
temperature_sensor->publish_state(temperature); | |
phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation | |
ph_sensor->publish_state(phValue); | |
} | |
void enter_ph_calibration_mode() { | |
voltage = ads.readADC_SingleEnded(0) / 10; | |
temperature = readTemperature(); | |
char ENTERPH[8] = "ENTERPH"; | |
ph.calibration(voltage, temperature, ENTERPH); | |
} | |
void exit_ph_calibration_mode() { | |
voltage = ads.readADC_SingleEnded(0) / 10; | |
temperature = readTemperature(); | |
char EXITPH[7] = "EXITPH"; | |
ph.calibration(voltage, temperature, EXITPH); | |
} | |
void calibrate_ph() { | |
voltage = ads.readADC_SingleEnded(0) / 10; | |
temperature = readTemperature(); | |
char CALPH[6] = "CALPH"; | |
ph.calibration(voltage, temperature, CALPH); | |
} | |
}; |
No, I do not
…On Fri, Nov 17, 2023 at 12:54 PM jdrobb ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
you don't happen to have same code to add the EC sensor they have do you?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/nkgilley/4b768638b06c4904fc86d1f1f05be240#gistcomment-4764097>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALQBKSC3XNT3C4LSFCR6ATYE6QEHBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNZVGI4TOMBYU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you don't happen to have same code to add the EC sensor they have do you?