Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sensor Data MQTT • Device to Database, NYU ITP Spring 2019
const char WIFI_SSID[] = " ";
const char WIFI_PASSWORD[] = " !";
const char MQTT_BROKER[] = "iotwork.shop";
const int MQTT_PORT = ;
const char MQTT_USER[] = " ";
const char MQTT_PASSWORD[] = " ";
const String DEVICE_ID = " ";
// Device to Database, ITP NYU Spring 2019
// IoT Workshop, Week 2, modified by Ellen Nickles
// Send temperature, humidity and color sensor data to MQTT
//
// Uses WiFi101 https://www.arduino.cc/en/Reference/WiFi101 (MKR1000)
// Uses WiFiNINA https://www.arduino.cc/en/Reference/WiFiNINA (MKR WiFi 1010)
// Arduino MQTT Client Library https://github.com/arduino-libraries/ArduinoMqttClient
// Adafruit DHT Sensor Library https://github.com/adafruit/DHT-sensor-library
// Adafruit Unified Sensor Library https://github.com/adafruit/Adafruit_Sensor
//
// Adafruit TCS34725 Sensor Library: https://github.com/adafruit/Adafruit_TCS34725
// Uses modified code from Adafruit colorview.ino example:
// https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/colorview/colorview.ino
#include <SPI.h>
#ifdef ARDUINO_SAMD_MKR1000
#include <WiFi101.h>
#define WL_NO_MODULE WL_NO_SHIELD
#else
#include <WiFiNINA.h>
#endif
#include <ArduinoMqttClient.h>
#include "config.h"
WiFiSSLClient net;
MqttClient mqtt(net);
// Temperature and Humidity Sensor
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 7
DHT dht(DHTPIN, DHTTYPE);
// RGB Color Sensor
#include <Wire.h>
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
String temperatureTopic = "itp/" + DEVICE_ID + "/temperature";
String humidityTopic = "itp/" + DEVICE_ID + "/humidity";
String hexColorTopic = "itp/" + DEVICE_ID + "/hexColor";
String redTopic = "itp/" + DEVICE_ID + "/red";
String greenTopic = "itp/" + DEVICE_ID + "/green";
String blueTopic = "itp/" + DEVICE_ID + "/blue";
// Publish every 10 seconds for the workshop. Real world apps need this data every 5 or 10 minutes.
unsigned long publishInterval = 10 * 1000;
unsigned long lastMillis = 0;
const int wifiConnectedLED = 5;
void setup() {
Serial.begin(9600);
// Uncomment next line to wait for a serial connection
// while (!Serial) { }
// initialize temperature sensor
dht.begin();
Serial.println("Color View Test!");
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
Serial.println("Connecting WiFi");
connectWiFi();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
if (!mqtt.connected()) {
connectMQTT();
}
// capturing and processing RGB Color Sensor values
uint16_t clear, red, green, blue;
tcs.setInterrupt(false); // turn on LED
delay(60); // takes 50ms to read
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true); // turn off LED
// Figure out some basic hex code for visualization <-- Adafruit's comment
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
// individual RGB values
byte thisRed = int(r);
byte thisGreen = int(g);
byte thisBlue = int(b);
// converting RGB into HEX
String stringRed = String(thisRed, HEX);
String stringGreen = String(thisGreen, HEX);
String stringBlue = String(thisBlue, HEX);
String hexColor = "#" + stringRed + stringGreen + stringBlue;
// poll for new MQTT messages and send keep alives
mqtt.poll();
if (millis() - lastMillis > publishInterval) {
lastMillis = millis();
float temperature = dht.readTemperature(true);
float humidity = dht.readHumidity();
Serial.print(temperature);
Serial.print("°F\t");
Serial.print(humidity);
Serial.print("% RH\t");
Serial.print(hexColor);
Serial.print(" hexColor\t");
Serial.print(thisRed);
Serial.print(" R\t");
Serial.print(thisGreen);
Serial.print(" G\t");
Serial.print(thisBlue);
Serial.println(" B");
mqtt.beginMessage(temperatureTopic);
mqtt.print(temperature);
mqtt.endMessage();
mqtt.beginMessage(humidityTopic);
mqtt.print(humidity);
mqtt.endMessage();
mqtt.beginMessage(redTopic);
mqtt.print(thisRed);
mqtt.endMessage();
mqtt.beginMessage(greenTopic);
mqtt.print(thisGreen);
mqtt.endMessage();
mqtt.beginMessage(blueTopic);
mqtt.print(thisBlue);
mqtt.endMessage();
mqtt.beginMessage(hexColorTopic);
mqtt.print(hexColor);
mqtt.endMessage();
}
}
void connectWiFi() {
// Check for the WiFi module
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print("WiFi firmware version ");
Serial.println(WiFi.firmwareVersion());
Serial.print("Attempting to connect to SSID: ");
Serial.print(WIFI_SSID);
Serial.print(" ");
while (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(3000);
}
Serial.println("Connected to WiFi");
printWiFiStatus();
}
void connectMQTT() {
Serial.print("Connecting MQTT...");
mqtt.setId(DEVICE_ID);
mqtt.setUsernamePassword(MQTT_USER, MQTT_PASSWORD);
while (!mqtt.connect(MQTT_BROKER, MQTT_PORT)) {
Serial.print(".");
delay(5000);
}
Serial.println("connected.");
}
void printWiFiStatus() {
// print your WiFi IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
digitalWrite(wifiConnectedLED, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment