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)

Lets start with the demo of the system :


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)

4 commentaires:

  1. Do you have an sketches for the Arduino IDE application showing working code using either a ESP8266 or WeMos D1 R2 and the Catalex SD MP3 Player?

    RépondreSupprimer
    Réponses
    1. I think if he had, he wouldnt have made this project, but the one you are asking for

      Supprimer
    2. Haha. Indeed. But sincerly Lua make protoryping much quicker than arduino (no compilation, easy debugging)

      Supprimer
  2. No, but it is very easy to flash Nodemcu firmware following the instructions.

    RépondreSupprimer