Guest User

Untitled

a guest
Jan 11th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <DHT.h>
  3. #include <PubSubClient.h>
  4.  
  5. #define DHTTYPE DHT11
  6. #define DHTPIN 2
  7. #define GASPIN A0
  8. #define BUZZERPIN 3
  9. #define mqtt_server "broker.mqttdashboard.com"
  10. #define mqtt_user "karlo"
  11. #define mqtt_password "123456789"
  12.  
  13. #define humidity_topic "sensor/humidity"
  14. #define temperature_topic "sensor/temperature"
  15.  
  16.  
  17. const char* ssid = "HUAWEI_P9lite_A4A2";
  18. const char* password = "123456789";
  19. float humidity, temp_f;
  20. int smokeLevel;
  21. unsigned long previousMillis = 0;
  22. const long interval = 2000;
  23. const int smoke = 500;
  24.  
  25. DHT dht(DHTPIN, DHTTYPE, 11);
  26. WiFiClient espClient;
  27. PubSubClient mqtt_client(espClient);
  28.  
  29. void setup() {
  30. Serial.begin(115200);
  31. dht.begin();
  32.  
  33. setup_wifi();
  34. mqtt_client.setServer(mqtt_server, 1883);
  35.  
  36. pinMode(GASPIN, INPUT);
  37. pinMode(BUZZERPIN, OUTPUT);
  38. }
  39.  
  40. void loop() {
  41. if (!mqtt_client.connected()) {
  42. reconnect();
  43. }
  44. mqtt_client.loop();
  45.  
  46. getTemperature();
  47. mqtt_client.publish(temperature_topic, String(temp_f).c_str(), true);
  48. mqtt_client.publish(humidity_topic, String(humidity).c_str(),true);
  49.  
  50. }
  51.  
  52. void readGasLevel(){
  53. smokeLevel = analogRead(GASPIN);
  54.  
  55. Serial.print("Pin A0: ");
  56. Serial.println(smokeLevel);
  57.  
  58. if(smokeLevel > smoke){
  59.  
  60. }
  61.  
  62. }
  63.  
  64. void getTemperature() {
  65. unsigned long currentMillis = millis();
  66.  
  67. if(currentMillis - previousMillis >= interval) {
  68. // save the last time you read the sensor
  69. previousMillis = currentMillis;
  70. humidity = dht.readHumidity(); // Read humidity (percent)
  71. temp_f = dht.readTemperature(false); // Read temperature as Fahrenheit
  72. if (isnan(humidity) || isnan(temp_f)) {
  73. Serial.println("Failed to read from DHT sensor!");
  74. return;
  75. }
  76. }
  77. }
  78.  
  79. void setup_wifi() {
  80. delay(10);
  81. // We start by connecting to a WiFi network
  82. Serial.println();
  83. Serial.print("Connecting to ");
  84. Serial.println(ssid);
  85.  
  86. WiFi.begin(ssid, password);
  87.  
  88. while (WiFi.status() != WL_CONNECTED) {
  89. delay(500);
  90. Serial.print(".");
  91. }
  92.  
  93. Serial.println("");
  94. Serial.println("WiFi connected");
  95. Serial.println("IP address: ");
  96. Serial.println(WiFi.localIP());
  97. }
  98.  
  99. void reconnect() {
  100. // Loop until we're reconnected
  101. while (!mqtt_client.connected()) {
  102. Serial.print("Attempting MQTT connection...");
  103. // Attempt to connect
  104. // If you do not want to use a username and password, change next line to
  105. if (mqtt_client.connect("clientId-jXYJCabq7T")) {
  106. //if (mqtt_client.connect("clientId-eVYR0oBFnB", mqtt_user, mqtt_password)) {
  107. Serial.println("connected");
  108. } else {
  109. Serial.print("failed, rc=");
  110. Serial.print(mqtt_client.state());
  111. Serial.println(" try again in 5 seconds");
  112. // Wait 5 seconds before retrying
  113. delay(5000);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment