I'm using paho.mqtt.python in python 2.7 and when I try to send a file (image), I see the next error:
Traceback (most recent call last):
File "/home/pi/Desktop/Device1/Scripts/mqtt_publish.py", line 39, in
mqttc.publish(MQTT_TOPIC,byteArr,0,True)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 980, in publish
rc = self._send_publish(local_mid, topic, local_payload, qos, retain, False, info)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1988, in _send_publish
upayload = payload.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
The publish script is the next:
# Import package
import paho.mqtt.client as mqtt
# Define Variables
MQTT_HOST = "192.168.1.39" #iot.eclipse.org
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "Device1"
#MQTT_MSG = 25
def on_connect(mqttc, userdata, flags, rc):
#Subscribe to a Topic
mqttc.subscribe(MQTT_TOPIC, 0)
print("Connection returned result: "+connack_string(rc))
# Define on_publish event function
def on_publish(mqttc, userdata, mid):
print "Message Published..."
print("mid: " +str(mid))
mqttc.disconect()
# Initiate MQTT Client
mqttc = mqtt.Client(client_id="LCESS", clean_session=False)
# Register publish callback function
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect
# Connect with MQTT Broker
# probar mqttc.username_pw_set(username, password)
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Publish message to MQTT Broker
f= open("/home/pi/Desktop/images/image.jpg")
filecontent = f.read()
byteArr = bytes(filecontent)
mqttc.publish(MQTT_TOPIC,byteArr,0,True)
The solution is the following:
replace:
byteArr = bytes(filecontent)
to
byteArr = bytearray(filecontent)
and replace:
connack_string(rc)
to
mqtt.connack_string(rc)
Because I've imported the module: import paho.mqtt.client as mqtt
Error using: bytes(filecontent).decode()
Traceback (most recent call last):
File "/home/pi/Desktop/Device1/Scripts/mqtt_publish.py", line 37, in <module>
byteArr = bytes(filecontent).decode()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
Related
I'm trying to send an email using python but everytime I try to send, I'm getting UnicodeDecodeError. Where am I doing wrong? I couldn't figure out by searching on web.
Here's my full code:
import smtplib
import random
import datetime
# Random name list
names = ['Jack', 'Kevin', 'Laura']
# Datetime get time
date = datetime.datetime.now().strftime('%a, %b %d')
# Random order id's
r0 = random.randint(702, 711)
r1 = random.randint(1111111, 9999999)
r2 = random.randint(1111111, 9999999)
r3 = round(random.uniform(13, 25), 4)
# Mail content
mailc = """Hi {}! You did it buddy: #{}
Service: Standard
*Profit: S$ {}
Send it no later than {}...
""".format(random.choice(names), {r0}-{r1}-{r2}, {r3}, date)
# Subject
sub = '(.LO) New toy: {}-{}-{}'.format({r0}, {r1}, {r2})
# Mail adreesses and password
senderAddress = 'xxx#gmail.com'
senderPass = 'xxyyww'
receiverAddress = 'yyy#gmail.com'
# Server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(senderAddress, senderPass)
# Message
message = 'Subject: {}\n\n{}'.format(sub, mailc)
server.sendmail(senderAddress, receiverAddress, message)
server.quit()
print("Mail sent.")
And this is the error:
Traceback (most recent call last):
File "C:\Users\WINDOWS 8.1\Desktop\automaticOrderEmailSender.py", line 35, in
<module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Users\WINDOWS\AppData\Local\Programs\Python\Python37-32\lib\smtpl
ib.py", line 261, in __init__
fqdn = socket.getfqdn()
File "C:\Users\WINDOWS\AppData\Local\Programs\Python\Python37-32\lib\socke
t.py", line 676, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 1: invalid
continuation byte
Hope you can help.
(ERROR MODULE) python mqtt_Listen_Sensor_Data.py
Previously I had a problem with localhost but now im getting another error. I'm trying to store my MQTT data in SQLITE database on raspberry. What am I doing wrong to get the error below?
CODE:
import paho.mqtt.client as mqtt
from store_Sensor_Data_to_DB import sensor_Data_Handler
# MQTT Settings
MQTT_Broker = "localhost"
MQTT_Port = 1883
Keep_Alive_Interval = 45
MQTT_Topic = "kuca/primanje/kanta01/r"
#Subscribe to all Sensors at Base Topic
def on_connect(mosq, obj, rc):
self.subscribe(MQTT_Topic,0)
#Save Data into DB Table
def on_message(mosq, obj, msg):
self.subscribe(MQTT_Topic, 0)
# This is the Master Call for saving MQTT Data into DB
# For details of "sensor_Data_Handler" function please refer "sensor_data_to_db.py"
print "MQTT Data Received..."
print "MQTT Topic: " + msg.topic
print "Data: " + msg.payload
sensor_Data_Handler(msg.topic, msg.payload)
def on_subscribe(mosq, obj, mid, granted_qos):
pass
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
# Connect
mqttc.connect(MQTT_Broker, int(MQTT_Port), int(Keep_Alive_Interval))
# Continue the network loop
mqttc.loop_forever()
ERROR:
pi#Pi:~/Desktop/SQLITE $ python mqtt_Listen_Sensor_Data.py
Traceback (most recent call last):
File "mqtt_Listen_Sensor_Data.py", line 46, in <module>
mqttc.loop_forever()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1481, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1003, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1284, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1849, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2311, in _packet_handle
return self._handle_connack()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2372, in _handle_connack
self.on_connect(self, self._userdata, flags_dict, result)
TypeError: on_connect() takes exactly 3 arguments (4 given)
I am really sorry for having troubled you so much.
Your problem resides here: you define a function, and address the self without actually passing it:
#Subscribe to all Sensors at Base Topic
def on_connect(mosq, obj, rc):
self.subscribe(MQTT_Topic,0)
Then you create an object, and assign your function as an attribute of that object:
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
Your on_connect function isn't an instance method.
When you call an instance method, the self variable is passed as first argument.
So when you call mgttc.on_connect(x, y, z) what actually executed is mgttc.on_connect(self, x, y, z).
Your on_connect() callback must have this signature (from paho python doc) :
def on_connect(client, userdata, flags, rc):
If you want to use it in another class you can use this signature :
def on_connect(self, client, userdata, flags, rc):
Furthermore, variable self is undefined in the on_connect() and on_message() callbacks, for example :
self.subscribe(MQTT_Topic,0)
must be
mqttc.subscribte(MQTT_Topic, 0)
Hope this will help you
I am trying to publish a multiple random data using mqtt to the broker. Below is the script for the publish part.
import paho.mqtt.client as mqtt
import json, schedule, time, random
client = mqtt.Client()
client.connect("<broker address", 1883, 60)
def pub_message():
tempreading = random.uniform(0, 100)
pHreading = random.uniform(1,14)
oxyreading = random.uniform(0, 100)
data_string1 = str(oxyreading)
data_string2 = str(pHreading)
data_string3 = str(tempreading)
msgs = [("randomdata", data_string1),("randomdata", data_string2),("randomdata", data_string3)]
client.publish(msgs)
schedule.every(1).minutes.do(pub_message)
while True:
schedule.run_pending()
time.sleep(1)
client.disconnect()
I ran the script and there is error like below:
Traceback (most recent call last):
File "mqttpub.py", line 27, in <module>
schedule.run_pending()
File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 462, in run_pending
default_scheduler.run_pending()
File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 75, in run_pending
self._run_job(job)
File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 129, in _run_job
ret = job.run()
File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 377, in run
ret = self.job_func()
File "mqttpub.py", line 22, in pub_message
client.publish(msgs)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 980, in publish
rc = self._send_publish(local_mid, topic, local_payload, qos, retain, False, info)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1979, in _send_publish
utopic = topic.encode('utf-8')
AttributeError: 'list' object has no attribute 'encode'
I searched about the publish multiple message with mqtt but did not find any good reference. I also included my mqtt subscribe part for receiving the multiple messages. I did search about this part too but did not find any good reference.
import paho.mqtt.client as mqtt
from models import *
from sqlalchemy.orm import sessionmaker
import json
def on_connect(client, userdata, rc):
print("connected with result code" + str(rc))
client.subscribe("randomdata")
def on_message(client, userdata, msg):
print "Topic:", msg.topic + " " + "Message:" + " " + "Value1:" + str(msg.payload1) + " " + "Value2:" + str(msg.payload2) + " " + "Value3:" + str(msg.payload3)
engine = create_engine('postgresql://user:password#localhost/mydatabase')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# store message received into database
raw_data = _Data(Value1=msg.payload1, Value2=msg.payload2, Value3=msg.payload3, time=msg.timestamp)
session.add(raw_data)
session.commit()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("<broker address>",1883, 60)
client.loop_forever()
Does anyone have any experience doing it? Thank you in advance.
What makes you think client.publish() will accept an array?
The doc's (https://pypi.python.org/pypi/paho-mqtt/1.1#publishing) don't mention anything about publishing multiple messages, you will have to call client.publish() once for every message you want to send.
You should also be calling client.loop() in your while true loop.
while True:
schedule.run_pending()
client.loop()
time.sleep(1)
I'm coding with Python on Raspberry pi 2.
I'm trying to send several orders to two devices by bluetooth at the same time but it doesn't work.
According to the error message, when I send an order to the second device, I can't decode the response. I already tried to encode with 'UTF-8' but it didn't work either...
Here is the error message:
Traceback (most recent call last):
File "/home/pi/Desktop/Bluetooth-master/Bluetooth-master/rfcommcli.py", line 60, in <module>
StartBTClient()
File "/home/pi/Desktop/Bluetooth-master/Bluetooth-master/rfcommcli.py", line 54, in StartBTClient
print('reception 2 : ', rec2.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 6: unexpected end of data
So can you help me, please.
Here is the code :
import bluetooth
class BT(object):
address_2 = ('00:04:3E:93:39:A9')
address_1 = ('00:04:3E:6A:10:A9')
def __init__(self):
self.btSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
def __exit__(self):
self.Disconnect()
def Connect(self, mac, port=1):
self.btSocket.connect((mac, port))
print('Connecter')
def Disconnect(self):
try:
self.btSocket.close()
except Exception:
pass
def Send(self, data):
self.btSocket.send(data.encode())
def Receive(self, size=1024):
return self.btSocket.recv(size)
def StartBTClient():
cli = BT()
print('BT1 Connexion en cours ...')
#cli.Discover()
cli.Connect(cli.address_1, 0o01)
cli2 = BT()
print('BT2 Connexion en cours ...')
#cli.Discover()
cli2.Connect(cli2.address_2, 0o02)
print('Donner un ordre ... (ordre shutter)')
while True:
data = input()
if (data == 'exit'):
break
cli.Send("read\r")
cli2.Send("read\r")
rec = cli.Receive()
rec2 = cli2.Receive()
print('reception 1 : ', rec.decode())
print('reception 2 : ', rec2.decode())
cli.Disconnect()
cli2.Disconnect()
if __name__ == '__main__':
StartBTClient()
Apparently device #2 is responding with some, but not all, of the bytes of a UTF-8 encoded value.
You need to ensure that you have received and assembled all of the bytes of the message before you attempt to decode them. This may involve calling .recv() multiple times.
I am writing a multi-threaded client/server program. It splits a large file into smaller files in its client side and sends the smaller files to the server concurrently.
The problem is that in every run, the server can only receive two of the smaller files (the first one and another random one). Meanwhile, I encounter the error: "[Errno 32] Broken pipe" in client side of the program in s.sendall(part). The error arises in every thread that starts to send one of the smaller files before reception of the first file (on the server). In other words, every thread that starts to send after the reception the first file (on the server) can complete its task.
I run each of the client and server codes on different computers (both have the following specification: Ubuntu 14.04 desktop, 64 bit, 16GiB ram)
Client side Error:
Traceback (most recent call last):
File "Desktop/File_transmission/Client.py", line 56, in sendSplittedFile
s.sendall(part)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe
Client.py:
import random
import socket
import time
import threading
import errno
import select
import File_manipulation
import sys, traceback
class Client:
nodesIpAddr = ["....", "...."] #Server = ....
dataPort = 45678
delay = 2
inputFileAddress = 'tosend.dat'
fileOutputPrefix = 'output'
fileOutputSuffix = ".dat"
bufferSize = 2048
max_size_splitted_file = 10*(2**20) # 10 MiB
def __init__ (self, my_ip):
self.ip = my_ip
def send(self, ip_toSend, dataPort):
print "\tSend function is runing."
totalNumSplittedFile = File_manipulation.split_file(Client.inputFileAddress, Client.fileOutputPrefix, Client.max_size_splitted_file , Client.bufferSize)
for i in range(0, totalNumSplittedFile):
thread_send = threading.Thread(target = self.sendSplittedFile, args = (ip_toSend, dataPort, Client.bufferSize, i, Client.fileOutputPrefix, totalNumSplittedFile))
thread_send.start()
def sendSplittedFile(self, ip_toSend, dataPort, bufferSize, fileNumber, fileNamePrefix, totalNumSplittedFile):
# Create a socket (SOCK_STREAM means a TCP socket)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
BUFFER_SIZE = bufferSize
try:
s.connect((ip_toSend, dataPort))
f = open(fileNamePrefix + '.%s' % fileNumber,'rb')
s.send(str(fileNumber) + " " + str(totalNumSplittedFile))
part = f.read(BUFFER_SIZE)
while (part):
s.sendall(part)
part = f.read(BUFFER_SIZE)
f.close()
s.sendall(part)
time.sleep(Client.delay)
s.sendall('EOF')
print "Done Sending."
print s.recv(BUFFER_SIZE)
s.close()
print "\tData is sent to ", ip_toSend,
except socket.error, v:
traceback.print_exception(*sys.exc_info())
s.close()
nodeIP = [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
n = Client(nodeIP)
n.send(n.nodesIpAddr[0], n.dataPort)
Server Side Error:
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "Desktop/File_transmissionServer.py", line 37, in handle
totalFileNumber = int(details[1])
ValueError: null byte in argument for int()
Server.py
import socket
import time
import threading
import errno
import select
import SocketServer
import File_manipulation
class ServerThreadHandler(SocketServer.BaseRequestHandler):
nodesIpAddr = ["....", "...."] #Server = ....
fileOutputPrefix = 'torec '
fileOutputSuffix = '.dat'
dataPort = 45678
delay = 3
maxNumClientListenedTo = 200
timeout_in_seconds = 5
bufferSize = 2048
totalFileNumber = 0 #Total number of splitted files. It should be set by the incoming packets
def handle(self):
BUFFER_SIZE = ServerThreadHandler.bufferSize # Normally 1024, but we want fast response
# self.request is the TCP socket connected to the client
data = self.request.recv(BUFFER_SIZE)
addr = self.client_address[0]
details = str(data).split()
currentFileNum = int(details[0])
totalFileNumber = int(details[1])
print '\tReceive: Connection address:', addr,'Current File Number: ', currentFileNum, 'Total Number of splitted files: ', totalFileNumber
f = open(ServerThreadHandler.fileOutputPrefix + '_Received.%s' % currentFileNum, 'wb')
data = self.request.recv(BUFFER_SIZE)
while (data and data != 'EOF'):
f.write(data)
data = self.request.recv(BUFFER_SIZE)
f.close()
print "Done Receiving." ," File Number: ", currentFileNum
self.request.sendall('\tThank you for data. File Number: ' + str(currentFileNum))
if __name__ == "__main__":
HOST, PORT = ServerThreadHandler.nodesIpAddr[0], ServerThreadHandler.dataPort # HOST = "localhost"
server = SocketServer.TCPServer((HOST, PORT), ServerThreadHandler)
# Activate the server; this will keep running until you interrupt the program with Ctrl-C
server.serve_forever()