Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #include <PubSubClient.h>
- #include <ESP8266WiFi.h>
- const char* ssid = "masoko";
- const char* password = "123123123";
- char* topic = "masoko";
- char* server = "broker.hivemq.com";
- char* hellotopic = "hello_topic";
- #define DHTPIN 1 // what pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- #define REPORT_INTERVAL 130 // in sec
- void callback(char* topic, byte* payload, unsigned int length) {
- // handle message arrived
- }
- String clientName;
- DHT dht(DHTPIN, DHTTYPE, 15);
- WiFiClient wifiClient;
- PubSubClient client(server, 1883, callback, wifiClient);
- float oldH ;
- float oldT ;
- void setup() {
- Serial.begin(38400);
- Serial.println("DHTxx test!");
- delay(20);
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- clientName += "esp8266-";
- uint8_t mac[6];
- WiFi.macAddress(mac);
- clientName += macToStr(mac);
- clientName += "-";
- clientName += String(micros() & 0xff, 16);
- Serial.print("Connecting to ");
- Serial.print(server);
- Serial.print(" as ");
- Serial.println(clientName);
- if (client.connect((char*) clientName.c_str())) {
- Serial.println("Connected to MQTT broker");
- Serial.print("Topic is: ");
- Serial.println(topic);
- if (client.publish(hellotopic, "hello from ESP8266")) {
- Serial.println("Publish ok");
- }
- else {
- Serial.println("Publish failed");
- }
- }
- else {
- Serial.println("MQTT connect failed");
- Serial.println("Will reset and try again...");
- abort();
- }
- dht.begin();
- oldH = -1;
- oldT = -1;
- }
- void loop() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- float f = dht.readTemperature(true);
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- float hi = dht.computeHeatIndex(f, h);
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" *C ");
- Serial.print(f);
- Serial.print(" *F\t");
- Serial.print("Heat index: ");
- Serial.print(hi);
- Serial.println(" *F");
- String payload = "{\"Humidity\":";
- payload += h;
- payload += ",\"Temperature\":";
- payload += t;
- payload += "}";
- if (t != oldT || h != oldH )
- {
- sendTemperature(payload);
- oldT = t;
- oldH = h;
- }
- int cnt = REPORT_INTERVAL;
- while (cnt--)
- delay(1000);
- }
- void sendTemperature(String payload) {
- if (!client.connected()) {
- if (client.connect((char*) clientName.c_str())) {
- Serial.println("Connected to MQTT broker again");
- Serial.print("Topic is: ");
- Serial.println(topic);
- }
- else {
- Serial.println("MQTT connect failed");
- Serial.println("Will reset and try again...");
- abort();
- }
- }
- if (client.connected()) {
- Serial.print("Sending payload: ");
- Serial.println(payload);
- if (client.publish(topic, (char*) payload.c_str())) {
- Serial.println("Publish ok");
- }
- else {
- Serial.println("Publish failed");
- }
- }
- }
- String macToStr(const uint8_t* mac)
- {
- String result;
- for (int i = 0; i < 6; ++i) {
- result += String(mac[i], 16);
- if (i < 5)
- result += ':';
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement