// You Have Mail !! // // Requires: // rookie // esp8266 // esp menu system - for setting up // IMAP library // // The following MUST be set correctly constant IMAPPORT 143 // this is probably correct constant IMAPSERVER$ "imap.myspace.co.uk" constant MAILBOX$ "me@info.com" constant MAILPASS$ "maypassword" // // ============================================================================= // ============================================================================= // ============================================================================= // L C D S E C T I O N // ============================================================================= // ============================================================================= // This is a basic display driver for the 16x2 lcd display // Display connection // LCDpin port:bit MX1 Pin // 1 GND GND 8 // 2 VCC +5V // 3 Contrast GND // 4 RS RB4 // 5 RW GND // 6 E RB5 // 11 D4 RB0 4 // 12 D5 RB1 5 // 13 D6 RB2 6 // 14 D7 RB3 7 // 15 BL RA2 9 // 16 BL GND // constant RS 4 constant E 5 constant D4 0 constant D5 1 constant D6 2 constant D7 3 constant BL 2 // ******************************************************************* // Sets ports I/O direction // ******************************************************************* function lcd_initPorts() io_pinMode(PORTB,RS,OUT,WOFF) io_pinMode(PORTB,E,OUT,WOFF) io_pinMode(PORTA,BL,OUT,WOFF) io_pinMode(PORTB,D4,OUT,WOFF) io_pinMode(PORTB,D5,OUT,WOFF) io_pinMode(PORTB,D6,OUT,WOFF) io_pinMode(PORTB,D7,OUT,WOFF) // important initailiswe with ports low io_write(PORTB,RS,LOW) io_write(PORTB,E,LOW) io_write(PORTA,BL,HIGH) // but turn on BL io_write(PORTB,D4,LOW) io_write(PORTB,D5,LOW) io_write(PORTB,D6,LOW) io_write(PORTB,D7,LOW) endf // ******************************************************************* // writes nibble to the 4 bits of the lcd port // ******************************************************************* function lcd_write4(nibble) io_write(PORTB,E,HIGH) // set E high io_setPortL(PORTB, 0xf) // clear the port bits first io_setPortH(PORTB, (nibble & 0xf)) // set the 1's io_write(PORTB,E,LOW) // latch it in endf // ******************************************************************* // sends data (8 bytes) to the 4 bit lcd port // ******************************************************************* function lcd_data(d) lcd_write4(d >> 4) // high nibble first lcd_write4(d) io_write(PORTB,RS,HIGH) // default high (cmd sets low) wait(5) // no LCD WR line do delay needed - may vary endf // ******************************************************************* // lcd command data is sent with RS low // ******************************************************************* function lcd_cmd(d) io_write(PORTB,RS,LOW) lcd_data(d) endf // ******************************************************************* // initiaalise display // ******************************************************************* function lcd_init() lcd_initPorts() wait(20) lcd_write4(3) // still in 8 bit mode wait(20) lcd_write4(3) // again wait(20) lcd_write4(2) // 4 bit mode wait(20) io_write(PORTB,RS,HIGH) // default now high wait(10) lcd_cmd(0x28); // function set 4 bit interface wait(10); lcd_cmd(0x0e); // display control wait(10); lcd_cmd(0x06); // entry mode wait(10); lcd_cmd(0x01); // cursor home wait(30); io_write(PORTA,BL,HIGH) endf // ============================================================================= // Some basic lcd items // ***************************************************************************** // ***************************************************************************** function lcd_bl(onn) if onn = 1 then io_write(PORTA,BL,HIGH) else io_write(PORTA,BL,LOW) endif endf // ***************************************************************************** // clear display // ***************************************************************************** function lcd_cls() lcd_cmd(1) endf // ***************************************************************************** // send to row, column top row is 1, first column is 0 // ***************************************************************************** function lcd_rc(r,c) if r = 1 then c = c + 0xc0 else c = c + 0x80 endif lcd_cmd(c) endf // ***************************************************************************** // sends a string // ***************************************************************************** function lcd_puts(s$[40]) dim j, k=min(strlen(s$),15) for j = 0 to k-1 lcd_data(asc(s$,j)) next endf // ***************************************************************************** // combines rx and puts // ***************************************************************************** function lcd_text(r,c,s$[40]) wait(10) lcd_rc(r,c) lcd_puts(s$) wait(100) // for it to complete endf // ***************************************************************************** // scrolling text from right to left, this is a blocking scroll so that // nothing else can happen while its on going // ***************************************************************************** function lcd_scroll(line, s$[120]) dim j, k, cp=15, ts=0, len=1 for k = 0 to strlen(s$) lcd_rc(line,cp) lcd_puts(mid$(s$,ts,len)) wait(150) // keeps test on the screen when finished if k = strlen(s$) then return endif lcd_rc(line,cp) for j = 0 to len lcd_data(' ') next if cp <> 0 then cp = cp-1 len = len + 1 else // scrolling ts = ts + 1 endif next endf // ============================================================================= // ============================================================================= // ============================================================================= // Y H M S E C T I O N // ============================================================================= // ============================================================================= //****************************************************************************** // This re-defines wf_start becasue some pins used for com1 are also used for // the LCD //****************************************************************************** function wf_start() //dim j io_pinMode(pCHPD,CHPD,OUT,WOFF) // set Power down pin io_pinMode(pRESET,RESET,OUT,WOFF) // reset pin io_write(pRESET,RESET,HIGH) // reset high io_write(pCHPD,CHPD,HIGH) // enable power comopen(1,115200,1000) // dont use rookie version, clashes with LCD // rx,tx com1_pins(*PRA4(),*PRA0()) print "\nwait..." waituntil("ready",5000) // cant rely on 'ready' endf // ***************************************************************************** // truncates the date so it will fit on a display from // from: Wed, 29 Oct 2014 09:02:32 GMT // To: 29 Oct 09:02:32 // ***************************************************************************** function imap_shortDate$(msgn) dim k, sd$[16]="", theDate$[40]="" imap_field(msgn, "Date") // Wed, 29 Oct 2014 09:02:32 GMT // put date in string // strip leading spaces (and a bit of the day) k = comkey(CPORT) while k = ' ' k = comkey(CPORT) wend while comkey?(CPORT) > 0 k = comkey(CPORT) if k < ' ' then break endif theDate$ = theDate$ + chr$(k) wend // use tokener to extract details k = 0 // for v pre 2.3 token$(theDate$,?k,' ') token$(theDate$,*k,' ') // dump Wed, sd$ = token$(theDate$,*k,' ') // 29 sd$ = sd$ + " "+ token$(theDate$,?*k,' ') // Oct token$(theDate$,*k,' ') // dump year sd$ = sd$ + " "+ token$(theDate$,*k,' ') // time return sd$ endf // ***************************************************************************** // email loop, needs global information, for demo this is gathered from the // user using the serial consol // ***************************************************************************** function imap_lcd() dim msgold=0, msg, j dim from$[120], subject$[120] while comkey?(2) = 0 if imap_start(IMAPSERVER$,IMAPPORT,MAILBOX$,MAILPASS$) <> 1 then lcd_text(0,0,"Error connecting") return endif imap_select("inbox") msg = imap_lastMessage() // get last message in inbox if msgold <> msg then for j = 1 to 5 lcd_bl(0) wait(250) lcd_bl(1) wait(250) next lcd_bl(1) // make sure its on msgold=msg lcd_cls() endif lcd_text(0,0,imap_shortDate$(msg)) from$ = imap_field$(msg,"From") subject$ = imap_field$(msg,"Subject") imap_quit() // scroll from lcd_text(1,0," ") // clear line lcd_scroll(1,from$) wait(2000) // pause // scroll subject lcd_text(1,0," ") // clear line lcd_scroll(1,subject$) wait(2000) wend endf function main() dim ssid$[60], pas$[60] lcd_init() // start LCD wait(500) lcd_cmd(0xc) // turn off cursor lcd_text(0,0,"Wait getting IP") wf_start() // get wi fi going wait(500) if mode(-1) <> 1 then mode(1) // set as a station endif if isIP() <> 1 then print "\nThere is no IP address, run esp_menu() again" lcd_text(0,0,"Error") lcd_text(1,0,"No IP address") endif imap_lcd() lcd_cls() lcd_text(0,1,"Ended") endf