Showing posts with label lcd. Show all posts
Showing posts with label lcd. Show all posts

Wednesday, 22 July 2015

ESP8266 net display

Using the previous lcdprint function here is a simple remote display. To display on it from your server you can use wget (or nc or whatever) eg.

a message
wget "http://xxx.xxx.xxx.xxx/?message=This%20is%20line%201&line=1&col=0" 
the time and date
wget "http://xxx.xxx.xxx.xxx/?message=`date +"%H:%M %a %d %b"`&line=2&col=0"
 
 
Here is the code based on the toggle_pin.lua webap (here)  Run node.compile() on this as well as the lcdprint or it runs out of heap.

--based on the webap toggle_pin.lua
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
 conn:on("receive", function(client,request)
   local buf = "ok";
   local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
   if(method == nil)then
      _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
   end
   local _GET = {}
   if (vars ~= nil)then
     for k, v in string.gmatch(vars, "(%w+)=([%(%)%-:%w%s%%]+)&*") do
        _GET[k] = v
      end
   end
 if (_GET.message ~= nil)then
  -- from http://lua-users.org/wiki/StringRecipes
  local function url_decode(str)
    str = string.gsub (str, "+", " ")
    str = string.gsub (str, "%%(%x%x)",function(h) return string.char(tonumber(h,16)) end)
    str = string.gsub (str, "\r\n", "\n")
    return str
  end 
 
  local line=1
  local col=0

  if (_GET.line~= nil) then  line=_GET.line+0 end
  if (_GET.col ~= nil) then  col=_GET.col+0 end
  local message=url_decode(_GET.message)
  dofile("lcdprint.lc").lcdprint(message,line,col)
 end
 client:send(buf);
 client:close();
 collectgarbage();
 end)
end) 
 

Friday, 17 July 2015

ESP8266 I2C LCD

Starting with the init and send code from from abel's post on ESP8266.com , thanks it was very helpful. Changed the lcdprint function to use bit-wise ops and  added cursor movement . The display I have is this  from banggood, the reg value needed to be 0x00 instead of 0x4e.

Example usage:

--print on the first line 
dofile("lcdprint.lua").lcdprint("This is line 1",1,0)
 --print on the second line 
dofile("lcdprint.lua").lcdprint("This is line 2",2,2)
--clear screen
dofile("lcdprint.lua").cls()
--home
dofile("lcdprint.lua").home()
--show cursor
dofile("lcdprint.lua").cursor(1)
--hide cursor
dofile("lcdprint.lua").cursor(0)  

Here is the code (to be saved in "lcdprint.lua")  

--based on http://www.esp8266.com/viewtopic.php?f=6&t=841&p=20082&hilit=lcd#p11075 by abel 

--   PCF8574
--   P7   P6   P5   P4    P3      P2   P1   P0
--   D7   D6   D5   D4   (BL)   EN   RW   RS
--   HD44780
local M
do
local id = 0
local sda = 4      -- GPIO2
local scl = 5      -- GPIO14
local dev = 0x27   -- PCF8574
local reg = 0x00   -- write
i2c.setup(id, sda, scl, i2c.SLOW)

local bl = 0x08      -- 0x08 = back light on

local function send(data)
   local value = {}
   for i = 1, #data do
      table.insert(value, data[i] + bl + 0x04 + rs)
      table.insert(value, data[i] + bl +  rs)      -- fall edge to write
   end
  
   i2c.start(id)
   i2c.address(id, dev ,i2c.TRANSMITTER)
   i2c.write(id, reg, value)
   i2c.stop(id)
end
 

if (rs == nil) then
-- init
 rs = 0
 send({0x30})
 tmr.delay(4100)
 send({0x30})
 tmr.delay(100)
 send({0x30})
 send({0x20, 0x20, 0x80})   -- 4 bit, 2 line
 send({0x00, 0x10})            -- display clear
 send({0x00, 0xc0})            -- display on
end

local function cursor(op)
 local oldrs=rs
 rs=0
 if (op == 1) then 
   send({0x00, 0xe0})            -- cursor on
  else 
   send({0x00, 0xc0})            -- cursor off
 end
 rs=oldrs
end

local function cls()
 local oldrs=rs
 rs=0
 send({0x00, 0x10})
 rs=oldrs
end

local function home()
 local oldrs=rs
 rs =0
 send({0x00, 0x20})
 rs=oldrs
end

local function lcdprint (str,line,col)
rs = 0
--move cursor
if (line == 2) then
 send({0xc0,bit.lshift(col,4)})
elseif (line==1) then 
 send({0x80,bit.lshift(col,4)})
end

rs = 1
for i = 1, #str do
 local char = string.byte(string.sub(str, i, i))
 send ({ bit.clear(char,0,1,2,3),bit.lshift(bit.clear(char,4,5,6,7),4)})
end

end

M={
lcdprint=lcdprint,
cls = cls,
home=home,
cursor=cursor,
}
end
return M


Thursday, 26 May 2011

Arduino LCD Clock

Using the Wire library to read a DS1307 RTC chip (I used this one from Seeed Studio and bits of the example code ) and the LCD3wire library to implement, what is almost the "hello world" of arduino projects, a digital clock. The LCD3wire example uses a 4094 shift register, my parts box only had a 74HC595 so the connections are a little different:





















74HC595LCDArduinopower
14

25

36

414

513

612

711

8

gnd
9


10

+ve
11
4 (clk)
12
2 (strobe)
13

gnd
14
8 (data)
15


16

+ve

1
gnd

2
+ve

3
2.2K to gnd

//the "sketch"
// Example use of LCD3Wire and Wire library
// to read time from RTC and display on LCD

#include <LCD3Wire.h>
#include "Wire.h"
// Arduino pins
#define LCD_LINES 2 // number of lines in your display
#define DOUT_PIN 8 // Dout pin
#define CLK_PIN 4 // Clock pin
#define STR_PIN 2 // Strobe pin

#define DS1307_I2C_ADDRESS 0x68

long previousMillis = 0; // will store last time time was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
long interval = 200;

//create object to control an LCD.
LCD3Wire lcd = LCD3Wire(LCD_LINES, DOUT_PIN, STR_PIN, CLK_PIN);
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

}

void printTime()
{
lcd.cursorTo(1, 4); //line=1, x=4.
lcd.print( char( hour/10 + 0x30) );
lcd.print( char( hour%10 + 0x30) );
lcd.print(':');
lcd.print( char(minute/10 + 0x30));
lcd.print( char(minute%10 + 0x30));
lcd.print(':');
lcd.print(char (second/10+0x30));
lcd.print(char (second%10+0x30));

lcd.cursorTo(2, 3); //line=2, x=3.
lcd.print(char(dayOfMonth/10+0x30));
lcd.print(char(dayOfMonth%10+0x30));
lcd.print('/');
lcd.print(char(month/10+0x30));
lcd.print(char(month%10+0x30));
lcd.printIn("/20");
lcd.print(char(year/10+0x30));
lcd.print(char(year%10+0x30));
}

void setup() {
Wire.begin();
getDateDs1307() ;
lcd.init();
lcd.clear();

}

void loop() {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you printed and updated
previousMillis = currentMillis;
printTime();
//read RTC
getDateDs1307() ;
}
}
//end sketch

The result as expected (just a clock :-) ):