Guest User

TechPosts' $10 IoT Automation

a guest
Feb 14th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. /Google Assistant Home Automation
  2. #include <ESP8266WiFi.h>π
  3. #include "Adafruit_MQTT.h"
  4. #include "Adafruit_MQTT_Client.h"
  5. #include <Adafruit_Sensor.h>
  6. #include <DHT.h>
  7. #include <DHT_U.h>
  8. #include <DNSServer.h>
  9. #include <ESP8266WebServer.h>
  10. #include <WiFiManager.h>
  11.  
  12.  
  13. // pin connected to DH22 data line
  14.  
  15.  
  16. #define Relay1 D0
  17. #define Relay2 D1
  18. #define Relay3 D2
  19. //#define DHTTYPE DHT22
  20. #define DHTPIN D4
  21. #define DHTTYPE DHT11 // DHT 11
  22.  
  23.  
  24. //WLAN Details
  25. //#define WLAN_SSID "YOURWIFINAME" // Your SSID
  26. //#define WLAN_PASS "PASSWORD" // Your password
  27.  
  28. /************************* Adafruit.io Setup *********************************/
  29.  
  30. #define AIO_SERVER "io.adafruit.com" //Adafruit Server
  31. #define AIO_SERVERPORT 1883
  32. #define AIO_USERNAME "Replace with Adafruit Username" // Username
  33. #define AIO_KEY "PasteAiokeyHere" // Auth Key
  34.  
  35. // DHT sensor
  36. DHT dht(DHTPIN, DHTTYPE);
  37.  
  38. //WIFI CLIENT
  39. WiFiClient client;
  40.  
  41. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  42.  
  43. Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");
  44. Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");
  45. Adafruit_MQTT_Subscribe Light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Relay1"); // Feeds name should be same everywhere
  46. Adafruit_MQTT_Subscribe Lamp = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay2");
  47. Adafruit_MQTT_Subscribe Fan = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay3");
  48.  
  49. void MQTT_connect();
  50.  
  51. void setup() {
  52.  
  53. // initialize dht22
  54. dht.begin();
  55.  
  56. Serial.begin(115200);
  57.  
  58.  
  59. pinMode(Relay1, OUTPUT);
  60. pinMode(Relay2, OUTPUT);
  61. pinMode(Relay3, OUTPUT);
  62.  
  63.  
  64.   //WiFiManager
  65.     //Local intialization. Once its business is done, there is no need to keep it around
  66.     WiFiManager wifiManager;
  67.     //reset saved settings
  68.     //wifiManager.resetSettings();
  69.    
  70.     //set custom ip for portal
  71.     //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  72.  
  73.     //fetches ssid and pass from eeprom and tries to connect
  74.     //if it does not connect it starts an access point with the specified name
  75.     //here  "AutoConnectAP"
  76.     //and goes into a blocking loop awaiting configuration
  77.     wifiManager.autoConnect("AutoConnectAP");
  78.     //or use this for auto generated name ESP + ChipID
  79.     //wifiManager.autoConnect();
  80.  
  81.    
  82.     //if you get here you have connected to the WiFi
  83. Serial.println("connected...yeey :)");
  84.  
  85.  
  86.  
  87. // Connect to WiFi access point.
  88. /*Serial.println(); Serial.println();
  89. Serial.print("Connecting to ");
  90. Serial.println(WLAN_SSID);
  91.  
  92. WiFi.begin(WLAN_SSID, WLAN_PASS);
  93. while (WiFi.status() != WL_CONNECTED) {
  94. delay(500);
  95. Serial.print(".");
  96. }
  97. Serial.println();
  98.  
  99. Serial.println("WiFi connected");
  100. Serial.println("IP address: ");
  101. Serial.println(WiFi.localIP());*/
  102.  
  103. mqtt.subscribe(&Light);
  104. mqtt.subscribe(&Lamp);
  105. mqtt.subscribe(&Fan);
  106.  
  107. }
  108. void temperatureAndHumidity()
  109. {
  110.  
  111. float humidity_data = (float)dht.readHumidity();
  112. float temperature_data = (float)dht.readTemperature();
  113.  
  114. // By default, the temperature report is in Celsius, for Fahrenheit uncomment
  115. // following line.
  116. // temperature_data = temperature_data*(9.0/5.0) + 32.0;
  117.  
  118. // Publish data
  119. if (! temperature.publish(temperature_data))
  120. Serial.println(F("Failed to publish temperature"));
  121. else
  122. Serial.println(F("Temperature published!"));
  123. Serial.print("celsius: ");
  124. Serial.print(temperature_data);
  125. Serial.println("∞C");
  126.  
  127.  
  128. if (! humidity.publish(humidity_data))
  129. Serial.println(F("Failed to publish humidity"));
  130. else
  131. Serial.println(F("Humidity published!"));
  132. Serial.print("Humidity: ");
  133. Serial.print(humidity_data);
  134. Serial.println("%");
  135.  
  136. // Repeat every 10 seconds
  137. delay(5000);
  138.  
  139. }
  140. void loop() {
  141.  
  142. MQTT_connect();
  143.  
  144.  
  145. Adafruit_MQTT_Subscribe *subscription;
  146. while ((subscription = mqtt.readSubscription(20000))) {
  147. if (subscription == &Light) {
  148. Serial.print(F("Got: "));
  149. Serial.println((char *)Light.lastread);
  150. int Light_State = atoi((char *)Light.lastread);
  151. digitalWrite(Relay1, Light_State);
  152.      
  153. }
  154. if (subscription == &Lamp) {
  155. Serial.print(F("Got: "));
  156. Serial.println((char *)Lamp.lastread);
  157. int Lamp_State = atoi((char *)Lamp.lastread);
  158. digitalWrite(Relay2, Lamp_State);
  159. }
  160. if (subscription == &Fan) {
  161. Serial.print(F("Got: "));
  162. Serial.println((char *)Fan.lastread);
  163. int Fan_State = atoi((char *)Fan.lastread);
  164. digitalWrite(Relay3, Fan_State);
  165. }
  166.    
  167. }
  168.  
  169. }
  170.  
  171. void MQTT_connect() {
  172. int8_t ret;
  173.  
  174. if (mqtt.connected()) {
  175. return;
  176. }
  177.  
  178. Serial.print("Connecting to MQTT... ");
  179.  
  180. uint8_t retries = 3;
  181.  
  182. while ((ret = mqtt.connect()) != 0) {
  183. Serial.println(mqtt.connectErrorString(ret));
  184. Serial.println("Retrying MQTT connection in 2 seconds...");
  185. mqtt.disconnect();
  186. delay(2000);
  187. retries--;
  188. if (retries == 0) {
  189. while (1);
  190. }
  191. }
  192. Serial.println("MQTT Connected!");
  193.  
  194. }
Add Comment
Please, Sign In to add comment