Created
May 15, 2025 22:25
-
-
Save pepinisillo/6c132568e1f3e4ff08c76c6c5da00aae to your computer and use it in GitHub Desktop.
Rasberry Pi Pico W con Display OLED ssd1306 y ChatGPT API
This file contains hidden or 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
# ♡ ∩_∩ | |
# („• ֊ •„)♡ | |
# | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
# | • Lenguajes de Interfaz en TECNM Campus ITT | | |
# | • Autor: Alejandro Suarez Sandoval | | |
# | • Fecha: 2025/05/15 | | |
# | • Descripción: Programa en Python para Raspberry Pi Pico W que se conecta con la API | | |
# | de ChatGPT para que cambie un texto dado por el usuario a uno con palabras | | |
# | más bonitas. | | |
#  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
# | |
# ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
# ═════════•°• Código en lenguaje Python Rasberry Pi Pico W•°•═══════ | |
import json | |
import network | |
import time | |
import urequests | |
from machine import Pin, I2C | |
import ssd1306 | |
# ----- CONFIG ----- | |
WIFI_SSID = "IZZI-E324" | |
WIFI_PASSWORD = "******" | |
API_KEY = "sk-******" | |
ENDPOINT = "https://api.openai.com/v1/edits" | |
MODEL = "gpt-3.5-turbo" | |
# ----- CONFIG I2C OLED ----- | |
i2c = I2C(1, scl=Pin(27), sda=Pin(26)) | |
oled = ssd1306.SSD1306_I2C(128, 64, i2c) | |
# ----- FUNCIONES ----- | |
def conectar_wifi(ssid, password): | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
print(f"Conectando a {ssid}...") | |
wlan.connect(ssid, password) | |
for _ in range(20): | |
if wlan.isconnected(): | |
break | |
print(".", end="") | |
time.sleep(0.5) | |
if wlan.isconnected(): | |
print("\n✅ Conectado") | |
print("IP:", wlan.ifconfig()[0]) | |
else: | |
raise RuntimeError("❌ No se pudo conectar al Wi-Fi") | |
def mostrar_texto_oled(texto, oled): | |
oled.fill(0) # limpia pantalla | |
lineas = texto.split('\n') | |
y = 0 | |
for linea in lineas: | |
oled.text(linea, 0, y) | |
y += 10 | |
if y > 54: | |
break | |
oled.show() | |
def editar_texto(input_text, instruction): | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {API_KEY}" | |
} | |
data = { | |
"model": MODEL, | |
"input": input_text, | |
"instruction": instruction | |
} | |
json_data = json.dumps(data) | |
print("⏳ Enviando solicitud de edición a OpenAI...") | |
try: | |
r = urequests.post(ENDPOINT, data=json_data, headers=headers) | |
if r.status_code == 200: | |
respuesta = r.json() | |
texto_editado = respuesta["choices"][0]["text"] | |
print("✅ Respuesta editada:") | |
print(texto_editado) | |
mostrar_texto_oled(texto_editado, oled) | |
else: | |
print("❌ Error en respuesta:") | |
print(r.text) | |
r.close() | |
except Exception as e: | |
print("❌ Error al hacer la solicitud:", e) | |
# ----- EJECUCIÓN ----- | |
conectar_wifi(WIFI_SSID, WIFI_PASSWORD) | |
input_text = "Fui a la playa esta tarde, el sol se estaba poniendo." | |
instruction = "Edita este texto con palabras más bonitas, en vez de comunes" | |
editar_texto(input_text, instruction) |
This file contains hidden or 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
# ♡ ∩_∩ | |
# („• ֊ •„)♡ | |
# | ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄| | |
# | • Lenguajes de Interfaz en TECNM Campus ITT | | |
# | • Autor: Alejandro Suarez Sandoval | | |
# | • Fecha: 2025/05/15 | | |
# | • Descripción: Programa en Python para Raspberry Pi Pico W que se conecta con la API | | |
# | de ChatGPT para obtener el clima de Tijuana y mostrar una representación gráfica | | |
# | en una pantalla OLED. | | |
#  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ | |
# | |
# ⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂⠁⠁⠂⠄⠄⠂ ⠂⠄⠄⠂☆ | |
# ═════════•°• Código en lenguaje Micropython Rasberry Pi Pico W•°•═══════ | |
import json | |
import network | |
import time | |
import urequests | |
from machine import Pin, I2C | |
import ssd1306 | |
# ----- CONFIGURACIÓN ----- | |
WIFI_SSID = "IZZI-E324" | |
WIFI_PASSWORD = "*******" | |
API_KEY = "sk-*******" | |
ENDPOINT = "https://api.openai.com/v1/edits" | |
MODEL = "gpt-3.5-turbo" | |
# OLED I2C en GP26 (SDA) y GP27 (SCL) | |
i2c = I2C(1, scl=Pin(27), sda=Pin(26)) | |
oled = ssd1306.SSD1306_I2C(128, 64, i2c) | |
# ----- FUNCIONES ----- | |
def conectar_wifi(ssid, password): | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
print(f"Conectando a {ssid}...") | |
wlan.connect(ssid, password) | |
for _ in range(20): | |
if wlan.isconnected(): | |
break | |
print(".", end="") | |
time.sleep(0.5) | |
if wlan.isconnected(): | |
print("\n✅ Conectado") | |
print("IP:", wlan.ifconfig()[0]) | |
else: | |
raise RuntimeError("❌ No se pudo conectar al Wi-Fi") | |
def mostrar_clima_oled(clima): | |
oled.fill(0) | |
clima = clima.lower() | |
if "soleado" in clima: | |
oled.text("Clima: Soleado", 0, 0) | |
oled.circle(64, 32, 10, 1) | |
elif "nublado" in clima: | |
oled.text("Clima: Nublado", 0, 0) | |
oled.fill_rect(40, 25, 48, 20, 1) | |
elif "lluvia" in clima or "lloviendo" in clima: | |
oled.text("Clima: Lluvia", 0, 0) | |
oled.fill_rect(40, 20, 48, 15, 1) | |
oled.text("///", 55, 40) | |
elif "viento" in clima: | |
oled.text("Clima: Viento", 0, 0) | |
oled.text("~ ~ ~ ~ ~", 20, 30) | |
elif "nevado" in clima or "nieve" in clima: | |
oled.text("Clima: Nieve", 0, 0) | |
oled.text("* * *", 50, 30) | |
else: | |
oled.text("Clima desconocido", 0, 0) | |
oled.show() | |
def obtener_clima(): | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {API_KEY}" | |
} | |
data = { | |
"model": MODEL, | |
"messages": [ | |
{ | |
"role": "user", | |
"content": "Describe en una palabra el clima actual en Tijuana. Solo responde con una palabra como: soleado, nublado, lluvia, viento o nieve." | |
} | |
] | |
} | |
try: | |
print("Preguntando a ChatGPT el clima en Tijuana...") | |
response = urequests.post(ENDPOINT, headers=headers, data=json.dumps(data)) | |
if response.status_code == 200: | |
respuesta = response.json() | |
mensaje = respuesta["choices"][0]["message"]["content"].strip() | |
print("Clima recibido:", mensaje) | |
mostrar_clima_oled(mensaje) | |
else: | |
print("❌ Error:", response.status_code) | |
print(response.text) | |
response.close() | |
except Exception as e: | |
print("❌ Error al consultar el clima:", e) | |
# ----- EJECUCIÓN ----- | |
conectar_wifi(WIFI_SSID, WIFI_PASSWORD) | |
obtener_clima() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment