commit 1e257669de536be1ee6fc5b91fbba5e49120f2f9 Author: Robert Marshall Date: Fri Jul 27 22:49:10 2018 +0100 Initial commit diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..0981416 --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,6 @@ +{ + "port": "COM4", + "board": "esp8266:esp8266:d1_mini", + "configuration": "CpuFrequency=80,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,UploadSpeed=921600", + "sketch": "monitor.ino" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..9483722 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,26 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "/home/rob/Arduino/libraries/Adafruit_ADS1X15", + "/home/rob/.arduino15/packages/esp8266/tools/**", + "/home/rob/.arduino15/packages/esp8266/hardware/esp8266/2.4.0/**", + "/home/rob/Apps/arduino-1.8.5/tools/**", + "/home/rob/Apps/arduino-1.8.5/hardware/arduino/avr/**" + ], + "intelliSenseMode": "clang-x64", + "compilerPath": "/usr/bin/gcc", + "cStandard": "c11", + "cppStandard": "c++17" + }, + { + "name": "Win32", + "includePath": [ + "C:\\Users\\Rob\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\**", + "C:\\Users\\Rob\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.4.0\\**" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5055ffc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,45 @@ +{ + "files.associations": { + "array": "cpp", + "hash_map": "cpp", + "initializer_list": "cpp", + "utility": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "memory": "cpp", + "new": "cpp", + "ostream": "cpp", + "numeric": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "cinttypes": "cpp", + "type_traits": "cpp", + "tuple": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/monitor.ino b/monitor.ino new file mode 100644 index 0000000..4f5a1ee --- /dev/null +++ b/monitor.ino @@ -0,0 +1,132 @@ +// 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 +#include +#include +#include +#include +#include + +#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(); +} \ No newline at end of file