Skip to content

Instantly share code, notes, and snippets.

@yildak
Last active August 29, 2015 14:18
Show Gist options
  • Save yildak/4506dfde87c82a5b149d to your computer and use it in GitHub Desktop.
Save yildak/4506dfde87c82a5b149d to your computer and use it in GitHub Desktop.
plant_pot_esp12
--plant doctor, smart pot system, esp-12 standalone wiring
--
--ds18b20 on GPIO5/pin2
--photocell on GPIO4/pin1
--soil probes on ADC/id=0
--R on GPIO13/pin7 - set low first
--G on GPIO12/pin6 - set low first
--B on GPIO14/pin5 - set low first
--
-- GPIO15 & GND on ESP to GND
-- GPIO0, GPIO2, VCC & CH_PD on ESP to 3.3V @500mA
--
--example lines
--pin = 1
--gpio.mode(pin,gpio.OUTPUT)
--gpio.write(pin,gpio.HIGH)
--gpio.mode(pin,gpio.INPUT)
--print(gpio.read(pin))
--
id = 0
water_value = adc.read(0)
--
light_sensor = 1
light_value = gpio.read(1)
--
red = 7
green = 6
blue = 5
--
gpio.mode(7,gpio.OUTPUT)
gpio.mode(6,gpio.OUTPUT)
gpio.mode(5,gpio.OUTPUT)
--
gpio.write(7,gpio.LOW)
gpio.write(6,gpio.LOW)
gpio.write(5,gpio.LOW)
--ds18b20
pin = 2
ow.setup(pin)
counter=0
lasttemp=-999
function bxor(a,b)
local r = 0
for i = 0, 31 do
if ( a % 2 + b % 2 == 1 ) then
r = r + 2^i
end
a = a / 2
b = b / 2
end
return r
end
--- Get temperature from DS18B20
function getTemp()
addr = ow.reset_search(pin)
repeat
tmr.wdclr()
if (addr ~= nil) then
crc = ow.crc8(string.sub(addr,1,7))
if (crc == addr:byte(8)) then
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE, 1)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
crc = ow.crc8(string.sub(data,1,8))
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32768) then
t = (bxor(t, 0xffff)) + 1
t = (-1) * t
end
t = t * 625
lasttemp = t
print("Last temp: " .. lasttemp)
end
tmr.wdclr()
end
end
end
addr = ow.search(pin)
until(addr == nil)
end
getTemp()
t1 = lasttemp
print("Temp:"..t1)
--
print("water:"..water_value)
--
print("light:"..light_value)
--
if light_value > 0 then
print("sun is shining")
else
print("plant sleeping")
end
--
tmr.delay(100000)
--
if water_value < 600 then
gpio.write(5,gpio.HIGH)
print("plant thirsty")
else
print("plant healthy")
gpio.write(6,gpio.HIGH)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment