Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include "Adafruit_MQTT.h"
- #include "Adafruit_MQTT_Client.h"
- #include "DHT.h"
- #define water A0
- #define buzzer D6
- #define DHTPIN D7
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- /************************* WiFi Access Point *********************************/
- #define WLAN_SSID "network"
- #define WLAN_PASS "123456789"
- /************************* Adafruit.io Setup *********************************/
- #define AIO_SERVER "io.adafruit.com"
- #define AIO_SERVERPORT 1883 // use 8883 for SSL
- #define AIO_USERNAME "Tezkar786"
- #define AIO_KEY "aio_OkNq45ypm1PcwDoUuLAtrA4Oor7H"
- /************ Global State (you don't need to change this!) ******************/
- WiFiClient client;
- Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
- /****************************** Feeds ***************************************/
- // Setup a feed called 'photocell' for publishing.
- // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
- Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");
- Adafruit_MQTT_Publish hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/hum");
- Adafruit_MQTT_Publish wL = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/wL");
- Adafruit_MQTT_Publish inputonoff = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/of");
- // Setup a feed called 'onoff' for subscribing to changes.
- Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
- /*************************** Sketch Code ************************************/
- // Bug workaround for Arduino 1.6.6, it seems to need a function declaration
- // for some reason (only affects ESP8266, likely an arduino-builder bug).
- void MQTT_connect();
- void setup() {
- Serial.begin(115200);
- pinMode(water, INPUT);
- pinMode(buzzer, OUTPUT);
- dht.begin();
- delay(10);
- Serial.println(F("Adafruit MQTT demo"));
- // Connect to WiFi access point.
- Serial.println(); Serial.println();
- Serial.print("Connecting to ");
- Serial.println(WLAN_SSID);
- WiFi.begin(WLAN_SSID, WLAN_PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("WiFi connected");
- Serial.println("IP address: "); Serial.println(WiFi.localIP());
- // Setup MQTT subscription for onoff feed.
- mqtt.subscribe(&onoffbutton);
- }
- uint32_t x = 0;
- void loop() {
- MQTT_connect();
- Adafruit_MQTT_Subscribe *subscription;
- while ((subscription = mqtt.readSubscription(5000))) {
- if (subscription == &onoffbutton) {
- Serial.print(F("Got: "));
- Serial.println((char *)onoffbutton.lastread);
- }
- }
- // Now we can publish stuff!
- //****************************************************
- float t = dht.readTemperature();
- Serial.print(F("\nSending temp val "));
- Serial.print(t);
- Serial.print("...");
- if (! temp.publish(t)) {
- Serial.println(F("Failed"));
- } else {
- Serial.println(F("OK!"));
- }
- //****************************************************
- //****************************************************
- float h = dht.readHumidity();
- Serial.print(F("\nSending hum val "));
- Serial.print(h);
- Serial.print("...");
- if (! hum.publish(h)) {
- Serial.println(F("Failed"));
- } else {
- Serial.println(F("OK!"));
- }
- //****************************************************
- //****************************************************
- int waterLevel = analogRead(water);
- if(waterLevel<20){waterLevel=0;}
- waterLevel = map(waterLevel,0,1023,0,100);
- Serial.print(F("\nSending water val "));
- Serial.print(waterLevel);
- Serial.print("...");
- if (! wL.publish(waterLevel)) {
- Serial.println(F("Failed"));
- } else {
- Serial.println(F("OK!"));
- }
- if (waterLevel > 0 && waterLevel <= 100) {
- int onStatus = 10;
- soundAlarm();
- if (! inputonoff.publish(onStatus)) {
- Serial.println(F("Failed"));
- } else {
- Serial.println(F("OK!"));
- }
- } else {
- int onStatus = 0;
- if (! inputonoff.publish(onStatus)) {
- Serial.println(F("Failed"));
- } else {
- Serial.println(F("OK!"));
- }
- }
- //****************************************************
- delay(5000);
- }//end of Loop
- // Function to connect and reconnect as necessary to the MQTT server.
- // Should be called in the loop function and it will take care if connecting.
- void MQTT_connect() {
- int8_t ret;
- // Stop if already connected.
- if (mqtt.connected()) {
- return;
- }
- Serial.print("Connecting to MQTT... ");
- uint8_t retries = 3;
- while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
- Serial.println(mqtt.connectErrorString(ret));
- Serial.println("Retrying MQTT connection in 5 seconds...");
- mqtt.disconnect();
- delay(5000); // wait 5 seconds
- retries--;
- if (retries == 0) {
- // basically die and wait for WDT to reset me
- while (1);
- }
- }
- Serial.println("MQTT Connected!");
- }
- void soundAlarm() {
- digitalWrite(buzzer, HIGH); //Set PIN 8 feet as HIG67890]\H = 5 v
- delay(2000); //Set the delay time2000ms
- digitalWrite(buzzer, LOW); //Set PIN 8 feet for LOW = 0 v
- delay(2000); //Set the delay time2000ms
- }
Add Comment
Please, Sign In to add comment