Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- po spusteni programu na raspberry pi pico W a pripojeni k wifi se jednou data odeslou a skonci chybou:
- import network
- import machine
- import time
- from machine import Pin, I2C
- import bmp180
- from umqtt.simple import MQTTClient
- # Inicializace I2C
- i2c = I2C(0, scl=Pin(1), sda=Pin(0))
- # Funkce pro načtení přihlašovacích údajů ze souboru
- def load_credentials(filename):
- with open(filename, 'r') as file:
- lines = file.readlines()
- ssid = lines[1].strip()
- password = lines[2].strip()
- ADAFRUIT_AIO_USERNAME = lines[4].strip()
- ADAFRUIT_AIO_KEY = lines[5].strip()
- return ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY
- ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY = load_credentials('credit.txt')
- #print(ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY)
- # Inicializace Wi-Fi připojení
- wlan = network.WLAN(network.STA_IF)
- #print("wlan = network.WLAN(network.STA_IF)")
- wlan.active(True)
- #print("wlan.active(True)")
- wlan.connect(ssid, password)
- #print("wlan.connect(ssid, password)")
- # Čekání na připojení
- max_attempts = 10
- attempts = 0
- while not wlan.isconnected() and attempts < max_attempts:
- print('Připojuji se k Wi-Fi...')
- time.sleep(1)
- attempts += 1
- if wlan.isconnected():
- print('Připojeno k Wi-Fi')
- print('IP adresa:', wlan.ifconfig()[0])
- print('IP adresa:', wlan.ifconfig())
- else:
- print('Nepodařilo se připojit k Wi-Fi')
- # Inicializace senzoru BMP180
- try:
- sensor = bmp180.BMP180(i2c)
- # print("zkouska senzoru")
- # sensor.oversample_sett = 2
- # sensor.baseline = 101325
- print("Senzor úspěšně inicializován")
- except OSError as e:
- print("Chyba při inicializaci senzoru:", e)
- # Funkce pro odesílání dat do Adafruit IO pomocí MQTT
- def send_data():
- temp = sensor.temperature
- pressure = sensor.pressure
- altitude = sensor.altitude
- # print("Teplota:", temp)
- # print("Tlak:", pressure)
- # print("Nadmořská výška:", altitude)
- client = MQTTClient("client_id", "io.adafruit.com", user=ADAFRUIT_AIO_USERNAME, password=ADAFRUIT_AIO_KEY)
- client.connect()
- client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/temperature", str(temp))
- time.sleep(1)
- client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/pressure", str(pressure))
- time.sleep(1)
- client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/altitude", str(altitude))
- time.sleep(1)
- client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/altitude2", str(altitude*8))
- client.disconnect()
- # Hlavní smyčka pro odesílání dat každých 900 sekund
- while True:
- send_data()
- time.sleep(900)
- # attempts = 0
- # while attempts < 20:
- # #print(attempts)
- # time.sleep(15)
- # attempts += 1
- MPY: soft reboot
- Připojuji se k Wi-Fi...
- Připojuji se k Wi-Fi...
- Připojuji se k Wi-Fi...
- Připojuji se k Wi-Fi...
- Připojeno k Wi-Fi
- IP adresa: 192.168.88.16
- IP adresa: ('192.168.88.16', '255.255.255.0', '192.168.88.1', '208.67.222.222')
- Senzor úspěšně inicializován
- Traceback (most recent call last):
- File "main.py", line 80, in <module>
- File "main.py", line 74, in send_data
- File "umqtt/simple.py", line 131, in publish
- OSError: [Errno 104] ECONNRESET
- MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico W with RP2040
- Type "help()" for more information.
- >>> help()
- Welcome to MicroPython!
- For online docs please visit http://docs.micropython.org/
- For access to the hardware use the 'machine' module. RP2 specific commands
- are in the 'rp2' module.
- Quick overview of some objects:
- machine.Pin(pin) -- get a pin, eg machine.Pin(0)
- machine.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p
- methods: init(..), value([v]), high(), low(), irq(handler)
- machine.ADC(pin) -- make an analog object from a pin
- methods: read_u16()
- machine.PWM(pin) -- make a PWM object from a pin
- methods: deinit(), freq([f]), duty_u16([d]), duty_ns([d])
- machine.I2C(id) -- create an I2C object (id=0,1)
- methods: readfrom(addr, buf, stop=True), writeto(addr, buf, stop=True)
- readfrom_mem(addr, memaddr, arg), writeto_mem(addr, memaddr, arg)
- machine.SPI(id, baudrate=1000000) -- create an SPI object (id=0,1)
- methods: read(nbytes, write=0x00), write(buf), write_readinto(wr_buf, rd_buf)
- machine.Timer(freq, callback) -- create a software timer object
- eg: machine.Timer(freq=1, callback=lambda t:print(t))
- Pins are numbered 0-29, and 26-29 have ADC capabilities
- Pin IO modes are: Pin.IN, Pin.OUT, Pin.ALT
- Pin pull modes are: Pin.PULL_UP, Pin.PULL_DOWN
- Useful control commands:
- CTRL-C -- interrupt a running program
- CTRL-D -- on a blank line, do a soft reset of the board
- CTRL-E -- on a blank line, enter paste mode
- For further help on a specific object, type help(obj)
- For a list of available modules, type help('modules')
Advertisement
Add Comment
Please, Sign In to add comment