Sending data to server using MQTT
-
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: -1Kindly help with this where am doing wrong. Please reply as soon as possible.
Thank You
[0_1524203120867_sub.py](Uploading 100%)
-
@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.
-
@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.
-
I did not understand clearly, ping fipy means , do i need to do
ping 192.168.4.1 into my computer??
-
@nazz ok, but do you have a valid network connection. Can you ping the fipy for instance.
-
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/mosquittoIn code i have given my computer ip address, but also not working.
-
@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.
-
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
-
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.
-
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: -1CODE:
from network import WLAN
from mqtt import MQTTClient
import machine
import time
from pysense import Pysense
from LTR329ALS01 import LTR329ALS01
import osdef settimeout(duration):
passwlan = 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