Last active
August 17, 2018 09:58
-
-
Save li2hub/2479839d29514560d095d41b0b918b86 to your computer and use it in GitHub Desktop.
This file contains 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
/******************************************************************************************************** | |
We'll be using an MQTT client software to receive and transmit data to our NodeMCU device | |
We have used MQTTBox to do the same here, please feel free to use any other software of your choice | |
Download MQTTBox from the following link: workswithweb.com/html/mqttbox/installing_apps.html | |
Client settings used:- | |
Client_name: ESP_TEST | |
MQTT Client ID: randomly generated, no need to change | |
Protocol: mqtt/tcp | |
Host: broker.mqtt-dashboard.com | |
Rest are default settings, do not change | |
*******************************************************************************************************/ | |
#include <DHT.h> //https://github.com/adafruit/DHT-sensor-library, requires Adafruit Unified Sensor Library installed | |
#define DHTTYPE DHT11 | |
#define DHTPIN 16 | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> //Install this library from Sketch-->Include Library-->Manage Libraries...-->PubSubClient by Nick O'Leary | |
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 | |
const char* ssid = "Your SSID"; //Input your network ID and password(if any) | |
const char* password = "Your Password"; | |
const char* mqtt_server = "broker.mqtt-dashboard.com"; //Input the MQTT server you will be using here | |
WiFiClient espClient; //Declaring a Wifi client object | |
PubSubClient client(espClient); //Declare espClient as 'client' | |
long lastMsg = 0; //Variables used | |
char detail[100]; //Setting maximum length of message as 101 characters | |
int value = 0; | |
float humidity, temp_c; // Values read from sensor | |
void gettemperature() { | |
// Reading temperature for humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) | |
humidity = dht.readHumidity(); // Read humidity (percent) | |
temp_c = dht.readTemperature(false); // Read temperature as Celsius | |
snprintf(detail, 100, "Temperature is %3.1f and Humidity is %3.1f %%", temp_c, humidity); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(humidity) || isnan(temp_c)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
client.publish("MESSAGE_OUT", "Failed to read from DHT sensor!"); | |
} | |
else { | |
client.publish("MESSAGE_OUT", detail); //Publish to MQTT client | |
Serial.println(detail); | |
} | |
return; | |
} | |
void setup() { | |
pinMode(16, OUTPUT); // Initialize the 16 pin as an output | |
digitalWrite(16, HIGH); //Switch LED OFF | |
Serial.begin(115200); //Initialize serial communication rate | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(250); | |
Serial.print("."); | |
} | |
Serial.println("WiFi connected"); //Connect to your wifi network | |
client.setServer(mqtt_server, 1883); //Address of server,the port to connect to | |
client.setCallback(callback); //a pointer to a message callback function called when a message arrives for a subscription created by this client. | |
dht.begin(); | |
} | |
void callback(char* topic, byte* message_i_o, unsigned int length) { //Callback function called when a message arrives for a subscription created by this client | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)message_i_o[i]); | |
} | |
Serial.println(); | |
// Switch on the LED if a '0' was received as first character, or toggle it several times if 't' is the first character | |
if ((char)message_i_o[0] == '0') { | |
digitalWrite(16, LOW); // Turn the LED on (ACTIVE LOW LED) | |
} | |
else if ((char)message_i_o[0] == 't') { //Toggle the LED several times and switch off | |
for (int tog = 0; tog < 20; tog++) | |
{ digitalWrite(16, LOW); | |
delay(100); | |
digitalWrite(16, HIGH); | |
delay(100); | |
} | |
} | |
else { //Switch OFF the LED if the first character is neither 't' nor '0' | |
digitalWrite(16, HIGH); // Turn the LED off by making the voltage HIGH | |
} | |
} | |
void reconnect() { //In the case of a disconnect, the client attempts to reconnect to the server | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection...."); | |
if (client.connect("ESP8266Client")) { | |
Serial.println("Successfully Connected"); //The topic to publish to, the message to publish | |
client.subscribe("BUILTIN_LED"); //Subscribe to the "LED" topic | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" Trying again in 2 seconds"); | |
// Wait 2 seconds before retrying | |
delay(2000); | |
} | |
} | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); //Get currently elapsed time | |
if (now - lastMsg > 5000) { //Print the message every 5 seconds | |
lastMsg = now; | |
++value; | |
gettemperature(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment