132 lines
No EOL
2.9 KiB
C++
132 lines
No EOL
2.9 KiB
C++
// see here: https://forum.arduino.cc/index.php?topic=336012.msg2643184#msg2643184 and https://scidle.com/how-to-use-a-ph-sensor-with-arduino/
|
|
|
|
#include <Wire.h>
|
|
#include <Adafruit_ADS1015.h>
|
|
#include <PubSubClient.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
#define MQTT_SERVER "192.168.1.3"
|
|
#define SSID "GCHQ Surveillance Van"
|
|
#define PASSWORD "cocklol."
|
|
#define PH_TOPIC "/home/sensors/fishtank/ph"
|
|
#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature"
|
|
#define TEMPERATURE_PIN D5
|
|
|
|
#define PH_7_VOLTAGE 2.5
|
|
#define PH_4_VOLTAGE 3.04
|
|
|
|
#define VOLTAGE_OFFSET 0.03
|
|
|
|
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
|
|
|
Adafruit_ADS1115 ads;
|
|
OneWire oneWire(TEMPERATURE_PIN);
|
|
DallasTemperature DS18B20(&oneWire);
|
|
WiFiClient wifiClient;
|
|
PubSubClient client = PubSubClient(MQTT_SERVER, 1883, mqttCallback, wifiClient);
|
|
|
|
float pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
|
|
|
|
void waitForWiFi() {
|
|
if (WiFi.status() == WL_CONNECTED)
|
|
return;
|
|
Serial.println("Reconnecting WiFi...");
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
delay(100);
|
|
}
|
|
Serial.println("Connected WiFi!");
|
|
}
|
|
|
|
void waitForMQTT() {
|
|
if (client.connected())
|
|
return;
|
|
Serial.println("Reconnecting MQTT...");
|
|
while (!client.connected())
|
|
{
|
|
if (!client.connect(PH_TOPIC))
|
|
delay(50);
|
|
}
|
|
Serial.println("Connected MQTT!");
|
|
}
|
|
|
|
void reconnect() {
|
|
waitForWiFi();
|
|
waitForMQTT();
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
ads.setGain(GAIN_TWOTHIRDS);
|
|
ads.begin();
|
|
|
|
pinMode(TEMPERATURE_PIN, INPUT_PULLUP);
|
|
DS18B20.begin();
|
|
|
|
WiFi.begin(SSID, PASSWORD);
|
|
reconnect();
|
|
}
|
|
|
|
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
|
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
}
|
|
|
|
float readpH() {
|
|
Serial.println("Reading pH...");
|
|
int sum = 0;
|
|
const int readCount = 10;
|
|
|
|
for (int i = 0; i < readCount; i++)
|
|
{
|
|
sum += ads.readADC_SingleEnded(0);
|
|
delay(10);
|
|
}
|
|
|
|
float averageRead = float(sum) / readCount;
|
|
Serial.print("Average value: ");
|
|
Serial.println(averageRead);
|
|
|
|
float voltage = 6.144 / 32768.0 * averageRead;
|
|
voltage -= VOLTAGE_OFFSET;
|
|
Serial.println(voltage);
|
|
float pH = 7 - ((PH_7_VOLTAGE - voltage) / pHStep);
|
|
return pH;
|
|
}
|
|
|
|
void publishpH() {
|
|
float pH = readpH();
|
|
String pHString(pH, 2);
|
|
Serial.println(pHString);
|
|
client.publish(PH_TOPIC, pHString.c_str(), true);
|
|
}
|
|
|
|
void publishTemperature() {
|
|
DS18B20.requestTemperatures();
|
|
float temperature = DS18B20.getTempCByIndex(0);
|
|
String temperatureString(temperature, 2);
|
|
Serial.println(temperatureString);
|
|
client.publish(TEMPERATURE_TOPIC, temperatureString.c_str(), true);
|
|
}
|
|
|
|
void loop() {
|
|
long startTick = millis();
|
|
reconnect();
|
|
client.loop();
|
|
|
|
publishpH();
|
|
publishTemperature();
|
|
|
|
delay(60000 - (millis() - startTick));
|
|
}
|
|
|
|
void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
|
Serial.print("Got payload: ");
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
Serial.print((char)payload[i]);
|
|
}
|
|
Serial.println();
|
|
} |