Skip to content

Instantly share code, notes, and snippets.

@adegard
Forked from slayerjay/ArduinoCode.ino
Last active June 14, 2020 20:14
Show Gist options
  • Save adegard/943ee86d3f8fdd720e0d2e2195bc21ad to your computer and use it in GitHub Desktop.
Save adegard/943ee86d3f8fdd720e0d2e2195bc21ad to your computer and use it in GitHub Desktop.
Arduino and Python Serial Connection with LCD . UPDATED . (https://www.instructables.com/id/Wiring-up-the-LCD-and-the-LED/)
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("start");
}
void loop() {
if (Serial.available()) {
delay(100); //wait some time for the data to fully be read
lcd.clear();
while (Serial.available() > 0) {
char c = Serial.read();
lcd.write(c);
}
}
}
#import library to do http requests:
import urllib.request as urllib2
#https://stackoverflow.com/questions/34475051/need-to-install-urllib2-for-python-3-5-1
#import urllib2
#import pyserial Library
import serial
#import time library for delays
import time
#import xml parser called minidom:
from xml.dom.minidom import parseString
#Initialize the Serial connection in COM3 or whatever port your arduino uses at 9600 baud rate
ser = serial.Serial("\\.\COM3", 9600)
i = 1
#delay for stability while connection is achieved
time.sleep(5)
while i == 1:
#download the rss file feel free to put your own rss url in here
file = urllib2.urlopen('https://www.lemonde.fr/rss/une.xml')
#convert to string
data = file.read()
#close the file
file.close()
#parse the xml from the string
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName change tags to get different data
xmlTag = dom.getElementsByTagName('title')[2].toxml()
# the [2] indicates the 3rd title tag it finds will be parsed, counting starts at 0
#strip off the tag (<tag>data</tag> ---> data)
xmlData=xmlTag.replace('<title>','').replace('</title>','')
#write the marker ~ to serial
ser.write(str.encode('~'))
time.sleep(5)
#split the string into individual words
nums = xmlData.split(' ')
#loop until all words in string have been printed
for num in nums:
#write 1 word
ser.write(str.encode(num))
# write 1 space
ser.write(str.encode(' '))
# THE DELAY IS NECESSARY. It prevents overflow of the arduino buffer.
time.sleep(2)
# write ~ to close the string and tell arduino information sending is finished
ser.write(str.encode('~'))
# wait 5 minutes before rechecking RSS and resending data to Arduino
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment