Last active
April 6, 2020 17:02
-
-
Save djotaku/2c9e9d60903297b26107a59a87a0c83d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Master Bathroom environmental sensor | |
* | |
* Intended for IoT Home Automation projects involving the Master Bathroom. | |
* | |
* The current circuit: | |
* | |
* - Arduino MKR WiFi 1010 | |
* - Arudiono MKR ENV Shield attached | |
* | |
* Code licensed under GPL V3.0 | |
*/ | |
//Define some debug LEDs | |
#define GREENLED 13 | |
#define BLUELED 14 | |
// includes for WiFi | |
#include <SPI.h> | |
#include <WiFiNINA.h> | |
#include "arduino_secrets.h" | |
// include for ENV Shield | |
#include <Arduino_MKRENV.h> | |
//include for MQTT | |
#include <PubSubClientEric.h> | |
//secrets | |
char ssid[] = SECRET_SSID; // your network SSID (name) | |
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | |
//global variables | |
int status = WL_IDLE_STATUS; // the Wifi radio's status | |
IPAddress server(192,168,1,14); //for Pubsubclient | |
WiFiClient wifiClient; //for Pubsubclient | |
PubSubClient client(wifiClient); //for Pubsubclient | |
int REPORTING_DELAY = 5000; // in milliseconds | |
void setup() { | |
// Serial init code. This is for debugging and should be commented out when it's running if debugging is not taking place | |
Serial.begin(9600); | |
// make sure ENV shield will initialize | |
if (!ENV.begin()) { | |
Serial.println("Failed to initialize MKR ENV shield!"); | |
while (1); | |
} | |
//Initialize LEDs and start Low | |
pinMode(GREENLED, OUTPUT); | |
digitalWrite(GREENLED, LOW); | |
pinMode(BLUELED, OUTPUT); | |
digitalWrite(BLUELED, LOW); | |
//attempt to connect to Wifi Network | |
WifiConnect(); | |
// you're connected now, so print out the data: - this is also for debugging and should be commented out when running if not debugging | |
Serial.println("You're connected to the network"); | |
printCurrentNet(); | |
printWifiData(); | |
//mqtt | |
client.setServer(server, 1883); | |
client.setCallback(callback); | |
} | |
void loop(){ | |
// setup variables | |
float temperature = ENV.readTemperature(FAHRENHEIT); | |
float humidity = ENV.readHumidity(); | |
float pressure = ENV.readPressure(PSI); | |
float illuminance = ENV.readIlluminance(FOOTCANDLE); | |
// debugging - wifi check | |
printCurrentNet(); | |
// if the WiFi has disconnected, we need it to reconnect. | |
status = WiFi.status(); | |
if(status == WL_CONNECTED) | |
{ | |
digitalWrite(GREENLED, HIGH); //on | |
} | |
if(status != WL_CONNECTED) | |
{ | |
digitalWrite(GREENLED, LOW); // off | |
WifiConnect(); | |
} | |
// debugging - ENV board check | |
Serial.print("Temperature = "); | |
Serial.print(temperature); | |
Serial.println(" °F"); | |
Serial.print("Humidity = "); | |
Serial.print(humidity); | |
Serial.println(" %"); | |
Serial.print("Pressure = "); | |
Serial.print(pressure); | |
Serial.println(" psi"); | |
Serial.print("Illuminance = "); | |
Serial.print(illuminance); | |
Serial.println(" fc"); | |
// mqtt code | |
char temp[8]; | |
String(temperature, 2).toCharArray(temp,8); //for string method: float and decimal places | |
char hum[8]; | |
String(humidity, 2).toCharArray(hum,8); | |
char light[8]; | |
String(illuminance, 2).toCharArray(light,8); | |
if (!client.connected()) { | |
digitalWrite(BLUELED, LOW); | |
reconnect(); | |
} | |
client.publish("masterbath/temp",temp); | |
client.publish("masterbath/humidity",hum); | |
client.publish("masterbath/illumination", light); | |
delay(REPORTING_DELAY); | |
} | |
///////////////////// WIFI Functions ///////////////////// | |
void WifiConnect() | |
{ | |
//debugging | |
Serial.println("I'm in WiFiConnect"); | |
//attempt to connect to Wifi Network | |
while (status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to WPA SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network: | |
status = WiFi.begin(ssid, pass); | |
// wait 10 seconds for connection: | |
delay(10000); | |
} | |
digitalWrite(GREENLED, HIGH); | |
} | |
//////////////////// END WIFI Functions //////////////////// | |
//////////// Pubsubclient Functions /////////////// | |
void reconnect() { | |
// Try to reconnect | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("arduinoMasterBath", "masterbath/connection", 0, false, "disconnected")) { | |
Serial.println("connected"); | |
digitalWrite(BLUELED, HIGH); | |
// Once connected, publish an announcement... | |
client.publish("masterbath/connection","connected"); | |
// ... and resubscribe | |
//client.subscribe("inTopic"); | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i=0;i<length;i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
//////////// End Pubsubclient Functions /////////////// | |
///////////////////// WIFI Debugging ///////////////////// | |
///////// These are all for debugging and should be commented out if not debugging ////////// | |
void printWifiData() { | |
// print your board's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
Serial.println(ip); | |
// print your MAC address: | |
byte mac[6]; | |
WiFi.macAddress(mac); | |
Serial.print("MAC address: "); | |
printMacAddress(mac); | |
} | |
void printCurrentNet() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print the MAC address of the router you're attached to: | |
byte bssid[6]; | |
WiFi.BSSID(bssid); | |
Serial.print("BSSID: "); | |
printMacAddress(bssid); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.println(rssi); | |
// print the encryption type: | |
byte encryption = WiFi.encryptionType(); | |
Serial.print("Encryption Type:"); | |
Serial.println(encryption, HEX); | |
Serial.println(); | |
} | |
void printMacAddress(byte mac[]) { | |
for (int i = 5; i >= 0; i--) { | |
if (mac[i] < 16) { | |
Serial.print("0"); | |
} | |
Serial.print(mac[i], HEX); | |
if (i > 0) { | |
Serial.print(":"); | |
} | |
} | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment