PYCOM FiPy does not attach to the Cellular Network - LTE Italy - micropython

I have tried to connect to the internet using LTE but the network won't attach.
I want to determine the proper carrier on my Fipy, I did not get any response, the list was empty.
The comand AT+CEREG?' response OK.
The firmware version that I have is LR5.1.1.0-47510
Does someone have any idea on how I could manage to attach to the network? Any previous experiences Thank you in advance.
`>>> from network import LTE
>>> lte=LTE()
>>> lte.send_at_cmd("AT+SQNCTM=?")
''
>>>
I have been running all variations of the default test program with any success
from network import LTE
import time
import socket
lte = LTE()
lte.attach()
print("attaching..",end='')
while not lte.isattached():
time.sleep(0.25)
print('.',end='')
print(lte.send_at_cmd('AT!="fsm"')) # get the System FSM
print("attached!")
lte.connect()
print("connecting [##",end='')
while not lte.isconnected():
time.sleep(0.25)
print('#',end='')
#print(lte.send_at_cmd('AT!="showphy"'))
print(lte.send_at_cmd('AT!="fsm"'))
print("] connected!")

Related

OSError micropython

I get OSError on esp8266. First request is successful but second and more are failed with OSError, I dont know why. Can you help me?
Edit: I solved it. I wrote the solution at the end of the codes.
import network
name, password="wifiname", "passwordd"
wlan = network.WLAN(network.STA_IF)
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect(name, password) # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
print('Wifi connected! My IP:', wlan.ifconfig()[0])
import urequests
import time
while 1:
try:
t1=time.time()
r=urequests.get('https://saitamatechnoo.web.app/')
t2=time.time()
print(r.status_code, 'Time:', t2-t1)
except OSError:
print('error')
Guys, I solved it. It works with socket and we should close every socket after using it.
For example:
r=urequests.get('https://saitamatechnoo.web.app/')
print(r.status_code)
r.close()

Modbus RTU on an Raspberry Pi 3b+ as an master

My task is to develop an automation system for testing the device.
I have at my disposal:
raspberry pi(3b+/4)
modbus modules: icp con m - 7051D, m - 7055D, m-7061D, and few others.
At first I want to understand, how I can implement modbus protocol on RPi, how does it work.
In order to do that, I've used pymodbus library on RPi, which was working as an master, and modbusMAT on an PC, for recieving and decoding instructions from RPi. They were connected via rs-485 - USB adapter.
I've written some simple code, for writing a coil.
The RPi is communciating with PC, but the problem is, that the output, doesn't make sense to me.
Here's the code:
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
client = ModbusClient(method = 'rtu', port='/dev/ttyUSB0', stopbits=1, bytesize = 8, parity='N', baurdrate = '9600', timeout=0.3)
connection = client.connect()
print (connection)
client.write_coils(1, 1, unit=0x01)
Screenshot from modbusMat console
The output:
[9C][84][8C][E4][FF]
[9C][08][8C][B4][FF]
[9C][84][8C][E4][FF]
[38][18][18][8C][CA]
The output is related to changes I've made parameters. From top to bottom:
address=1, values=1, unit=0x01
address=2, values=1, unit=0x01
address=1, values=2, unit=0x01
address=1, values=1, unit=0x02
How I understand is, that if I've set address to 1, then the addres byte on the console should be the same - [01], not [9C].
I've managed before to successfully send some instructions to the icp modules(turn on, turn off coil) from PC, via RS-485 but not from RPi.

ESP32 MicroPython SSL WebSocket server fail

I'm trying to set up a secure socket server on esp32 with micropython. I used/tried the latest bulid (esp32-idf3-20200117-v1.12-68-g3032ae115.bin) with a self-signed certificate.
I saw a lot of memory leak related problem with ssl.wrap_socket() on esp32/esp8266 but what I got is different:
mbedtls_ssl_handshake error: -4310
Traceback (most recent call last):
File "boot.py", line 100, in <module>
OSError: [Errno 5] EIO
and the connection fails of course. I try to connect from my laptop. The exact same code of client side works if I start a secure socket server on the laptop itself (127.0.0.1) so I suppose that the client side is OK and the problem is on the server side.
I could not find any solution for this problem yet. I tried certificate and key both in 'der' and 'pem' format the result is the same.
The toy server code I tried:
import esp
esp.osdebug(None)
import gc
gc.collect()
import usocket as socket
import ssl
import machine
import network
KEY_PATH = 'server_key.der'
CERT_PATH = 'server_cert.der'
ssid= "AP_local"
pw="passwd"
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.config(essid=ssid, password=pw, authmode=4) # set the ESSID of the access point
ap.ifconfig(('192.168.3.1', '255.25.255.0', '192.168.3.1', '8.8.8.8'))
ap.active(True)
with open(KEY_PATH, 'rb') as f:
key = f.read()
print(key)
print(" ")
with open(CERT_PATH, 'rb') as f:
cert = f.read()
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ad = ap.ifconfig()
addr = socket.getaddrinfo(ad[0], 8443)[0][-1]
s.bind(addr)
s.listen(5)
import gc
gc.collect()
while True:
print("ssl connection started")
cl, addr = s.accept()
scl = ssl.wrap_socket(cl, server_side=True, cert=cert, key=key)
print(gc.mem_free())
l = 0
while True:
req = scl.read(1024)
print(req)
if not req or b'\r\n' in req:
break
response = '\r\n'.join(['HTTP/1.1 200 OK',
'Content-Type: text/plain',
'OK',
'Connection: close', '\r\n'])
scl.write(response.encode("utf-8"))
scl.close()
I hope someone could help me with this, thanks!
OK, after spending a few days on searching and reading and testing I got a solution and a possible reason for this issue. I hope it will help others a little.
I want to put forward that I didn't dig into the source code of mbedtls so the reasoning is somewhat phenomenological.
First, I tried to connect from a python program with default settings which failed as shown above. Then I tried with the openssl cli, which also failed.
I tried with the example given at https://github.com/micropython/micropython/blob/master/examples/network/http_server_ssl.py and failed again but with different error codes.
Finally I found a very useful page:
https://tls.mbed.org/api/ssl_8h.html#a55ed67b6e414f9b381ff536d9ea6b9c0
which helped to understand where the problem occures.
Now depending on requirements different errors occured from -7900,-7780, -7380, -7d00.
It turned out that although in the documentation the cipher suit being used is automatically agreed during handshake but there is some bug in it or the implementation of some of them in micropython is different.
I didn't tested all the available ciphers on my laptop but e.g.: AES_256_GCM_SHA384
cipher works.
For me it is enough now.

Flask SocketIO disconnect on OS Date Change

I have a Web application that is using Flask-socketIO.
__init__.py
import flask
import flask_socketio
app = flask.Flask(__name__)
socketio = flask_socketio.SocketIO()
socketio.init_app(app, manage_session=False)
I have this in my manage.py
socketio.run(app, host='0.0.0.0', port=80)
I have this in my socket_IO.py
#WebApp.socketio.on('disconnect', namespace='/all')
def disconnect():
"""Socket disconnected."""
print('Disconnected')
If i now set the system clock ( Ubuntu 16.04 ) for example 1 day forward i get socket disconnect. I can set the clock backwards to whatever i want it does not effect the socket.
What i expect to happen is for the socket not to fire the disconnect function on a change in the system clock.
Does flask-socketIO use the system clock and because of that makes the socket disconnect if it's set forward and not backwards?
Thanks in advance

Get Device info through pymodbus

I need to get device information (device name, etc) through pymodbus.
Does anyone know how to accomplish that?
Thanks
If your modbus device supports the Device Information command via the encapsulated interface command (0x2b 0x0e), you can use the following to get the device information with pymodbus (Put in your correct addresses and such) :
>>> from pymodbus.client.sync import ModbusSerialClient as ModbusClient
>>> mc = ModbusClient(method='rtu', port='/dev/ttyACM1')
>>> mc.connect()
True
>>> from pymodbus import mei_message
>>> rq = mei_message.ReadDeviceInformationRequest(unit=5,read_code=0x03)
>>> rr = mc.execute(rq)
>>> rr.information
{0: ...}
If you are communicating through a serial port (usb-/comport) you can get a list of serial devices on Comports using list_ports from serial:
64bit python:
import serial.tools.list_ports as portlist
for port in portlist.comports():
print(port)
print(port.device)
Outputs:
COM10 - Serial USB-device (COM10)
USB VID:PID=15A2:0300 SER=6 LOCATION=1-4.3
For my Modbus device on Comport 10
32-bit python:
import serial.tools.list_ports as portlist
for port in portlist.comports():
print(port)
Outputs a tuple including device name, vendor id, product id etc.