Skip to content

Instantly share code, notes, and snippets.

@d3lta-v
Created April 28, 2022 02:39
Show Gist options
  • Select an option

  • Save d3lta-v/359f122da4e9d9e8a18363a01067132d to your computer and use it in GitHub Desktop.

Select an option

Save d3lta-v/359f122da4e9d9e8a18363a01067132d to your computer and use it in GitHub Desktop.
Classroom temperature and humidity IoT monitoring sensor
/*
Classroom Temperature and Humidity Monitoring Program
For the SSTuino Classic
This example sketch publishes temperature and humidity data
from a DHT11 sensor to ThingSpeak every 20 seconds using MQTT.
This example code is in the public domain.
https://sstuino.fourier.industries
*/
#include "SSTuino_Companion.h"
#include <DHT.h>
#include <DHT_U.h>
#define WIFI_SSID "SSID GOES HERE"
#define WIFI_PASSWORD "WIFI PASSWORD GOES HERE"
#define TS_CLIENTID "ThingSpeak MQTT Client ID goes here"
#define TS_PASSWORD "ThingSpeak MQTT Password goes here"
#define CHANNEL_ID "ThingSpeak Channel ID goes here"
SSTuino wifi = SSTuino();
#define DHTPIN 2
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
// Start the DHT11 sensor
dht.begin();
// Open the link between the two devices
wifi.openLink();
// Reset the Wi-Fi chip to clear any previous settings
wifi.reset();
// Verify that the link is ok between the two devices
if (!wifi.smokeTest()) {
Serial.println(F("Unable to establish link with Wi-Fi chip. Halting."));
while (true){};
}
wifiConnect();
setupMQTT();
/*
Insert any additional setup code here
*/
}
void loop()
{
/*
Read DHT11 data and transmit every 20 seconds
*/
// Get temperature
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
transmitData("field1", String(event.temperature).c_str());
}
// Get humidity
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
transmitData("field2", String(event.relative_humidity).c_str());
}
delay(20000); // 20 second interval
}
void wifiConnect(void)
{
// Connects to Wifi and displays connection state
wifi.connectToWifi(F(WIFI_SSID), F(WIFI_PASSWORD));
Serial.println(F("Connecting to Wi-Fi..."));
delay(10000); // 10 seconds optimal for wifi connection to fully establish
Status wifiStatus = wifi.getWifiStatus();
if (wifiStatus != SUCCESSFUL) {
Serial.println(F("Failed to connect to Wi-Fi"));
while (true){};
} else {
Serial.println(F("Wi-Fi connected"));
}
}
void setupMQTT(void)
{
// Setup MQTT
Serial.println(F("Setting up MQTT..."));
bool mqttSuccess = wifi.enableMQTT(F("mqtt3.thingspeak.com"), true, TS_CLIENTID, TS_PASSWORD);
if (!mqttSuccess) {
Serial.println(F("Failed to enable MQTT. Halting."));
while (true){};
}
delay(10000); // Wait for MQTT to fully connect
// Check if MQTT is connected
if (!wifi.isMQTTConnected()) {
Serial.println(F("MQTT did not connect successfully!"));
while (true){};
} else {
Serial.println(F("MQTT connected!"));
}
}
void transmitData(const char *feed, const char *value)
{
String topic = "channels/" CHANNEL_ID "/publish/fields/";
topic += String(feed);
if (wifi.mqttPublish(topic, value)) {
Serial.println(F("Successfully published data!"));
} else {
Serial.println(F("Failed to publish data!"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment