Last active
November 13, 2019 01:24
-
-
Save dgomes/8a0b16283ee61e91c4548ea2b8212c1e to your computer and use it in GitHub Desktop.
Monitoring Hot Sanitary Water Storage with an Arduino Yun
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 <Arduino.h> | |
#define LCD | |
#define ONEWIRE_PIN 2 | |
#define PUMP_PIN 3 | |
#ifdef CONSOLE | |
#include <Console.h> | |
#endif | |
#ifdef LCD | |
#include <U8x8lib.h> | |
#endif | |
#include <DallasTemperature.h> | |
#include <OneWire.h> | |
#include <BridgeClient.h> | |
#include <homie.h> | |
OneWire ds(ONEWIRE_PIN); | |
DallasTemperature sensors(&ds); | |
#ifdef LCD | |
#ifdef U8X8_HAVE_HW_SPI | |
#include <SPI.h> | |
#endif | |
#endif | |
/* Temperature Sensors */ | |
#define MAX_SENSORS 4 | |
DeviceAddress addr[MAX_SENSORS]; | |
const char *label[] = {"AQS", "PRH", "?", "?"}; | |
static float t_cur[MAX_SENSORS] = {0.0, 0.0, 0.0, 0.0}; | |
static float t_prev[MAX_SENSORS] = {1.0, 1.0, 1.0, 1.0}; | |
/* LCD display */ | |
#ifdef LCD | |
#define LCD_LIGHT 13 //pin 13 HIGH/1->light off LOW/0->light on | |
int lightState = 0; | |
U8X8_PCD8544_84X48_4W_SW_SPI u8x8(/* clock=*/ 12, /* data=*/ 11, /* cs=*/ 9, /* dc=*/ 10, /* reset=*/ 8); // Nokia 5110 Display | |
unsigned long int time_int = 0; | |
#endif | |
/* MQTT */ | |
IPAddress server(192,168,1,100); | |
String myip = "192.168.2.42"; | |
boolean pumping = false; | |
boolean prev_pumping = !pumping; | |
int server_port = 1883; | |
BridgeClient ethClient; | |
PubSubClient mqttClient(ethClient); | |
const char *nodes[] = {"cmd"}; | |
Homie homie(mqttClient, String("heating"), nodes, 1); | |
void setup_sensors() { | |
for (int i=0; i<MAX_SENSORS; i++) { | |
t_prev[i] = NULL; | |
if (!sensors.getAddress(addr[i], i)) { | |
homie.publish_property(String(F("$debug")), String(F("Unable to find address for "))+String(i)); | |
} | |
} | |
#ifdef RESET | |
u8x8.setPowerSave(0); | |
u8x8.setFont(u8x8_font_5x7_f); | |
u8x8.drawString(0,1,"Setup..."); | |
#endif | |
} | |
void mqtt_callback(char* topic, byte* payload, unsigned int length) { | |
char c_payload[length+1]; | |
memcpy(c_payload, payload, length); | |
c_payload[length] = '\0'; | |
if(strncmp(c_payload, "reset",5)==0) { | |
homie.publish_property(String("$debug"), "setup sensors"); | |
setup_sensors(); | |
} | |
#ifdef LCD | |
if(strncmp(c_payload, "light",5)==0) { | |
homie.publish_property(String("$debug"), "switch light"); | |
lightState=!lightState; | |
digitalWrite(LCD_LIGHT, lightState); | |
} | |
#endif | |
} | |
void pump() { | |
pumping = digitalRead(PUMP_PIN); | |
} | |
void setup(void) | |
{ | |
pinMode(PUMP_PIN, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(PUMP_PIN), pump, CHANGE); | |
pinMode(ONEWIRE_PIN, INPUT); | |
digitalWrite(ONEWIRE_PIN, HIGH); | |
#ifdef LCD | |
pinMode(LCD_LIGHT,OUTPUT); | |
digitalWrite(LCD_LIGHT, lightState); | |
u8x8.begin(); | |
u8x8.setPowerSave(0); | |
u8x8.setFont(u8x8_font_5x7_f); | |
u8x8.drawString(0,1,"Booting..."); | |
#endif | |
Bridge.begin(); | |
#ifdef CONSOLE | |
Console.begin(); | |
while (!Console){ | |
; // wait for Console port to connect. | |
} | |
Console.println(F("HeatingMonitorYun v0")); | |
#endif | |
mqttClient.setServer(server, server_port); | |
homie.setBrand(F("Arduino Yun")); | |
homie.setFirmware(F("dgomes"), F("0.1.1")); | |
homie.setup(myip , mqtt_callback); | |
sensors.begin(); | |
sensors.setResolution(12); //12bits temperatures | |
setup_sensors(); | |
#ifdef LCD | |
u8x8.clear(); | |
lightState=!lightState; | |
digitalWrite(LCD_LIGHT, lightState); | |
#endif | |
} | |
void loop(void) | |
{ | |
homie.loop(); | |
sensors.requestTemperatures(); // Send the command to get temperatures | |
for(int i=0; i<sensors.getDeviceCount(); i++) { | |
t_cur[i] = (int) ((sensors.getTempC(addr[i])*4.0)+0.5)/4.0; | |
if (t_prev[i] == NULL || | |
(abs(t_cur[i]-t_prev[i]) > 0.25 && abs(t_cur[i]-t_prev[i]) < 10.0)) { | |
homie.publish_property(String(label[i]), String(t_cur[i])); | |
#ifdef LCD | |
u8x8.drawString(0,i+2, label[i]); | |
u8x8.drawString(5,i+2,String(t_cur[i]).c_str()); | |
#endif | |
t_prev[i] = t_cur[i]; | |
} | |
} | |
#ifdef LCD | |
if(millis()/10000 != time_int) { | |
u8x8.setFont(u8x8_font_5x7_f); | |
u8x8.drawString(0,0,"PUMP "); | |
time_int = millis()/10000; | |
} | |
#endif | |
if(pumping != prev_pumping) { | |
#ifdef LCD | |
u8x8.drawString(5,0,pumping ? "ON " : "OFF"); | |
#endif | |
homie.publish_property(String("pump"), pumping ? "ON" : "OFF"); | |
prev_pumping = pumping; | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hardware: