dimanche 16 octobre 2016

Disco on demand

The purpose of this project is to:
   - test the Sonoff smart switch (which uses an esp8266, see here : http://captain-slow.dk/2016/05/22/replacing-the-itead-sonoff-firmware/)
  - test the wemos D1 battery shield
  - make a system controlable with a pebble smart Watch
  - create a simplified-ultra light messaging system (using a simple file)
 - test PCM sound capability (here a low-fi one : 8kHz, mono)



Basic architecture:

   i) On the pebble, a java  app will touch a PHP pages, which triggers a very simple cloud application that updates the status of the disco ('on or off)
  iii) The sonoff runs a lua program which will poll every 2 seconds the server for the status.txt file and will start or stop the power according to the status
  iv) In the vintage radio, a wemos D1 + wemos battery shield + battery system does the same thing and plays (using the PCM Library) a disco song whenever the status is "on" (or stops it when the status turns to "off")
  v) The disco bowl connected to the sonoff has not been modified. You can find it on pearl.de, but it can be replace by any electrical device.

Détails & sources
A) Pebble app

var UI = require('ui');
var main = new UI.Card({
  banner: 'images/twitbot3.png',
});
main.show();
main.on('click', 'up', function(e) {
  var Vibe = require('ui/vibe');
  var card = new UI.Card();
  var req = new XMLHttpRequest();
 card.title('Disco on');
  card.show();
  req.open("GET", "http://my.host.net/sonof-seton.php", true);
  req.send(null);
  Vibe.vibrate('short');
  Vibe.vibrate('short');
});
main.on('click', 'select', function(e) {
  var Vibe = require('ui/vibe');
  var card = new UI.Card();
  var req = new XMLHttpRequest();
  card.title('Disco off');
  card.show();
  req.open("GET", "http://my.host.net/sonof-setoff.php", true);
  req.send(null);
  Vibe.vibrate('short');
});

B) PHP pages running on the server

sonoff-seton

<?php
$fname="/myserver/state.txt";
ini_set('display_errors', true);
echo ("Switching on\n");
$handle=fopen($fname,"w");
fwrite($handle,"SONOF_STATEon");
fclose($handle);
?>

sonoff-setoff

<?php
$fname="/myserver/state.txt";
ini_set('display_errors', true);
echo ("Switching off\n");
$handle=fopen($fname,"w");
fwrite($handle,"SONOF_STATEoff");
fclose($handle);
?>

state.txt

SONOF_STATEoff

C) Vintage radio hardware

The vintage radio uses
   - a wemos D1 shield
   - a wemos battery shield
   - a 2$ battery shield soldered on the battery shield
   - a 2N3904 transistor and a resistor connected to the speaker of an old radio (needed to get enough power)






The firmware can be generated using nodemcu custom-build  site (https://nodemcu-build.com/). You need to select wifi, file and PCM options
Then you need the following lua files runnig on the wemos.

init.lua

gpio.mode(6, gpio.OUTPUT)
dofile("connect.lua")

connect.lua

--Connects to the first hotspot, then the second if no success
wifi.setmode(wifi.STATION)
wifi.sta.config("acces point 1","password 1")
wifi.sta.connect()
tries=-1
tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("Connecting to AP...\n")
      tries = tries + 1
      if (tries==30) then
        wifi.sta.config("acces point 2","password 2")
      end
      if (tries > 100) then
          tmr.stop(0)
      end
   else
      tmr.stop(0)
      dofile("poll-web.lua")
   end
end)

poll-web.lua

music=0
function poller()
 conn=net.createConnection(net.TCP)
 conn:on("connection",function(conn)
    conn:send("GET  /myfolder/state.txt HTTP/1.1\r\nHost: cluster014.ovh.net\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp; Win NT 5)\r\n\r\n")
    end)
 conn:connect(80,'myhost')
 conn:on("receive", function(conn, pl)
 conn:close()
 i=string.find(pl,"SONOF_STATE")
 if (i ~= nil) then
  pl=string.sub(pl,i+11)
  if (pl=="on") then
    if (music==0) then
     dofile("disco.lua")
     music=1
     end
    else
      if (pl=="off") then
        if (music==1) then
         drv:stop()
         music=0
         end
      end
  end
 end
 end)
 conn=""
 collectgarbage()
end
tmr.alarm(0, 5000, 1, poller)

disco.lua

function cb_drained(d)
  print("drained "..node.heap())
  file.seek("set", 0)
  d:play(pcm.RATE_8K)
end
function cb_stopped(d)
  print("playback stopped")
  file.seek("set", 0)
end
function cb_paused(d)
  print("playback paused")
end
file.open("Borntobe.wav", "r")
drv = pcm.new(pcm.SD, 6)
drv:on("data", file.read)
drv:on("drained", cb_drained)
drv:on("stopped", cb_stopped)
drv:on("paused", cb_paused)
drv:play(pcm.RATE_8K)

Borntobe.wav

**This is a 8 khz, mono PCM file. For copyright reasons i cant distribute it but using a software like goldwave you can easily put any MP3 in this format**

D) Sonoff smart switch

I reflashed it using the method described here: http://captain-slow.dk/2016/05/22/replacing-the-itead-sonoff-firmware
Please not that you need a small firmware (the esp in the sonoff is a 1Mb flash memory chip, 4 times smaller than the wemos).

The lua file is similar to the ones running in the vintage radio (he only difference is that instead of playing the disco music, you put GPIO7 high or low to start the power)

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)

samedi 5 mars 2016

