Created
February 13, 2017 05:58
-
-
Save cerebrate/b0a0b09d00687102a506f8134fc0bb1c to your computer and use it in GitHub Desktop.
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 <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <Adafruit_TSL2561_U.h> | |
#include <ArduinoJson.h> | |
#include <ESP8266WiFi.h> | |
#include <Ticker.h> | |
#include <PubSubClient.h> | |
// Expose Espressif SDK functionality - wrapped in ifdef so that it still | |
// compiles on other platforms | |
#ifdef ESP8266 | |
extern "C" { | |
#include "user_interface.h" | |
} | |
#endif | |
// Network -- WiFi and MQTT configuration ----------------- | |
// WiFi setup | |
const char* ssid = "Arkane Systems" ; | |
const char* password = "NotMyActualWifiPassword" ; | |
// MQTT server | |
const char* mqttserver = "ariadne.arkane-systems.lan"; | |
const char* subTopic = "environment/central/command"; | |
const char* pubStatusTopic = "environment/central/status"; | |
const char* pubDataTopic = "environment/livingroom/data"; | |
// Create ESP8266 WiFiClient class to connect to the MQTT server | |
WiFiClient wifiClient; | |
PubSubClient client (wifiClient); | |
// Pins -- pin definitions -------------------------------- | |
#define statusPin 0 | |
#define relay4pin 12 | |
#define relay3pin 13 | |
#define relay2pin 14 | |
#define relay1pin 15 | |
#define fanPin relay4pin | |
#define furnacePin relay3pin | |
#define acPin relay2pin | |
// Tickers -- periodic calls to scanning routines --------- | |
Ticker statusBlink; | |
Ticker envScan; | |
// Sensors -- environmental sensor declarations ----------- | |
Adafruit_BME280 bme; // I2C | |
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); | |
// Setup -- set up the smart thermostat ------------------- | |
void setup() | |
{ | |
// Setup the system. | |
Serial.begin (9600); | |
Serial.println ("SMARTHERMOSTAT: Setup"); | |
// Setup the LED pin. | |
pinMode (statusPin, OUTPUT); | |
// Setup the relay pins. | |
pinMode (relay4pin, OUTPUT); | |
pinMode (relay3pin, OUTPUT); | |
pinMode (relay2pin, OUTPUT); | |
pinMode (relay1pin, OUTPUT); | |
// Set up the WiFi. | |
wifi_station_set_hostname ("iot-smarthermostat"); | |
WiFi.begin (ssid, password); | |
Serial.print("Connecting to WiFi"); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.print("Connected, IP: "); | |
Serial.println(WiFi.localIP()); | |
// Setup MQTT client. | |
client.setServer (mqttserver, 1883); | |
client.setCallback (MQTT_callback); | |
// Start sensors. | |
// BME280 | |
if (!bme.begin()) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring!"); | |
while (1); | |
} | |
// TSL2561 | |
if(!tsl.begin()) | |
{ | |
Serial.println("Could not find a valid TSL2561 sensor, check wiring!"); | |
while(1); | |
} | |
configureLightSensor(); | |
// Commence status blinking. | |
statusBlink.attach (1.0, blink); | |
// Enable environmental monitor. | |
envScan.attach (10.0, environmentalScan); | |
} | |
// Monitor -- query sensors and return data -------------- | |
void environmentalScan () | |
{ | |
float temperature = bme.readTemperature(); // C | |
float pressure = bme.readPressure() / 100.0F; // hPa | |
float humidity = bme.readHumidity(); // % | |
sensors_event_t event; | |
tsl.getEvent(&event); | |
// Format as JSON. | |
// Allocate memory pool for the object tree. | |
StaticJsonBuffer<256> jsonBuffer; | |
// Create the root of the object tree. | |
JsonObject& root = jsonBuffer.createObject(); | |
// Add values to the object. | |
root["light-level"] = event.light; | |
root["temperature"] = temperature; | |
root["pressure"] = pressure; | |
root["humidity"] = humidity; | |
char buffer[256]; | |
root.printTo(buffer, sizeof(buffer)); | |
Serial.println (buffer); | |
int result = client.publish (pubDataTopic, buffer); | |
} | |
// Config -- sensor configuration ------------------------ | |
void configureLightSensor() | |
{ | |
tsl.enableAutoRange(true); | |
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); | |
// display details | |
sensor_t sensor; | |
tsl.getSensor(&sensor); | |
Serial.print ("TSL Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux"); | |
Serial.print ("TSL Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux"); | |
Serial.print ("TSL Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux"); | |
Serial.println (); | |
} | |
// LED -- LED management --------------------------------- | |
void blink() | |
{ | |
int state = digitalRead(statusPin); | |
digitalWrite(statusPin, !state); | |
} | |
// Loop -- run the MQTT loop forever --------------------- | |
void loop() | |
{ | |
// MQTT server connect and loop | |
if (!client.connected()) | |
{ | |
MQTT_connect(); | |
} | |
client.loop(); | |
} | |
// Function to connect and reconnect as necessary to the MQTT server. | |
// Should be called in the loop function and it will take care if connecting. | |
void MQTT_connect() | |
{ | |
// Loop until we're reconnected | |
while (!client.connected()) | |
{ | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("SmarThermostat")) | |
{ | |
Serial.println("connected"); | |
// ... and subscribe to topic | |
client.subscribe(subTopic); | |
} | |
else | |
{ | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
// Process incoming MQTT messages. | |
void MQTT_callback (char* topic, byte* payload, unsigned int length) | |
{ | |
Serial.print("Received MQTT topic="); | |
Serial.println(topic); | |
// check topic | |
if (strcmp (topic, subTopic) != 0) | |
return; | |
// check length | |
if (length != 1) | |
return; | |
// here | |
Serial.println("unknown command, noop"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment