Created
November 18, 2015 10:46
-
-
Save jeroavf/8bcf6a160979a29f0a09 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
ESP8266 + DHT + MQTT | |
- Teste inicial de conexão de um DHT22 a um ESP8266 | |
- Esta versão apenas envia dados pela porta Serial | |
Falta Corrigir | |
- connection to an MQTT server | |
- subscription to the topic "casa/escritorio/sensor" | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <DHT.h> | |
#define DHTTYPE DHT22 | |
#define DHTPIN 2 | |
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266 | |
unsigned long msgCounter = 0; // will store msgcounter | |
float humidity, temp_f; // Values read from sensor | |
String webString = ""; // String to display | |
// Generally, you should use "unsigned long" for variables that hold time | |
unsigned long previousMillis = 0; // will store last temp was read | |
const long interval = 2000; // interval at which to read sensor | |
const char *ssid = "SSID"; // cannot be longer than 32 characters! | |
const char *pass = "PASSWORD"; // | |
// Update these with values suitable for your network. | |
IPAddress server(192, 168, 25, 5); | |
WiFiClient wclient; | |
PubSubClient client(wclient, server); | |
// Callback function | |
void callback(const MQTT::Publish& pub) { | |
// In order to republish this payload, a copy must be made | |
// as the orignal payload buffer will be overwritten whilst | |
// constructing the PUBLISH packet. | |
// Copy the payload to a new message | |
//MQTT::Publish newpub("outTopic", pub.payload(), pub.payload_len()); | |
Serial.println(" Recebi topico: " + pub.topic()) ; | |
Serial.println(" REcebi payload: " + pub.payload_string()) ; | |
//client.publish(newpub); | |
} | |
void setup() { | |
// Setup console | |
Serial.begin(115200); | |
delay(10); | |
dht.begin(); // initialize temperature sensor | |
Serial.println("Inicializando ..." ); | |
} | |
void loop() { | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.println("..."); | |
WiFi.begin(ssid, pass); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) | |
return; | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
if (WiFi.status() == WL_CONNECTED) { | |
if (!client.connected()) { | |
if (client.connect("arduinoClient")) { | |
Serial.println(" Connected to broker") ; | |
delay(2000) ; | |
gettemperature() ; | |
Serial.println(" Sensor reading ok") ; | |
msgCounter += 1 ; | |
webString = "{ T:" + String((int)temp_f) + " , H:" + String((int)humidity) + ", C:" + String((int)msgCounter) + " }" ; | |
client.publish("outTopic", webString); | |
Serial.println(" Message " + String((int)msgCounter) +" sent to broker") ; | |
client.set_callback(callback); | |
client.subscribe("inTopic"); | |
//Serial.println(" New subscription made\n") ; | |
} | |
} | |
if (client.connected()) | |
//Serial.println("A") ; | |
client.loop(); | |
} | |
//Serial.println("Volta") ; | |
delay(1 * 60 * 1000) ; | |
} | |
void gettemperature() { | |
// Wait at least 2 seconds seconds between measurements. | |
// if the difference between the current time and last time you read | |
// the sensor is bigger than the interval you set, read the sensor | |
// Works better than delay for things happening elsewhere also | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
// save the last time you read the sensor | |
previousMillis = currentMillis; | |
// 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_f = dht.readTemperature(); // Read temperature as Celsius | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(humidity) || isnan(temp_f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
humidity = 999 ; | |
temp_f = 999 ; | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment