Skip to content

Instantly share code, notes, and snippets.

@varlen
Last active April 18, 2024 00:02
Show Gist options
  • Save varlen/91170e7cdd61032107e833fce6b7106a to your computer and use it in GitHub Desktop.
Save varlen/91170e7cdd61032107e833fce6b7106a to your computer and use it in GitHub Desktop.
Writing on LCD Display using Python and Arduino. Requires pyfirmata module.
from pyfirmata import Arduino, util, STRING_DATA
board = Arduino('COM6')
board.send_sysex( STRING_DATA, util.str_to_two_byte_iter('Hello!') )
def msg( text ):
if text:
board.send_sysex( STRING_DATA, util.str_to_two_byte_iter( text ) )
#include <LiquidCrystal.h>
#include <Firmata.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
int lastLine = 1;
void stringDataCallback(char *stringData){
if ( lastLine ) {
lastLine = 0;
lcd.clear();
} else {
lastLine = 1;
lcd.setCursor(0,1);
}
lcd.print(stringData);
}
void setup() {
lcd.begin(16,2);
Firmata.setFirmwareVersion( FIRMATA_MAJOR_VERSION, FIRMATA_MINOR_VERSION );
Firmata.attach( STRING_DATA, stringDataCallback);
Firmata.begin();
}
void loop() {
while ( Firmata.available() ) {
Firmata.processInput();
}
}
from pyfirmata import Arduino, util, STRING_DATA
import requests, time
board = Arduino('COM6')
def msg( text ):
if text:
board.send_sysex( STRING_DATA, util.str_to_two_byte_iter( text ) )
else:
board.send_sysex( STRING_DATA, util.str_to_two_byte_iter( ' ' ) )
def load():
msg('Atualizando...')
rio = requests.get('http://api.hgbrasil.com/weather/?format=json&cid=BRXX0201')
rio_json = rio.json()['results']
msg('Carregou')
screens = [
[ rio_json['city_name'], rio_json['date'] + ' ' + rio_json['temp'] + 'C' ],
[ rio_json['city_name'], rio_json['description'] ],
[ rio_json['city_name'], 'Min:'+rio_json['forecast'][0]['min']+'C Max:' + rio_json['forecast'][0]['max'] + 'C' ],
[ rio_json['city_name'], 'Vento:'+rio_json['wind_speedy'] ],
[ rio_json['city_name'], 'Humidade:' + rio_json['humidity'] + '%' ],
[ rio_json['forecast'][1]['weekday'] + ' ' + rio_json['forecast'][1]['date'], rio_json['forecast'][1]['description'] ],
[ rio_json['forecast'][1]['weekday'] + ' ' + rio_json['forecast'][1]['date'], 'Min:'+rio_json['forecast'][1]['min']+'C Max:' + rio_json['forecast'][1]['max'] + 'C' ],
[ rio_json['forecast'][2]['weekday'] + ' ' + rio_json['forecast'][2]['date'], rio_json['forecast'][2]['description'] ],
[ rio_json['forecast'][2]['weekday'] + ' ' + rio_json['forecast'][2]['date'], 'Min:'+rio_json['forecast'][2]['min']+'C Max:' + rio_json['forecast'][2]['max'] + 'C' ],
[ rio_json['forecast'][3]['weekday'] + ' ' + rio_json['forecast'][3]['date'], rio_json['forecast'][3]['description'] ],
[ rio_json['forecast'][3]['weekday'] + ' ' + rio_json['forecast'][3]['date'], 'Min:'+rio_json['forecast'][3]['min']+'C Max:' + rio_json['forecast'][3]['max'] + 'C' ],
[ 'Atualizado em', time.asctime()[:16] ]
]
return screens
screens = load()
start_time = time.time()
while (True):
time_now = time.time()
if (time_now - start_time) > 1800.0:
screens = load()
start_time = time.time()
for screen in screens:
msg(screen[0])
time.sleep(0.01)
msg(screen[1])
time.sleep(2)
@nildude
Copy link

nildude commented Mar 6, 2023

This is an amazing example. I have not tested my example out yet but this seems so simple and obvious! Thank you! Do you have any good reference to the pyfirmata project? The documentation I am seeing are simply too basic and does not cover any of the packages such as the use of STRING_DATA?

@varlen thank you!

@varlen
Copy link
Author

varlen commented Sep 7, 2023

@nildude I believe that the firmata protocol docs may help you https://github.com/firmata/protocol/blob/master/protocol.md

Thanks for the inspiring feedback :)

@Perseverantia21
Copy link

Hi, I'm using Arduino Uno and HD44780 16x2 LCD screen with an I2C converter. I've just copied your first code but instead pyfirmata I'm using pyfirmata2. I've added an extra line of code "print("code works")" just to check if everything is working and everything works perfectly but after running a script my display goes out and lights up but instead of my text "Hello" I still have old text which I've loaded by Arduino IDE. I would like to load or display my own text by python via Studio Visual Code without using Arduino IDE. Any idea? Thx for help!

@wonton1234
Copy link

wonton1234 commented Apr 17, 2024

hey, when i do it it dosent work i am using a techspace learning I2C converter 🙂

@wonton1234
Copy link

@wonton1234 i also dont have a working camera to show you...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment