Created
April 5, 2019 15:59
-
-
Save giantmolecules/e05358060d42cf552dd6f985e60d510f 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
// This #include statement was automatically added by the Particle IDE. | |
#include <MQTT.h> | |
#include "Adafruit_Sensor.h" | |
#include "Adafruit_BME280.h" | |
#define AIO_SERVER "io.adafruit.com" | |
#define AIO_SERVERPORT 1883 // use 8883 for SSL | |
#define AIO_USERNAME "" | |
#define AIO_KEY "" | |
#define BME_SCK D4 | |
#define BME_MISO D3 | |
#define BME_MOSI D2 | |
#define BME_CS D5 | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
Adafruit_BME280 bme; | |
int alarmInterval = 60000; | |
int pollInterval = 60000; | |
int weatherInterval = 200000; | |
double outsideTemperature = 0.0; | |
double tempC = 0.0; | |
double tempF = 0.0; | |
double pressure = 0.0; | |
double humidity = 0.0; | |
double alarmTemp = 0.0; //50F | |
int currentHour = 0; | |
int currentDay = 0; | |
int currentMonth = 0; | |
int currentYear = 0; | |
int theYear = 0; | |
bool alarm = false; | |
String data = ""; | |
Timer alarmTimer(alarmInterval, publishAlarm); | |
Timer pollTimer(pollInterval, pollSensor); | |
Timer weatherTimer(weatherInterval, getWeather); | |
MQTT client(AIO_SERVER, 1883, callback); | |
////////////////////////////////////////////////////////////////////////////// | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(F("BME280 test")); | |
Spark.subscribe("hook-response/historical_weather", gotData, MY_DEVICES); | |
//if (!bme.begin(0x76)) { | |
if (!bme.begin()) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring!"); | |
//while (1); | |
} | |
Particle.variable("ALARM", alarm); | |
Particle.variable("SETPOINT", alarmTemp); | |
Particle.variable("TEMPC", tempC); | |
Particle.variable("TEMPF", tempF); | |
Particle.variable("PRES", pressure); | |
Particle.variable("HUMID", humidity); | |
Particle.variable("TEMPOUT", outsideTemperature); | |
Particle.function("SETPOINT", setPoint); | |
getWeather(); | |
pollSensor(); | |
pollTimer.start(); | |
weatherTimer.start(); | |
client.connect("mqttclient", AIO_USERNAME, AIO_KEY); | |
} | |
void loop() { | |
Time.zone(-6); | |
currentYear = Time.year(); | |
currentMonth = Time.month(); | |
currentDay = Time.day(); | |
currentHour = Time.hour(); | |
/* | |
if (client.isConnected()){ | |
client.loop(); | |
} | |
*/ | |
} | |
void getWeather(){ | |
data = String::format("{\"year\":\"%i\",\"month\":\"%i\",\"day\":\"%i\",\"hour\":\"%i\"}",currentYear, currentMonth, currentDay, currentHour); | |
Spark.publish("historical_weather", data, PRIVATE); | |
} | |
void pollSensor(){ | |
tempC = bme.readTemperature(); | |
tempF = tempC * 1.8 + 32; | |
pressure = bme.readPressure()/ 100.0F; | |
humidity = bme.readHumidity(); | |
if(!client.isConnected()){ | |
Particle.publish("STATUS", "Trying to connect"); | |
client.connect("mqttclient", AIO_USERNAME, AIO_KEY); | |
} | |
if (client.isConnected()) { | |
Particle.publish("STATUS", "Publishing feeds"); | |
client.publish("brettbalogh/feeds/temperature", String(tempF)); | |
client.publish("brettbalogh/feeds/humidity", String(humidity)); | |
client.publish("brettbalogh/feeds/pressure", String(pressure)); | |
client.publish("brettbalogh/feeds/tempout", String(outsideTemperature)); | |
} | |
else{ | |
Particle.publish("STATUS", "Disconnected"); | |
} | |
if(tempC < alarmTemp){ | |
alarm = true; | |
Particle.publish("STATUS", "ALARM"); | |
alarmTimer.start(); | |
} | |
else{ | |
alarm = false; | |
Particle.publish("STATUS", "OK"); | |
alarmTimer.stop(); | |
} | |
printStats(); | |
} | |
void gotData(const char *name, const char *data){ | |
outsideTemperature = atof(data); | |
} | |
void printStats(){ | |
Serial.print("Alarm Setpoint = "); | |
Serial.print(alarmTemp); | |
Serial.println(" *C"); | |
Serial.print("Temperature = "); | |
Serial.print(tempC); | |
Serial.println(" *C"); | |
Serial.print("Temperature = "); | |
Serial.print(tempF); | |
Serial.println(" *F"); | |
Serial.print("Pressure = "); | |
Serial.print(pressure); | |
Serial.println(" hPa"); | |
Serial.print("Humidity = "); | |
Serial.print(humidity); | |
Serial.println(" %"); | |
Serial.print("Alarm = "); | |
Serial.print(alarm); | |
Serial.println(); | |
} | |
int setPoint(String args){ | |
alarmTemp=atof(args); | |
} | |
void publishAlarm(){ | |
Particle.publish("STATUS", "ALARM"); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Particle.publish("STATUS", "Callback called"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment