Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Sending data to server using MQTT

    Scheduled Pinned Locked Moved FiPy
    mqttfipyservermosquitto
    10 Posts 2 Posters 2.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N Offline
      Nazz
      last edited by

      Hello Team,

      Am using MQTT to send sensor data to server, i have included mqtt.py library and am running the attached code in the atom but getting the following error,
      Traceback (most recent call last):
      File "<stdin>", line 23, in <module>
      File "mqtt.py", line 56, in connect
      OSError: -1

      Kindly help with this where am doing wrong. Please reply as soon as possible.

      Thank You
      [0_1524203120867_sub.py](Uploading 100%)

      N 1 Reply Last reply Reply Quote 0
      • N Offline
        Nazz @Nazz
        last edited by

        Hello,

        Sorry for posting again , this is the same issue , Am using MQTT to send sensor data to server, i have included mqtt.py library and am running the below code in the atom but getting the following error,

        Traceback (most recent call last):
        File "<stdin>", line 23, in <module>
        File "mqtt.py", line 56, in connect
        OSError: -1

        CODE:
        from network import WLAN
        from mqtt import MQTTClient
        import machine
        import time
        from pysense import Pysense
        from LTR329ALS01 import LTR329ALS01
        import os

        def settimeout(duration):
        pass

        wlan = WLAN(mode=WLAN.STA)
        wlan.antenna(WLAN.EXT_ANT)
        wlan.connect("ssid", auth=(WLAN.WPA2, "password"), timeout=5000)

        while not wlan.isconnected():
        machine.idle()

        print("Connected to Wifi\n")
        client = MQTTClient("fipy", "localhost", port=1883)
        client.settimeout = settimeout
        client.connect()

        while True:
        print("Sending ON")
        py = Pysense()
        lt = LTR329ALS01(py)
        print("Light (channel Blue lux, channel Red lux): " + str(lt.light()))
        client.publish("/lights", str(lt.light()))
        time.sleep(1)
        print("Battery voltage: " + str(py.read_battery_voltage()))
        print("Sending OFF")
        client.publish("/lights", str(py.read_battery_voltage()))
        time.sleep(1)
        client.disconnect()

        Kindly help with this where am doing wrong. Please reply as soon as possible.

        Thank You

        1 Reply Last reply Reply Quote 0
        • T Offline
          timh
          last edited by timh

          At the moment your MQTT connection is attempting to connect to itself. MQTTClient("fipy", "localhost", port=1883) unless you are running a broker on the FiPy which I doubt, you need to have the name or IP address of the MQTT server you wish to connect to.

          N 1 Reply Last reply Reply Quote 0
          • N Offline
            Nazz @timh
            last edited by

            @timh

            Thank you for your response, i dint get what does this line means " unless you are running a broker on the FiPy which I doubt" , means in case we are running subscriber code on fipy??

            And also in this line "MQTTClient("fipy", "localhost", port=1883)" , i have changed the localhost to respective ip address where i want to send the data, but still same error.

            Can you please help me where am going wrong.

            Thank You

            T 1 Reply Last reply Reply Quote 0
            • T Offline
              timh @Nazz
              last edited by

              @nazz have you confirmed that you have a valid network connection, (for instance can resolve DNS names) and the MQTT broker you are connecting to is listening on port 1883. Some are listening on 8883 and require TLS.

              N 1 Reply Last reply Reply Quote 0
              • N Offline
                Nazz @timh
                last edited by

                @timh

                Yes, right now am using my computer as broker and i have setup mosquitto in it which is listing on port 1883, below is the response
                [root@localhost /]# sudo netstat -tlnp | grep mosq
                tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 7918/mosquitto
                tcp6 0 0 :::1883 :::* LISTEN 7918/mosquitto

                In code i have given my computer ip address, but also not working.

                T 1 Reply Last reply Reply Quote 0
                • T Offline
                  timh @Nazz
                  last edited by

                  @nazz ok, but do you have a valid network connection. Can you ping the fipy for instance.

                  N 1 Reply Last reply Reply Quote 0
                  • N Offline
                    Nazz @timh
                    last edited by

                    @timh

                    I did not understand clearly, ping fipy means , do i need to do
                    ping 192.168.4.1 into my computer??

                    T 1 Reply Last reply Reply Quote 0
                    • T Offline
                      timh @Nazz
                      last edited by

                      @nazz you need stablish that your fipy is actually connected to the Wifi network, has got a valid IP/netmask etc from your DHCP server. These are basic network tests you should perform before thinking about problem with high level libraries like MQTT. Once you have established you have a working network connection then start looking at your code.

                      Another way of checking is trying to telnet or ftp to your FiPy from your PC.

                      N 1 Reply Last reply Reply Quote 0
                      • N Offline
                        Nazz @timh
                        last edited by

                        @timh
                        Ok, i have connected the fipy to our wifi network & it is connected successfully, and also checked the ip address address and ping fipy is working.

                        1 Reply Last reply Reply Quote 0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • 1 / 1
                        • First post
                          Last post

                        Pycom on Twitter

                        Pycom