Affichage des articles dont le libellé est esp8266. Afficher tous les articles
Affichage des articles dont le libellé est esp8266. Afficher tous les articles

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 31 octobre 2015

PAWLOV : get the right level of romance, at the right time.

Sometimes when you're working late at the office or when you're away for business, you're not there to feed your loved one with your witty romantic lines.

Of course, you can always text or tweet here, but what if (s)he could get the exact level of romance (s)he wants, at the time she wants ? And what if you had all the plans & instructions to build one before next Valentine ?

Introducing the PAWLOV - "Potentiometric Automatic Wireless LOVe button".





Instructions are easy - you just have to adjust the level of romance needed (from zero to highly romantic) using the potentiometer, press the red button and bingo - your lover gets a romantic tweet !

This projects was based on Benlo's ''the button" ESP project (even if this is a simpler version using the fact that the emergency button locks itself in the "on" position), and there are more energy efficient versions.

Hardware:
   - ESP-12 (less than 2$ on aliexpress) with nodemcu lua firmware installed (lots of manuals for this on the internet)
   - emergency push button (4$ http://www.aliexpress.com/item/22MM-emergency-stop-push-button-switch-NO-NC-scram-Waterproof-control-box/989368329.html)
   - a 10 kohm linear resistor (1 $ or less)
   - 2 AA batteries + battery holder (a few cents on aliexpress)
   - a 22 kohm resistor (to drop the voltage from 3V  to 1V, which is the maximum voltage for adc.read)


Software is quite simple: when the user hits the emergency button (the model I used stays locked in the "on" position), this powers the ESP8266. At init, ESP8266 connects the wifi spot. When connected it hits a PHP link and transmits the voltage on A0 pins, which generates a tweet which romantism reflected the volat. Then the ESP goes to sleep.

Software loaded on the ESP-12

Init.lua (nb: you need to put the name of your AP& password instead of the text between "XXX" marks)
========
wifi.setmode(wifi.STATION)
wifi.sta.config("XXX you ap anme XXX","XXX password XXX")
wifi.sta.connect()
tries=-1
out=0
tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("Connecting to AP...\n")
      tries = tries + 1
      if (tries > 100) 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())
      tmr.stop(0)
      dofile("sendmail.lua")
   end
end)


sendmail.lua (nb: you need to put the name of you host & PHP pages instead of the text between "XXX" marks)
==========

jdone=0
conn=net.createConnection(net.TCP)
conn:on("receive", function(conn, payload)
    print("Received:"..payload)
 jdone=jdone + 1
    end)

conn:on("sent",function(conn)
    conn:close()
    print("Sent !")
    jdone = jdone + 1
    end)

conn:connect(80,'XXX your server IP adress XXX')
conn:send("GET XXX folder of the php file that generates a tweetXXXbuttonp.php?m="..adc.read(0)..." HTTP/1.1\r\n")
conn:send("Host: XXXput the name of your host XXX\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")

tmr.alarm(0, 5000, 1, function()
  if (jdone == 2) then
      tmr.stop(0)
      print("Going to sleep")
      node.dsleep()
 end

end)

Software loaded on your PHP host
buttonp.php (nb: you need to put the name of you host & PHP pages instead of the text between "XXX" marks)
This page uses TwitterAPIExchange.php (you'll find it easily with google). You need keys to access it, but it is well documented on the internet.


====
<?php

require_once('../incl/TwitterAPIExchange.php');



function getFeed($feed_url) { 
    $content = file_get_contents($feed_url);
if (($content != FALSE) and (strpos($content,"<channel>") != FALSE)) {
$x = new SimpleXmlElement($content);
return($x);}
return("");
}


$lfeed="http://feeds.feedburner.com/brainyquote/QUOTELO";
if (rand(0,1)==0) {
  $lfeed="https://www.quotesdaddy.com/feed/tagged/Inspirational";}
$inFeed=getFeed($lfeed);

$levels=array("pas du tout","un peu","beaucoup","passionnément","a la folie");
$twtn=rand(0,count($inFeed->channel->item)-1);
$m=$_GET["m"];
$pot=(intval($m) * count($levels)) / 1024;
$tweet=" i love you ".$levels[$pot];

foreach($inFeed->channel->item as $entry) {
if ($twtn==0){
$tweet=substr("@xxxyour loved one twitter account XXXX ".$tweet."-".str_replace('"',"",strip_tags($entry->description)),0,116)." ".$entry->link;
break;
}
$twtn=$twtn-1;

$url = 'https://api.twitter.com/1.1/statuses/update.json'; 
$requestMethod = 'POST';
$postfields = array('status' => $tweet ); 
$settings = array(
    'consumer_key' => "XXX put here your twitter account info XXX",
    'consumer_secret' => "XXX put here your twitter account info XXX",,
    'oauth_access_token' => "XXX put here your twitter account info XXX",,
    'oauth_access_token_secret' => "XXX put here your twitter account info XXX",
);

$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url, $requestMethod)
             ->setPostfields($postfields)
             ->performRequest();

?>