i2c-16x2-text

Display text on i2c 16x2 LCD

With configurable i2c buss, i2c address, and alignment options.

Uses python-function node

[{"id":"cf107bdef85dbd33","type":"subflow","name":"i2c-16x2-text","info":"","category":"","in":[{"x":200,"y":180,"wires":[{"id":"b24b4640dbbe5443"}]}],"out":[{"x":880,"y":180,"wires":[{"id":"215d11bec0f661f5","port":0}]}],"env":[{"name":"i2cBus","type":"num","value":"1","ui":{"icon":"font-awesome/fa-bus","label":{"en-US":"I2C bus"},"type":"input","opts":{"types":["num","env"]}}},{"name":"i2cAddress","type":"str","value":"0x27","ui":{"icon":"font-awesome/fa-address-card-o","label":{"en-US":"I2C address"},"type":"input","opts":{"types":["str","env"]}}},{"name":"line1","type":"str","value":"","ui":{"icon":"font-awesome/fa-window-minimize","type":"input","opts":{"types":["str","num"]}}},{"name":"line2","type":"str","value":"","ui":{"icon":"font-awesome/fa-window-minimize","type":"input","opts":{"types":["str","num"]}}},{"name":"alignment","type":"str","value":"left","ui":{"icon":"font-awesome/fa-align-left","type":"select","opts":{"opts":[{"l":{"en-US":"left"},"v":"left"},{"l":{"en-US":"center"},"v":"center"},{"l":{"en-US":"right"},"v":"right"}]}}}],"meta":{},"color":"#DDAA99"},{"id":"4fb9374e5e5740f8","type":"python-function","z":"cf107bdef85dbd33","name":"i2c out","func":"# _____ _____ _____ __ __ _____ _____ \n#|     |   __|     |  |  |     |     |\n#|  |  |__   |  |  |_   _|  |  |  |  |\n#|_____|_____|_____| |_| |_____|_____|\n#\n# Project Tutorial Url:http://osoyoo.com/?p=1031\n#  \n \n\nimport smbus\nimport time\n\n# Define some device parameters\n# I2C_ADDR  = \"0x27\" # I2C device address, if any error, change this address to 0x3f\nI2C_ADDR  = msg[\"config\"][\"i2cAddress\"] # I2C device address, if any error, change this address to 0x3f\nLCD_WIDTH = 16   # Maximum characters per line\n\n# Define some device constants\nLCD_CHR = 1 # Mode - Sending data\nLCD_CMD = 0 # Mode - Sending command\n\nLCD_LINE_1 = 0x80 # LCD RAM address for the 1st line\nLCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line\nLCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line\nLCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line\n\nLCD_BACKLIGHT  = 0x08  # On\n# LCD_BACKLIGHT = 0x00  # Off\n\nENABLE = 0b00000100 # Enable bit\n\n# Timing constants\nE_PULSE = 0.0005\n# E_PULSE = 0.0500\nE_DELAY = 0.0005\n\n#Open I2C interface\n# bus = smbus.SMBus(0)  # Rev 1 Pi uses 0\n# bus = smbus.SMBus(1) # Rev 2 Pi uses 1\nbus = smbus.SMBus(msg[\"config\"][\"i2cBus\"]) # Rev 2 Pi uses 1\n\ndef lcd_init():\n  # Initialise display\n  lcd_byte(0x33,LCD_CMD) # 110011 Initialise\n  lcd_byte(0x32,LCD_CMD) # 110010 Initialise\n  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction\n  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off \n  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size\n  lcd_byte(0x01,LCD_CMD) # 000001 Clear display\n  time.sleep(E_DELAY)\n\ndef lcd_byte(bits, mode):\n  # Send byte to data pins\n  # bits = the data\n  # mode = 1 for data\n  #        0 for command\n\n  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT\n  bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT\n\n  # High bits\n  bus.write_byte(I2C_ADDR, bits_high)\n  lcd_toggle_enable(bits_high)\n\n  # Low bits\n  bus.write_byte(I2C_ADDR, bits_low)\n  lcd_toggle_enable(bits_low)\n\ndef lcd_toggle_enable(bits):\n  # Toggle enable\n  time.sleep(E_DELAY)\n  bus.write_byte(I2C_ADDR, (bits | ENABLE))\n  time.sleep(E_PULSE)\n  bus.write_byte(I2C_ADDR,(bits & ~ENABLE))\n#   time.sleep(E_DELAY)\n\ndef lcd_string(message,line):\n  # Send string to display\n\n  message = message.ljust(LCD_WIDTH,\" \")\n\n  lcd_byte(line, LCD_CMD)\n\n  for i in range(LCD_WIDTH):\n    lcd_byte(ord(message[i]),LCD_CHR)\n\ndef main():\n  # Main program block\n\n  # Initialise display\n  lcd_init()\n  lcd_string(msg['config']['line1'],LCD_LINE_1)\n  lcd_string(msg['config']['line2'],LCD_LINE_2)\n  \n  return msg\n#   while True:\n\n# Send some test\n\n \n# if __name__ == '__main__':\n\ntry:\n  return main()\n    # return msg[0]\nexcept KeyboardInterrupt:\n  pass\n# finally:\n#   lcd_byte(0x01, LCD_CMD)\n\n \n\n","outputs":1,"x":550,"y":180,"wires":[["215d11bec0f661f5"]]},{"id":"b24b4640dbbe5443","type":"change","z":"cf107bdef85dbd33","name":"config & overrides","rules":[{"t":"set","p":"config","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"config.i2cBus","pt":"msg","to":"i2cBus","tot":"env"},{"t":"set","p":"config.i2cAddress","pt":"msg","to":"i2cAddress","tot":"env"},{"t":"set","p":"config.line1","pt":"msg","to":"payload.line1 != null\t? payload.line1 \t: $env(\"line1\")\t\t","tot":"jsonata"},{"t":"set","p":"config.line2","pt":"msg","to":"payload.line2 != null\t? payload.line2 \t: $env(\"line2\")\t\t","tot":"jsonata"},{"t":"set","p":"config.alignment","pt":"msg","to":"payload.alignment != null\t? payload.alignment \t: $env(\"alignment\")\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":260,"wires":[["60867ed3b120ee2c"]]},{"id":"de379b706fa11fa9","type":"debug","z":"cf107bdef85dbd33","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":280,"wires":[]},{"id":"60867ed3b120ee2c","type":"function","z":"cf107bdef85dbd33","name":"clean up","func":"msg.config.i2cAddress = Number(msg.config.i2cAddress);\n\nmsg.config.line1 = \n    msg.config.line1 ?\n    msg.config.line1.toString().trim().substring(0,16) : \"\";\n\nmsg.config.line2 = \n    msg.config.line2 ?\n    msg.config.line2.toString().trim().substring(0,16) : \"\";\n    \nconst alignments = [\"left\", \"center\", \"right\"];\nif (!alignments.includes(msg.config.alignment)) msg.config.alignment = \"left\";\n\nif (msg.config.alignment==\"right\")\n{\n    msg.config.line1 = msg.config.line1.padStart(16);\n    msg.config.line2 = msg.config.line2.padStart(16);\n} else if (msg.config.alignment==\"center\"){\n    msg.config.line1 = msg.config.line1.padStart(msg.config.line1.length + ((16 - msg.config.line1.length)/2));\n    msg.config.line2 = msg.config.line2.padStart(msg.config.line2.length + ((16 - msg.config.line2.length)/2));\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":280,"wires":[["de379b706fa11fa9","4fb9374e5e5740f8"]]},{"id":"215d11bec0f661f5","type":"change","z":"cf107bdef85dbd33","name":"","rules":[{"t":"delete","p":"config","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":180,"wires":[[]]}]

Flow Info

Created 3 years, 4 months ago
Rating: 1 1

Owner

Actions

Rate:

Node Types

Core
  • change (x2)
  • debug (x1)
  • function (x1)
Other

Tags

  • i2c
  • 16x3
  • python-function
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option