ptrvlck

rpipicow-1

Feb 3rd, 2025
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. po spusteni programu na raspberry pi pico W a pripojeni k wifi se jednou data odeslou a skonci chybou:
  2. import network
  3. import machine
  4. import time
  5. from machine import Pin, I2C
  6. import bmp180
  7. from umqtt.simple import MQTTClient
  8.  
  9. # Inicializace I2C
  10. i2c = I2C(0, scl=Pin(1), sda=Pin(0))
  11.  
  12. # Funkce pro načtení přihlašovacích údajů ze souboru
  13. def load_credentials(filename):
  14. with open(filename, 'r') as file:
  15. lines = file.readlines()
  16. ssid = lines[1].strip()
  17. password = lines[2].strip()
  18. ADAFRUIT_AIO_USERNAME = lines[4].strip()
  19. ADAFRUIT_AIO_KEY = lines[5].strip()
  20. return ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY
  21.  
  22. ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY = load_credentials('credit.txt')
  23.  
  24. #print(ssid, password, ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY)
  25. # Inicializace Wi-Fi připojení
  26. wlan = network.WLAN(network.STA_IF)
  27. #print("wlan = network.WLAN(network.STA_IF)")
  28. wlan.active(True)
  29. #print("wlan.active(True)")
  30. wlan.connect(ssid, password)
  31. #print("wlan.connect(ssid, password)")
  32. # Čekání na připojení
  33. max_attempts = 10
  34. attempts = 0
  35. while not wlan.isconnected() and attempts < max_attempts:
  36. print('Připojuji se k Wi-Fi...')
  37. time.sleep(1)
  38. attempts += 1
  39.  
  40. if wlan.isconnected():
  41. print('Připojeno k Wi-Fi')
  42. print('IP adresa:', wlan.ifconfig()[0])
  43. print('IP adresa:', wlan.ifconfig())
  44. else:
  45. print('Nepodařilo se připojit k Wi-Fi')
  46.  
  47. # Inicializace senzoru BMP180
  48. try:
  49. sensor = bmp180.BMP180(i2c)
  50. # print("zkouska senzoru")
  51. # sensor.oversample_sett = 2
  52. # sensor.baseline = 101325
  53. print("Senzor úspěšně inicializován")
  54. except OSError as e:
  55. print("Chyba při inicializaci senzoru:", e)
  56.  
  57. # Funkce pro odesílání dat do Adafruit IO pomocí MQTT
  58. def send_data():
  59. temp = sensor.temperature
  60. pressure = sensor.pressure
  61. altitude = sensor.altitude
  62. # print("Teplota:", temp)
  63. # print("Tlak:", pressure)
  64. # print("Nadmořská výška:", altitude)
  65.  
  66. client = MQTTClient("client_id", "io.adafruit.com", user=ADAFRUIT_AIO_USERNAME, password=ADAFRUIT_AIO_KEY)
  67. client.connect()
  68.  
  69. client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/temperature", str(temp))
  70. time.sleep(1)
  71. client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/pressure", str(pressure))
  72. time.sleep(1)
  73. client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/altitude", str(altitude))
  74. time.sleep(1)
  75. client.publish(f"{ADAFRUIT_AIO_USERNAME}/feeds/altitude2", str(altitude*8))
  76.  
  77. client.disconnect()
  78.  
  79. # Hlavní smyčka pro odesílání dat každých 900 sekund
  80. while True:
  81. send_data()
  82. time.sleep(900)
  83. # attempts = 0
  84. # while attempts < 20:
  85. # #print(attempts)
  86. # time.sleep(15)
  87. # attempts += 1
  88.  
  89.  
  90.  
  91. MPY: soft reboot
  92. Připojuji se k Wi-Fi...
  93. Připojuji se k Wi-Fi...
  94. Připojuji se k Wi-Fi...
  95. Připojuji se k Wi-Fi...
  96. Připojeno k Wi-Fi
  97. IP adresa: 192.168.88.16
  98. IP adresa: ('192.168.88.16', '255.255.255.0', '192.168.88.1', '208.67.222.222')
  99. Senzor úspěšně inicializován
  100. Traceback (most recent call last):
  101. File "main.py", line 80, in <module>
  102. File "main.py", line 74, in send_data
  103. File "umqtt/simple.py", line 131, in publish
  104. OSError: [Errno 104] ECONNRESET
  105. MicroPython v1.24.1 on 2024-11-29; Raspberry Pi Pico W with RP2040
  106. Type "help()" for more information.
  107. >>> help()
  108. Welcome to MicroPython!
  109.  
  110. For online docs please visit http://docs.micropython.org/
  111.  
  112. For access to the hardware use the 'machine' module. RP2 specific commands
  113. are in the 'rp2' module.
  114.  
  115. Quick overview of some objects:
  116. machine.Pin(pin) -- get a pin, eg machine.Pin(0)
  117. machine.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p
  118. methods: init(..), value([v]), high(), low(), irq(handler)
  119. machine.ADC(pin) -- make an analog object from a pin
  120. methods: read_u16()
  121. machine.PWM(pin) -- make a PWM object from a pin
  122. methods: deinit(), freq([f]), duty_u16([d]), duty_ns([d])
  123. machine.I2C(id) -- create an I2C object (id=0,1)
  124. methods: readfrom(addr, buf, stop=True), writeto(addr, buf, stop=True)
  125. readfrom_mem(addr, memaddr, arg), writeto_mem(addr, memaddr, arg)
  126. machine.SPI(id, baudrate=1000000) -- create an SPI object (id=0,1)
  127. methods: read(nbytes, write=0x00), write(buf), write_readinto(wr_buf, rd_buf)
  128. machine.Timer(freq, callback) -- create a software timer object
  129. eg: machine.Timer(freq=1, callback=lambda t:print(t))
  130.  
  131. Pins are numbered 0-29, and 26-29 have ADC capabilities
  132. Pin IO modes are: Pin.IN, Pin.OUT, Pin.ALT
  133. Pin pull modes are: Pin.PULL_UP, Pin.PULL_DOWN
  134.  
  135. Useful control commands:
  136. CTRL-C -- interrupt a running program
  137. CTRL-D -- on a blank line, do a soft reset of the board
  138. CTRL-E -- on a blank line, enter paste mode
  139.  
  140. For further help on a specific object, type help(obj)
  141. For a list of available modules, type help('modules')
Advertisement
Add Comment
Please, Sign In to add comment