The purpose of this project is a simple "web messenger", including a weblink allowing you to enter a text remotely (from your smartphone or PC), and a receiver displaying any new message sent through the weblink.

You can easily modify this project to display any web based information you wanted (last tweets, weather,





0) What you need
   a) One ESP8266 (I used this one for debugging purposes but a cheaper one costs 2$) and an usb cable to power the messenger
   b) A 5V to 3.3 V converter. Cost : 0,4 $
   c) A 16x2 I2C LCD module.Cost 1,7 €
   d) A 3.3V - 5V logic converter (the LCD is 5V, the ESP 3.3V). Cost 0,28 € here
   d) The box is a business card plastic box (cost : zero)

You'll need a wifi internet access + a web hosting allowing PHP

1) The hardware

The hardware part is relatively simple.







 2) The software
    a) The web page (to be hosted on your php server)

msg_link.html : this is the page you'll call to send a new message

<html><body>
¨Please type your message
<form action="fetchmsg.php" method="post">
<table>
<tr><th>Auteur</th><td><input name=aut size=16 maxlength=16 type=text></td></th></tr>
<br>
<tr><th>Message</th><td><input name=msg size=144 maxlength=144 type=text></td></th></tr>
</table>
<br>
<input type=submit value="Send">
</form>
<br>

fetchmsg.php : this page is called by the msg_link.html page and stores your message in a "message.txt" file on your web host

<html><body><?php
$msg=$_POST['msg'];
$aut=$_POST['aut']."                ";
$counter_name = "message.txt";
$f = fopen($counter_name, "w");
$payld=substr($aut,0,16).substr(trim($msg),0,144);
fwrite($f,"<html><header><meta http-equiv='Cache-Control' content='no-cache, no-store, must-revalidate'/><meta http-equiv='Pragma' content='no-cache'/></header>BEGIN_MSG".$payld);
fclose($f);

echo "Message sent''".$payld."''/n";
?>

</body>
</html>

 b) On the nodemcu

init.lua 

NB: the delay is there to help you stop the program at startup if needed. Once you have debugged your code you can delete it

tmr.delay(2000000)
dofile("main.lua")

main.lua
main.lua is based on V Dronnikov's lcd control software (https://github.com/dvv/nodemcu-thingies/blob/master/lcd1602.lua).

ADR = 0x27
_ctl = 0x08
payload=""
oldpl=""
tries=100
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

  -- return command to set cursor at row/col
 _offsets = { [0] = 0x80, 0xC0, 0x94, 0xD4 } -- 20x4
  -- local _offsets = { [0] = 0x80, 0xC0, 0x90, 0xD0 } -- 16x4

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
      -- number?
      if type(x) == "number" then
        -- direct command
        w(x, 0)
      -- string?
      elseif type(x) == "string" then
        -- treat as data
        for i = 1, #x do w(x:byte(i), 1) end
      end
      tmr.delay(800)
    end
end

  -- show a running string s at row. shift delay is _delay using timer,
  --     on completion spawn callback

  run = function(row, s, _delay, timer, callback)
    _delay = _delay or 40
    tmr.stop(timer)
    local i = 16
    local runner = function()
      -- TODO: optimize calculus?
      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

domsg = function()
    if (tries ==0) then
    dofile("checkmsg.lua")
    if (payload ~= oldpl) then
tmr.alarm(1, 20000, 1, domsg)
    light(off)
    put(locate(0,0),string.sub(payload,1,16))
    run(1,string.sub(payload,17,-3),600,0)
    oldpl=payload
tmr.delay(100000)
    light(0)
tmr.delay(100000)
    light(off)
tmr.delay(100000)
    light(0)
else
put(locate(1,0),"Connection ok   ")
    end
     end
end


i2c.setup(0,3,4,i2c.SLOW)
w(0x33, 0)
w(0x32, 0)
w(0x28, 0)
w(0x0C, 0)
w(0x06, 0)
w(0x01, 0)
w(0x02, 0)

put(locate(0,0),"Esp messager")
put(locate(1,0),"Connecting wifi ")
dofile("connect.lua")
tmr.alarm(1, 2000, 1, domsg)

connect.lua

NB:You need to change Your_SSID and Your_Password according to you internet accesss

wifi.setmode(wifi.STATION)
wifi.sta.config("Your_SSID","Your_Passwod")
wifi.sta.connect()
tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("Connecting to AP...\n")
      tries = tries - 1
      if (tries < 0) then
          tmr.stop(0)
      end
   else
      ip, nm, gw=wifi.sta.getip()
      print("IP Info: \nIP Address: ",ip)
      print("Netmask: ",nm)
      print("Gateway Addr: ",gw,'\n')
      print("Mac:"..wifi.sta.getmac())
 tries=0
      tmr.stop(0)
   end

end)

checkmsg.lua

NB: You need to change 215.555.55.5,  /~your/folder and host.your.com respectively to the IP adress of your web host (if you dont know it you'll find online lost of tools to obtain it based on the internet adress of you msg_link.html file), the full path of you msg_link.html page and your host name

conn=net.createConnection(net.TCP)
conn:on("receive", function(conn, pl)
i=string.find(pl,"BEGIN_MSG")
if (i ~= nil) then
payload=string.sub(pl,i+9)
print("Recu:"..payload)
end
    collectgarbage()
    end)

conn:on("sent",function(conn)
    end)
conn:connect(80,'215.555.55.5')
conn:send("GET /~your/folder/message.txt HTTP/1.1\r\n")
conn:send("Host: host.your.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp; Win NT 5)\r\n")
conn:send("\r\n")