MQTT.Simple Loses Connection to AWS IoT - mqtt

I have a Raspberry Pi Pico W that I am trying to get to communicate with AWS IoT, and after about 12-24 hours it seems to lose its connection. I have the keep alive set and I can see it's pinging the server. And then it suddenly stops. I'm going to add some code below to show the connection and stuff, but I'm baffled at this point (I am cross-posting this on other Raspberry Pi sites too). Any thoughts on why I can't keep the connection open?
import machine
import time
import ussl as ssl
from robust import MQTTClient
def sub_cb(btopic, bmsg):
global led
led = machine.Pin(15, machine.Pin.OUT)
topic = btopic.decode()
msg = bmsg.decode()
mqtt_message_json = ujson.loads(msg)
if 'desired' in mqtt_message_json['state']:
led_status_update = (mqtt_message_json['state']['desired']['led_status'])
if led.value() != str(led_status_update):
if led_status_update == '0':
print("turning off LED")
led.value(0)
elif led_status_update == '1':
print("turning on LED")
led.value(1)
DeviceID='PicoTestBed03'
PORT=8883
AWS_ENDPOINT={My AWS API End Point}
DISCONNECTED = 0
CONNECTING = 1
CONNECTED = 2
state = DISCONNECTED
KeepAliveSeconds = 60
led = machine.Pin(15, machine.Pin.OUT, value=1)
#Use Websockets
useWebsocket = False
#Create SSL Params object
#Assume the cert, key, and rootCA are all created correctly because my connection is successful
SSL_PARAMS = {'cert': cert, 'key': key, 'server_side': False, "cert_reqs":ssl.CERT_REQUIRED, 'cadata':rootCA}
#Create MQTT Client
client = MQTTClient(DeviceId, AWS_ENDPOINT, port=PORT, keepalive=KeepAliveSeconds, ssl=True, ssl_params=SSL_PARAMS)
#Connect MQTT Client
while state != CONNECTED:
try:
state = CONNECTING
print('AWS TEST: Trying to connect via MQTT...')
client.connect()
state = CONNECTED
except Exception as e:
print('AWS TEST: Could not establish MQTT connection')
continue
print('AWS TEST: MQTT LIVE!')
device_shadow_last_checked = time.localtime()
while 1:
if current_led_status != led.value():
current_led_status = led.value()
device_shadow_update_msg = b'{"state":{"reported":{"led_status":%d}}}' %(led.value())
mqtt_client.publish(led_status_shadow_topic_update, device_shadow_update_msg, qos=0)
if (time.mktime(time.localtime()) - time.mktime(device_shadow_last_checked)) >=60:
client.ping()
device_shadow_last_checked = time.localtime()
client.check_msg()

Related

gRPC streaming call which takes longer than 2 minutes is killed by hardware (routers, etc.) in between client and server

Grpc.Net client:
a gRpc client sends large amount of data to a gRpc server
after the gRpc server receives the data from the client, the http2 channel becomes idle (but is open) until the server returns the response to the client
the gRpc server receives the data and starts processing it. If the data processing takes longer than 2 minutes (which is the default idle timeout for http calls) then the response never reaches the client because the channel is actually disconnected, but the client does not know this because it was shutdown by other hardware in between due to long idle time.
Solution:
when the channel is created at the gRpc client side, it must have a httpClient set on it
the httpClient must be instantiated from a socketsHttpHandler with
the following properties set (PooledConnectionIdleTimeout, PooledConnectionLifetime, KeepAlivePingPolicy, KeepAlivePingTimeout, KeepAlivePingDelay)
Code snipped:
SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
{
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(180),
PooledConnectionLifetime = TimeSpan.FromMinutes(180),
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always,
KeepAlivePingTimeout = TimeSpan.FromSeconds(90),
KeepAlivePingDelay = TimeSpan.FromSeconds(90)
};
socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
HttpClient httpClient = new HttpClient(socketsHttpHandler);
httpClient.Timeout = TimeSpan.FromMinutes(180);
var channel = GrpcChannel.ForAddress(_agentServerURL, new GrpcChannelOptions
{
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials),
MaxReceiveMessageSize = null,
MaxSendMessageSize = null,
MaxRetryAttempts = null,
MaxRetryBufferPerCallSize = null,
MaxRetryBufferSize = null,
HttpClient = httpClient
});
A workaround is to package your message in an oneof and then send a KeepAlive from a seperate thread every x seconds, for the duration of the calculations.
For example:
message YourData {
…
}
message KeepAlive {}
message DataStreamPacket {
oneof data {
YourData data = 1;
KeepAlive ka = 2;
}
}
Then in your code:
stream <-
StartThread() {
each 5 seconds:
Send KeepAlive
}
doCalculations()
StopThread()
SendData()
this is what I needed. I had this problem for months now, but my only solution was to decrease the volume of data.

MQTT Broker does not deliver the messages sent by publisher, on time

I wrote this MQTT publisher code:
import paho.mqtt.client as mqtt
import time
HOST = "localhost"
PORT = 1883
KEEP_ALIVE_INT = 100
TOPIC = "noti"
def sendMsg():
MSG = ["1111", "2222", "3333", "4444", "5555"]
i = 0
try:
while i<5:
client.publish(TOPIC, MSG[i], qos=0)
i+=1
time.sleep(1)
except Exception as e:
print("Caught Exception: " + e)
def onConnect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully")
sendMsg()
else:
print("Connection failed, result code: " + str(rc))
def onPublish(client, userdata, mid):
print ("Message is published")
client = mqtt.Client("pub")
client.on_connect = onConnect
client.on_publish = onPublish
client.connect(HOST, PORT, KEEP_ALIVE_INT)
client.loop_forever()
And, the following is the MQTT subscriber code:
import paho.mqtt.client as mqtt
import time
HOST = "localhost"
PORT = 1883
KEEP_ALIVE_INT = 100
TOPIC = "noti"
def onConnect(client, userdata, flags, rc):
if rc == 0:
print("=> Connected successfully")
client.subscribe(TOPIC, 0)
else:
print("=> Connection failed, result code: " + str(rc))
def onSubscribe(mosq, obj, mid, granted_qos):
print ("=> Subscribed to topic: " + TOPIC)
print ("Granted QOS: "+str(granted_qos))
def onMessage(client, userdata, msg):
print("=> Received message: " + msg.topic +" - " + msg.payload.decode("utf-8"))
client = mqtt.Client("sub")
client.on_message = onMessage
client.on_connect = onConnect
client.on_subscribe = onSubscribe
client.connect(HOST, PORT, KEEP_ALIVE_INT )
client.loop_forever()
I am using Mosquitto broker in my PC.
The publish is done in every 1 second, but I can see the print "Message is published" 5 times after all 5 messages are published. Also, the subscriber receives the messages together after 5 seconds, not in every 1 second.
Please help me understand the mistake, suggest modification.
This is because all callbacks and message handling happen on the client network loop thread and you are blocking that thread by not returning from the on_connect() callback.
So the calls to client.publish() are queued up until the on_connect() callback returns.
You need to find a way to trigger the sendMsg() function not on the client loop. (Probably on a separate thread)

Issues with controlling picamera using MQTT messages

I am trying to start a camera recording using Pi Zero W (which acts as MQTT client) upon receiving message and stop the recording on receiving the stop message. Below is my code:
continueRecording = 1
Broker = "192.168.0.105"
pub_topic = "picamera1"
sub_topics = ["Rpi_Master", 0]
def on_connect(client, userdata, flags, rc):
if rc == 0:
pass
else:
print("Bad Connection with result code: " + str(rc))
for topic in sub_topics:
client.subscribe(topic)
def on_message(client, userdata, msg):
global message_topic, message
global continueRecording
message = str(msg.payload.decode("utf-8"))
print("Received message is" + message)
message_start = str(message[:4])
print("Command to start recording is " + message_start)
if message_start == "shop":
print("Enter shop")
with picamera.PiCamera as camera:
camera.resolution = (640, 480)
camera.framerate=20
camera.start_recording("/home/pi/camera-recording/shop/shoprecording.h264")
time.sleep(0.5)
while continueRecording == 1:
camera.wait_recording(.01)
if message == "OK":
print("Stopping to record")
camera.stop_recording()
continueRecording = 0
def on_publish(mosq, obj, mid):
pass
# on mqtt disconnection#
def on_disconnect(client, userdata, rc):
if rc == 0:
pass
elif rc != 0:
print("Unexpected MQTT disconnection. Will try to reconnect")
try:
client.username_pw_set(username="ab", password="abcdef")
client.connect(Broker, 1883, 60)
except:
print("Error in trying to reconnect with the Broker")
# mqtt client broker Connection
def clientBrokerConnection():
print("Client Broker Function Running")
global client
client = mqtt.Client("piCamera1") # creating a new instance
##Defining the callback functions
client.username_pw_set(username="pi", password="lotus56789")
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.on_disconnect = on_disconnect
##End of callback functions
client.connect(Broker, 1883, 60) # Connecting to Broker
client.loop_start()
clientBrokerConnection()
The issue I am facing is that the Pi is recieving the correct message to start recording but it fails to enter the with picamera.PiCamera as camera: loop and the recording doesn't start. The compiler does not show any error in the code. I am unable understand why the recording doesn't start. I have checked the camera and it works fine. Thanks for your help and time in advance.

Having problem connecting to my mqtt broker using CocoaMQTT

I am making one iOS app communicating with Mqtt broker, mainly to publish message. But when I try to connect with broker using CocoaMQTT library it's always giving me error in connection.
I am trying with CocoaMQTT latest version and also 1.1.3 version. But both are failing in connection and giving me error
(Error Domain=kCFStreamErrorDomainNetDB Code=8 "nodename nor servname
provided, or not known" UserInfo={NSLocalizedDescription=nodename nor
servname provided, or not known})
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
let dateString = formatter.string(from: date)
let clientID = "smart-curtain-"+dateString
mqttClient = CocoaMQTT.init(clientID: clientID, host:
contantData.MQTT_BROKER_URL, port: UInt16(1883))
mqttClient.username = nil
mqttClient.password = nil
mqttClient.autoReconnect = true
mqttClient.allowUntrustCACertificate = true
mqttClient.keepAlive = 60
mqttClient.enableSSL = false
So its always ending up withmqttDidDisconnect delegate method. My broker url is tcp://xyz.com (xyz is just example) and port is 1883. I have tried 2-3 Mqtt toll apps from my iPhone to connect with broker detail, but no one able to connect it.
But same settings working fine in Android app. (it is using net.igenius:mqttservice:1.6.4) (this broker is no need authentication)
As shown in the CocoaMQTT doc, the host entry in the init method should be just the hostname, not a URI:
let clientID = "CocoaMQTT-" + String(NSProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()
e.g. should be xyz.com not tcp://xyz.com
var session = CocoaMQTT.init(clientID: "user1", host: "xx.xx.xxx.xx", port: 1883)
session.allowUntrustCACertificate = true
No need to send tcp://xx.xx.xxx.xx:port as in android, You can just pass xx.xx.xxx.xx by removing tcp:// and port number separately.

Why this f# echo server can't be connected by many clients?

I write this echo server:
let listener=new TcpListener(IPAddress.Parse("127.0.0.1"),2000)
let rec loop (client : TcpClient,sr : StreamReader, sw : StreamWriter) =
async {
let line=sr.ReadLine()
sw.WriteLine(line)
if line="quit" then
client.Close()
else
return! loop(client,sr,sw)
}
let private startLoop (listener:TcpListener) =
while true do
let client = listener.AcceptTcpClient()
let stream = client.GetStream()
let sr = new StreamReader(stream)
let sw = new StreamWriter(stream)
sw.AutoFlush <- true
sw.WriteLine("welcome")
Async.Start(loop (client,sr,sw))
[<EntryPoint>]
let main argv =
listener.Start()
startLoop(listener)
0
when I open one or two telnet window to test it,it works fine
but when I write this test program to test it:
static void Main(string[] args)
{
for (int a = 0; a < 5; a++)
{
var client = new TcpClient("localhost", 2000);
Console.WriteLine(client.Connected);
client.close();
}
}
the test program return one or two true,but the server raise an exception:
System.Net.Sockets.SocketException:An existing connection was forcibly closed by the remote host
in line 12:let line=sr.ReadLine()
and client raise the exception:System.Net.Sockets.SocketException:Because the target computer actively refused, unable to connect
at line 16:var client = new TcpClient("localhost", 2000);
I don't know why,please help me
Your problem is that the client opens a connection and then immediately closes it.
The server however expects a "quit" message from the client before it will terminate the connection. So the server sends a "welcome" to the client, then enters the loop. Inside the loop, sr.ReadLine() is called, which waits for the client to send something over the wire.
The client never sends anything. It closes the connection. Therefore, the server's call to ReadLine aborts with the a SocketException (forcibly closed...). And you do not handle this exception, so the server dies.
Then the client tries to connect once again, with no server listening anymore. The client can't connect and you see another SocketException (actively refused...).
You should guard your server code against clients that disconnect without saying "quit" first.

Resources