You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
void setup() {
// start the serial connection
Serial.begin(9600);
// wait for serial monitor to open
while(! Serial);
// initialize dht22
dht.begin();
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run(); if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
sensors_event_t event;
dht.temperature().getEvent(&event);
//We have to define on which pin the PowerSwitch Tail is connected to:
const int lamp_pin = 5;
//We also define a new feed for the project, simply called lamp:
const char LAMP_FEED[] PROGMEM = IO_USERNAME "/feeds/lamp";
//In the setup() function, we set the pin of the PowerSwitch Tail to an output:
pinMode(lamp_pin, OUTPUT);
//We also make the project subscribe to the lamp feed: mqtt.subscribe(&lamp);
//Then, in the loop() function, we have to listen to incoming messages from Adafruit IO, to know if we need to turn the lamp on or off. It starts by creating a subscription instance:
Adafruit_MQTT_Subscribe *subscription;
//Then, we listen for incoming messages. If the message is 'ON', we switch the lamp on. And if it's 'OFF', we simply switch the lamp off again. This is done by the following piece of code:
while (subscription = MQTT.readSubscription(1000)) {
// we only care about the lamp events
if (subscription == &lamp) {
// convert mqtt ascii payload to int
char *value = (char *)lamp.lastread;
Serial.print(F("Received: "));
Serial.println(value);
// Apply message to lamp
String message = String(value);
message.trim();
if (message == "ON") {digitalWrite(lamp_pin, HIGH);}
if (message == "OFF") {digitalWrite(lamp_pin, LOW);}
}
}
}
The text was updated successfully, but these errors were encountered:
why am i getting his error?
"mqtt" was not declared in this scope
#include "config.h"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#include <Adafruit_Sensor.h>
#include "DHT.h"
#include "DHT_U.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <PubSubClient.h>
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = IO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = IO_KEY;
// pin connected to DH22 data line
#define DATA_PIN 13
// create DHT22 instance
DHT_Unified dht(DATA_PIN, DHT22);
// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
void setup() {
// start the serial connection
Serial.begin(9600);
// wait for serial monitor to open
while(! Serial);
// initialize dht22
dht.begin();
// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
// we are connected
Serial.println();
Serial.println(io.statusText());
}
void loop() {
// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
sensors_event_t event;
dht.temperature().getEvent(&event);
float celsius = event.temperature;
float fahrenheit = (celsius * 1.8) + 32;
Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");
Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");
// save fahrenheit (or celsius) to Adafruit IO
temperature->save(fahrenheit);
dht.humidity().getEvent(&event);
Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
// save humidity to Adafruit IO
humidity->save(event.relative_humidity);
// wait 5 seconds (5000 milliseconds == 5 seconds)
delay(1000);
//We have to define on which pin the PowerSwitch Tail is connected to:
const int lamp_pin = 5;
//We also define a new feed for the project, simply called lamp:
const char LAMP_FEED[] PROGMEM = IO_USERNAME "/feeds/lamp";
//In the setup() function, we set the pin of the PowerSwitch Tail to an output:
pinMode(lamp_pin, OUTPUT);
//We also make the project subscribe to the lamp feed:
mqtt.subscribe(&lamp);
//Then, in the loop() function, we have to listen to incoming messages from Adafruit IO, to know if we need to turn the lamp on or off. It starts by creating a subscription instance:
Adafruit_MQTT_Subscribe *subscription;
//Then, we listen for incoming messages. If the message is 'ON', we switch the lamp on. And if it's 'OFF', we simply switch the lamp off again. This is done by the following piece of code:
while (subscription = MQTT.readSubscription(1000)) {
// we only care about the lamp events
if (subscription == &lamp) {
}
}
}
The text was updated successfully, but these errors were encountered: