├── ESP8266_PIR_Motion_Sensor_only_Youtube.ino └── README.md /ESP8266_PIR_Motion_Sensor_only_Youtube.ino: -------------------------------------------------------------------------------- 1 | 2 | //********************* Adafruit MQTT Library ******************************** 3 | 4 | #include "Adafruit_MQTT.h" // Adafruit MQTT library 5 | #include "Adafruit_MQTT_Client.h" // Adafruit MQTT library 6 | //***************************************************************************** 7 | 8 | #include "ESP8266WiFi.h" // ESP8266 library 9 | #include // Software Serial Library so we can use Pins for communication with the GPS module 10 | 11 | //*********************** PIR Motion Sensor Setup ***************************** 12 | 13 | int ledPin = D5; // choose the pin for the LED (Pin D5, GPIO 14) I tried using pin D4 but had problems for some reason 14 | int inputPin = D6; // choose the input pin (for PIR sensor) (Pin D6, GPIO 12) 15 | int pirState = LOW; // we start, assuming no motion detected 16 | int val = 0; // variable for reading the pin status 17 | 18 | //************************* WiFi Access Point ********************************* 19 | 20 | #define WLAN_SSID "**********" // Enter Your router SSID 21 | #define WLAN_PASS "**********" // Enter Your router Password 22 | 23 | //************************* Adafruit.io Setup ********************************* 24 | 25 | #define AIO_SERVER "io.adafruit.com" 26 | #define AIO_SERVERPORT 1883 // use 8883 for SSL 27 | #define AIO_USERNAME "************" // Enter Your Adafruit IO Username 28 | #define AIO_KEY "************" // Enter Your Adafruit IO Key 29 | 30 | //************ Global State (you don't need to change this!) ****************** 31 | 32 | WiFiClient client; // Create an ESP8266 WiFiClient class to connect to the MQTT server. 33 | 34 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; // Store the MQTT server, username, and password in flash memory. 35 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 36 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 37 | 38 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 39 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); 40 | 41 | //****************************** Feeds *************************************** 42 | 43 | // Setup a feed called 'motion' for publishing. 44 | // Notice MQTT paths for AIO follow the form: /feeds/ 45 | const char motion_FEED[] PROGMEM = AIO_USERNAME "/feeds/motion"; 46 | Adafruit_MQTT_Publish motion = Adafruit_MQTT_Publish(&mqtt, motion_FEED); 47 | 48 | //************************** Setup ******************************************* 49 | 50 | void setup() 51 | { 52 | Serial.begin(115200); // Serial Baud Rate 53 | pinMode(ledPin, OUTPUT); // declare LED for motion sensor as output 54 | pinMode(inputPin, INPUT); // declare Motion sensor pin as input 55 | 56 | WiFi.mode(WIFI_STA); // Setup ESP8266 as a wifi station 57 | WiFi.disconnect(); // Disconnect if needed 58 | delay(100); // short delay 59 | 60 | WiFi.begin(WLAN_SSID, WLAN_PASS); 61 | while (WiFi.status() != WL_CONNECTED) 62 | { 63 | delay(500); 64 | Serial.print("."); 65 | } 66 | } 67 | //************************ Main Loop ******************************************* 68 | void loop() 69 | { 70 | connectWifi(); 71 | MQTT_connect(); // Connect to Adafruit IO MQTT 72 | 73 | val = digitalRead(inputPin); // read input value of PIR motion sensor 74 | if (val == HIGH) // check if the input is HIGH 75 | { 76 | digitalWrite(ledPin, HIGH); // Turn the LED ON 77 | 78 | if (pirState == LOW) 79 | { 80 | Serial.println("Motion detected!"); // we have just turned on 81 | pirState = HIGH; // We only want to print on the output change, not state 82 | if (! motion.publish(val)) // Publish to Adafruit the PIR sensor value '1' 83 | { 84 | Serial.println(F("Failed")); // If it failed to publish, print Failed 85 | } else 86 | { 87 | Serial.println(F("Data Sent!")); // If data successfully published 88 | } 89 | } 90 | } else 91 | { 92 | digitalWrite(ledPin, LOW); // Turn LED OFF 93 | if (pirState == HIGH) // we have just turned off 94 | { 95 | Serial.println("Motion ended!"); // We only want to print on the output change, not state 96 | pirState = LOW; 97 | motion.publish(val); // Publish to Adafruit the PIR sensor value '0' 98 | } 99 | } 100 | 101 | if(! mqtt.ping()) {mqtt.disconnect();} // Ping Adafruit.IO to keep the MQTT connection alive 102 | 103 | } 104 | //********************** WiFi Coonect ************************************* 105 | void connectWifi() 106 | { 107 | if (WiFi.status() == WL_CONNECTED){ return; } // If already connected to WiFi, return to loop 108 | WiFi.begin(WLAN_SSID, WLAN_PASS); // Start a WiFi connection and enter SSID and Password 109 | while (WiFi.status() != WL_CONNECTED) { // While waiting on connection to start display "..." 110 | delay(500); 111 | Serial.print("."); 112 | } 113 | Serial.println("Connected"); 114 | MQTT_connect(); // Run Procedure to connect to Adafruit IO MQTT 115 | } 116 | 117 | // ******************* MQTT Connect - Adafruit IO ************************** 118 | void MQTT_connect() 119 | { 120 | int8_t ret; 121 | if (mqtt.connected()) { return; } // Stop if already connected to Adafruit 122 | Serial.println("Connecting to MQTT... "); 123 | 124 | uint8_t retries = 3; 125 | while ((ret = mqtt.connect()) != 0) { // Connect to Adafruit, will return 0 if connected 126 | Serial.println(mqtt.connectErrorString(ret)); 127 | Serial.println("Retrying MQTT..."); 128 | mqtt.disconnect(); 129 | delay(5000); // wait 5 seconds 130 | retries--; 131 | if (retries == 0) { // basically die and wait for WatchDogTimer to reset me 132 | while (1); 133 | } 134 | } 135 | Serial.println("MQTT Connected!"); 136 | } 137 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266_PIR_Sensor 2 | Hook-up an ESP8266 NodeMCU ver 1.0 to a PIR motion sensor and send data to Adafruit and IFTTT 3 | --------------------------------------------------------------------------------