Last active
March 1, 2019 15:21
-
-
Save B45i/befede353b3f3cdf91a0b8bd1d6f55be to your computer and use it in GitHub Desktop.
Micro python news display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import network | |
import urequests | |
import time | |
import machine | |
import ssd1306 | |
i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5)) | |
oled = ssd1306.SSD1306_I2C(128, 64, i2c) | |
URL = 'http://newsapi.org/v2/top-headlines?sources=the-verge&apiKey=374fb9f276c24901a6c74c82f7bc4d68' | |
def display_text(s): | |
for i in range(len(s)): | |
oled.fill(0) | |
oled.text(s, -i, 0) | |
oled.show() | |
time.sleep_ms(50) | |
def connect_to_wifi(ssid, password): | |
sta_if = network.WLAN(network.STA_IF) | |
if not sta_if.isconnected(): | |
print('connecting to network...') | |
display_text('connecting to network...') | |
sta_if.active(True) | |
sta_if.connect(ssid, password) | |
while not sta_if.isconnected(): | |
pass | |
print('Connected to %s' % ssid) | |
display_text('Connected to %s' % ssid) | |
print('network config:', sta_if.ifconfig()) | |
def get_news(): | |
print('Refreshing.....') | |
result = urequests.get(URL) | |
if(result.status_code == 200): | |
for x in result.json()['articles']: | |
print(x['title']) | |
display_text(x['title']) | |
time.sleep_ms(5000) | |
connect_to_wifi('EvilCorp', 'helloworld') | |
while True: | |
get_news() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment