Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt was not declared in this scope? #1

Open
ryleymcc opened this issue Jul 17, 2018 · 0 comments
Open

mqtt was not declared in this scope? #1

ryleymcc opened this issue Jul 17, 2018 · 0 comments

Comments

@ryleymcc
Copy link
Owner

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) {

// 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);}

}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant