samedi 6 août 2016

Family messenger deluxe

A few months ago, i built a messenger allowing my kids with no computer or internet connection to received messages with a cheap Esp8266+LCD project (here).

This was a nice concept, with a few limitations
   a) Ugly design
   b) Only one message possible
   c) No clear information that a message is present or not

Introducing Messenger 2.0, solving all of these limitations.



It has the same purpose than the former version (ie, displaying a message sent through a dedicated webpage), but it adds
   - a clear visual information that a message is present using leds (red when the message is from me, green when it is from my loved one, and red & green when it is from then children)
   - a button to delete the last message and display the former one - several messages with different targets can be sent
   - a simpler design (using a Wemos D1 which makes the 5V to 3.3 V regulator redudant, and getting rid of the 5-3.3 V  logic converter based on hackaday's discussion regarding ESP8266's 5V tolerance)

I also used a cardbox box (which makes cutting the opening for the LCD and gluing the button and the leds very easy). The operating mode is still the same:

i) Posting a message using a dedicated web page (it is easy to add a password, but i did not use one here)




ii) The messenger  checks the internet every 30 seconds, displays the message and flashes the led corresponding to the sender



iii) Pushing the button  deletes the current message and displays the next one (if any) or, if there is no more message, displays a "no message" indication with no light on.

Total cost is 5 euros
    - Wemos D1 (2,5 euros on alibaba)
    - A 16x2 I2C LCD module.Cost (1,5 $)
    - two leds and a button (1 $)
    - Cardboard box (old matches box, free)
  
The hardware is the same as the preceding Messenger, with a wemos D1 instead of an ESP8266, a red led (connected to pin7), a green led (connected to pin 6) and a small push button (connected to +3V and to pin 8).

The only software change is in main.lua



ADR = 0x27
_ctl = 0x08
sda=3 -- SDA pin for I2C LCD
scl=4 -- Scl pin for I2C LCD
gpledV=6 -- green led pin
gpledM=7 -- red led pin
gpbutt=8 -- Clear button pin
nmsg=1
payload=""
oldpl=""
paytab={" No new message"}
tries=100

gpio.mode(gpledM,gpio.OUTPUT)
gpio.mode(gpledV,gpio.OUTPUT)
gpio.mode(gpbutt,gpio.INPUT,gpio.FLOAT)

gpio.trig(gpbutt,"up", function()
if (nmsg>1) then
paytab[nmsg]=nil
nmsg=nmsg-1
collectgarbage()
end
dispmsg(paytab[nmsg])
end)

domsg = function()
if (tries ==0) then -- if connected to AP
dofile("checkmsg.lua")
if (payload ~= oldpl) then
nmsg=nmsg+1
paytab[nmsg]=payload
dispmsg(payload)
oldpl=payload
end
tmr.alarm(1, 20000, 1, domsg)
else -- if not, keep trying
put(locate(1,0),"Connection... ")
end
end

function dispmsg(message)
put(locate(0,0),string.sub(message,1,16))
put(locate(1,0)," ")
run(1,string.sub(message,17),600,0)
c=string.sub(message,1,1)
gpio.write(gpledM,gpio.LOW)
gpio.write(gpledV,gpio.LOW)
light(off)
tmr.delay(100000)
gpio.write(gpledM,gpio.HIGH)
light(0)
tmr.delay(100000)
gpio.write(gpledV,gpio.HIGH)
light(off)
tmr.delay(100000)
gpio.write(gpledM,gpio.LOW)
gpio.write(gpledV,gpio.LOW)
light(0)
if (c=="M") then
gpio.write(gpledM,gpio.HIGH)
else if (c=="V") then
gpio.write(gpledV,gpio.HIGH)
else if (c~=" ") then
gpio.write(gpledM,gpio.HIGH)
gpio.write(gpledV,gpio.HIGH)
end
end
end
end

-- I2C LCD display routines

w = function(b, mode)
i2c.start(0)
i2c.address(0, ADR, i2c.TRANSMITTER)
bh = bit.band(b, 0xF0) + _ctl + mode
bl = bit.lshift(bit.band(b, 0x0F), 4) + _ctl + mode
i2c.write(0, bh + 4, bh , bl + 4, bl)
i2c.stop(0)
end

-- backlight on/off
light = function(on)
_ctl = on and 0x08 or 0x00
w(0x00, 0)
end

clear = function()
w(0x01, 0)
end

_offsets = { [0] = 0x80, 0xC0, 0x94, 0xD4 } -- 20x4

locate = function(row, col)
return col + _offsets[row]
end

define_char = function(index, bytes)
w(0x40 + 8 * bit.band(index, 0x07), 0)
for i = 1, #bytes do w(bytes[i], 1) end
end

put = function(...)
for _, x in ipairs({...}) do
if type(x) == "number" then
w(x, 0)
elseif type(x) == "string" then
for i = 1, #x do w(x:byte(i), 1) end
end
tmr.delay(800)
end
end

run = function(row, s, _delay, timer, callback)
_delay = _delay or 40
tmr.stop(timer)
local i = 16
local runner = function()
put(
locate(row, i >= 0 and i or 0),
(i >= 0 and s:sub(1, 16 - i) or s:sub(1 - i, 16 - i)),
" "
)
if i == -#s then
if type(callback) == "function" then
tmr.stop(timer)
callback()
else
i = 16
end
else
i = i - 1
end
end
tmr.alarm(timer, _delay, 1, runner)
end

i2c.setup(0,sda,scl,i2c.SLOW)
w(0x33, 0)
w(0x32, 0)
w(0x28, 0)
w(0x0C, 0)
w(0x06, 0)
w(0x01, 0)
w(0x02, 0)
light(0xFF)
wifi.setmode(wifi.STATIONAP)
enduser_setup.start()
put(locate(0,0),"VCC messager")
put(locate(1,0),"Connecting wifi ")
dofile("connect.lua")
tmr.alarm(1, 5000, 1, domsg)