Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- #include "DHT.h"
- /*****************************************************
- * LED settings
- ******************************************************/
- int LED_BUILTIN = 2;
- /*****************************************************
- * MQTT settings
- * WIFI settings
- *****************************************************/
- const char* ssid = "wifi";
- const char* password = "myPass";
- const char* mqttServer = "192.168.25.20";
- const int mqttPort = 1883;
- const char* mqttUser = "yourMQTTuser";
- const char* mqttPassword = "yourMQTTpassword";
- WiFiClient espClient;
- PubSubClient client(espClient);
- /*****************************************************
- * ESP32 DHT Reading
- * DHT Input: ==> GPIO23.
- *****************************************************/
- #define DHTPIN 33
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- float localHum = 0;
- float localTemp = 0;
- char temp[20];
- char hum[20];
- /*****************************************************
- * Global settings
- *
- *****************************************************/
- int LoopCounter = 0;
- /***************************************************
- * Get indoor Temp/Hum data
- ****************************************************/
- void getDHT()
- {
- float tempIni = localTemp;
- float humIni = localHum;
- localTemp = dht.readTemperature();
- localHum = dht.readHumidity();
- if (isnan(localHum) || isnan(localTemp)) // Check if any reads failed and exit early (to try again).
- {
- localTemp = tempIni;
- localHum = humIni;
- return;
- }
- }
- void callback(char* topic, byte* payload, unsigned int length)
- {
- Serial.print("Message arrived in topic: ");
- Serial.println(topic);
- char msg[16];
- Serial.print("Message:");
- for (int i = 0; i < length; i++)
- {
- Serial.print((char)payload[i]);
- if ((char)payload[i] != ',')
- {
- msg[i]=(char)payload[i];
- }
- else
- {
- break;
- }
- }
- Serial.println();
- Serial.println("-----------------------");
- if ((char)payload[length-1] == '1')
- {
- digitalWrite(LED_BUILTIN, HIGH);
- }
- else
- {
- digitalWrite(LED_BUILTIN, LOW);
- }
- /*
- * TODO SOlve this!!
- * char ok_msg[]="ok=";
- strcat(ok_msg,msg);
- Serial.print("Lets reply back > ");
- Serial.println(ok_msg);
- client.publish("v1/username/things/clientid/reponse/",ok_msg);
- */
- }
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.println("Connecting to WiFi..");
- }
- Serial.println("Connected to the WiFi network");
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected())
- {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
- Serial.println("connected to MQTT");
- }
- else
- {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.subscribe("v1/username/things/clientid/things/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/cmd/152");
- }
- void loop()
- {
- digitalWrite(LED_BUILTIN, LOW);
- LoopCounter++;
- client.loop();
- getDHT();
- if (LoopCounter > 10)
- {
- digitalWrite(LED_BUILTIN, HIGH);
- Serial.println("Publishing to J");
- sprintf(hum,"analog_actuator=%g",localHum);
- Serial.println(hum);
- client.publish("v1/username/things/clientid/data/151",hum);
- sprintf(temp,"analog_actuator=%g",localTemp);
- delay(1000);
- Serial.println(temp);
- client.publish("v1/username/things/clientid/64a2f670-063e-11e7-a905-f3d1e6fb8dd5/data/150",temp);
- LoopCounter = 0;
- }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement