├── init.lua ├── README.md └── hcsr04.lua /init.lua: -------------------------------------------------------------------------------- 1 | -- My crappy open garage door notifier. 2 | -- init.lua sets up the wifi connection then runs the notify() function every 60 seconds. 3 | 4 | print("Setting up WIFI...") 5 | wifi.setmode(wifi.STATION) 6 | --modify according your wireless router settings 7 | wifi.sta.config("SSID","PASSWORD") 8 | wifi.sta.connect() 9 | tmr.alarm(1, 1000, 1, function() 10 | if wifi.sta.getip()== nil then 11 | print("IP unavaiable, Waiting...") 12 | else 13 | tmr.stop(1) 14 | print("Config done, IP is "..wifi.sta.getip()) 15 | dofile("hcsr04.lua") 16 | device = hcsr04.init() 17 | tmr.alarm(0, 60000, 1, function() notify() end) 18 | end 19 | end) 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266 + HC-SR04 + MB102 = Open Garage Door Notifier 2 | 3 | My crappy cobbling together of code for my ESP8266 module which uses a HC-SR04 ultrasonic sensor. This is a dirty hack together of code. Use as you wish. Don't cry if you think the code is shitty. 4 | 5 | Picture of my Open Garage Door Notifier - http://i.imgur.com/XUx8l6T.jpg 6 | 7 | Note that the HC-SR04 is being driven by the 5v rail and the ESP8266 is of course on 3.3v. The ECHO pin on the HC-SR04 has a 1K resistor in line to prevent the HC-SR04 from hurting the ESP8266. The HC-SR04 has no problem receiving a 3.3v signal on the TRIG pin, from the ESP8266. 8 | 9 | My ESP-01 pins are assigned to: 10 | 11 | 12 | - GPIO0 and GPIO2 are used for ECHO and TRIG on the HC-SR04. 13 | - CH_PD is connected to Vcc (3.3v). 14 | - GND is ground, duh. 15 | - VCC is 3.3v. 16 | - RST is not connected. 17 | - UTXD and URXD connected to RX and TX of your USB UART (e.g. FTDI232L) for testing only. 18 | 19 | 20 | 21 | Change the SSID and PASSWORD to the real values in init.lua. 22 | 23 | Change the GPIO pin assignments and YOUR_API_KEY for Notify My Android in hcsr04.lua. 24 | 25 | See my youtube video of an "Open Garage Door" notifier: https://www.youtube.com/watch?v=3zI72pZsJ4M 26 | 27 | 28 | -------------------------------------------------------------------------------- /hcsr04.lua: -------------------------------------------------------------------------------- 1 | 2 | n=0 3 | 4 | hcsr04 = {}; 5 | 6 | function hcsr04.init(pin_trig, pin_echo) 7 | local self = {} 8 | self.time_start = 0 9 | self.time_end = 0 10 | -- Choose the correct GPIO pins for the trigger and echo of the HC-SR04 module 11 | self.trig = pin_trig or 3 12 | self.echo = pin_echo or 4 13 | gpio.mode(self.trig, gpio.OUTPUT) 14 | gpio.mode(self.echo, gpio.INT) 15 | 16 | function self.echo_cb(level) 17 | if level == 1 then 18 | self.time_start = tmr.now() 19 | gpio.trig(self.echo, "down") 20 | else 21 | self.time_end = tmr.now() 22 | end 23 | end 24 | 25 | function self.measure() 26 | gpio.trig(self.echo, "up", self.echo_cb) 27 | gpio.write(self.trig, gpio.HIGH) 28 | tmr.delay(100) 29 | gpio.write(self.trig, gpio.LOW) 30 | tmr.delay(100000) 31 | if (self.time_end - self.time_start) < 0 then 32 | return -1 33 | end 34 | return (self.time_end - self.time_start) / 58 35 | end 36 | return self 37 | end 38 | 39 | -- Check to see if the ultrasonic sensor measures less than 50cm. If there are two measurements in a row less than 50cm then send a notification. 40 | -- This prevents false positives, as insects may set off the sensor every now and then. 41 | function notify() 42 | if device.measure() < 50 then 43 | print("Door is Open") 44 | if n = 1 then 45 | conn=net.createConnection(net.TCP, 0) 46 | conn:on("receive", function(conn, payload) print(payload) end) 47 | conn:connect(80,"50.116.34.97") 48 | conn:send("GET /publicapi/notify?apikey=YOUR_API_KEY&application=ESP8266&event=G_DOOR_OPEN&description=Garage%20door%is%20open&priority=2\r\n HTTP/1.1\r\n") 49 | conn:send("Host: notifymyandroid.com\r\n") 50 | conn:send("Accept: */*\r\n") 51 | conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") 52 | conn:send("\r\n") 53 | n = 0 54 | else n = 1 55 | end 56 | else print("Door is closed") 57 | end 58 | end 59 | --------------------------------------------------------------------------------