DIsable sensors (for now) and add lighting

This commit is contained in:
Robert Marshall 2018-09-16 11:50:22 +01:00
parent 1e257669de
commit b134bc0334
3 changed files with 79 additions and 10 deletions

View file

@ -1,5 +1,5 @@
{
"port": "COM4",
"port": "/dev/ttyUSB1",
"board": "esp8266:esp8266:d1_mini",
"configuration": "CpuFrequency=80,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,UploadSpeed=921600",
"sketch": "monitor.ino"

View file

@ -18,7 +18,8 @@
"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\\**"
"C:\\Users\\Rob\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\2.4.0\\**",
"C:\\Users\\Rob\\Documents\\Arduino\\libraries\\**"
]
}
],

View file

@ -6,10 +6,14 @@
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <FastLED.h>
#define MQTT_SERVER "192.168.1.3"
#define SSID "GCHQ Surveillance Van"
#define PASSWORD "cocklol."
#define LIGHT_TOPIC "/home/switches/kitchen/fishtank"
#define LIGHT_PIN D2
#define LED_COUNT 24
#define PH_TOPIC "/home/sensors/fishtank/ph"
#define TEMPERATURE_TOPIC "/home/sensors/fishtank/temperature"
#define TEMPERATURE_PIN D5
@ -26,6 +30,13 @@ OneWire oneWire(TEMPERATURE_PIN);
DallasTemperature DS18B20(&oneWire);
WiFiClient wifiClient;
PubSubClient client = PubSubClient(MQTT_SERVER, 1883, mqttCallback, wifiClient);
CRGB leds[LED_COUNT];
bool lightsOn = true;
static const uint8_t sunriseLength = 1;
static const uint8_t heatIndexMax = 240;
static const float lightChangeInterval = ((float)(sunriseLength * 60) / heatIndexMax) * 1000;
static uint8_t heatIndex = 0;
float pHStep = (PH_7_VOLTAGE - PH_4_VOLTAGE) / 3;
@ -33,9 +44,12 @@ void waitForWiFi() {
if (WiFi.status() == WL_CONNECTED)
return;
Serial.println("Reconnecting WiFi...");
while (WiFi.status() != WL_CONNECTED)
{
int waitCount=0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
waitCount++;
if (waitCount>600)
ESP.restart();
}
Serial.println("Connected WiFi!");
}
@ -45,10 +59,10 @@ void waitForMQTT() {
return;
Serial.println("Reconnecting MQTT...");
while (!client.connected())
{
if (!client.connect(PH_TOPIC))
delay(50);
}
client.subscribe(LIGHT_TOPIC);
Serial.println(LIGHT_TOPIC);
Serial.println("Connected MQTT!");
}
@ -68,6 +82,11 @@ void setup() {
WiFi.begin(SSID, PASSWORD);
reconnect();
FastLED.addLeds<WS2811, LIGHT_PIN, GRB>(leds, LED_COUNT).setCorrection(TypicalSMD5050).setTemperature(Tungsten40W);
Serial.print("Light interval: ");
Serial.println(lightChangeInterval);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
@ -111,15 +130,62 @@ void publishTemperature() {
client.publish(TEMPERATURE_TOPIC, temperatureString.c_str(), true);
}
void sunrise() {
CRGB color = ColorFromPalette(HeatColors_p, heatIndex);
fill_solid(leds, LED_COUNT, color);
EVERY_N_MILLISECONDS(lightChangeInterval) {
if (heatIndex < heatIndexMax)
heatIndex++;
}
}
void sunset() {
CRGB color = ColorFromPalette(HeatColors_p, heatIndex);
fill_solid(leds, LED_COUNT, color);
EVERY_N_MILLISECONDS(lightChangeInterval) {
if (heatIndex > 0)
heatIndex--;
}
}
void doLighting() {
CRGB color = ColorFromPalette(HeatColors_p, heatIndex);
fill_solid(leds, LED_COUNT, color);
EVERY_N_MILLISECONDS(lightChangeInterval) {
if (lightsOn && heatIndex < heatIndexMax)
heatIndex++;
else if (heatIndex > 0)
heatIndex--;
Serial.print("Heat index: ");
Serial.println(heatIndex);
}
FastLED.show();
}
void publishReadings(){
EVERY_N_SECONDS(60) {
publishpH();
publishTemperature();
}
}
void loop() {
long startTick = millis();
reconnect();
client.loop();
publishpH();
publishTemperature();
doLighting();
delay(60000 - (millis() - startTick));
//publishReadings();
delay(10);
}
void mqttCallback(char *topic, byte *payload, unsigned int length) {
@ -129,4 +195,6 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
Serial.print((char)payload[i]);
}
Serial.println();
lightsOn = payload[0] == '1';
}