How do I fix Unknown Format? - python

This is my first time trying out to code on C# or VisualStudio.
Sorry if I am too much of a newbie.
I have a raspberry pi zero setup as a BLE GATT server, which I followed exactly with this guide.
My ultimate goal is just trying to send a text from the pi zero to my laptop.
Being a little familiar with python, I have tried to use Bleak to communicate with the GATT server but for some reason, I'm never able to connect to the GATT server. So I gave up trying to tinker with Bleak.
( Please note that I have used nRF Toolbox to verify that the pi zero BLE GATT server is set up correctly )
Fast foward, one day I saw this BLE GATT Client Sample from Microsoft. So I downloaded VisualStudio and followed the guide to deploy the sample.
Everything worked fine, I can connect to the GATT server and write data to the specific RX characteristics.
But the real problem resides on the TX characteristics, when I try to send data from the GATT server console to the laptop. The BLE Client Sample will show the value as "Unknown Format"
It seems that the data will be encoded ( value.append(dbus.Byte(c.encode())) ) with this before updating the TX charactersitics.
I tried to Google about this, but any further research will only make my head burn harder.
I'm sorry, can anyone tell me how to fix this "Unknown Format" data decode problem?
Thanks so much in advance.

On the server (RPi) you have:
UART_TX_CHARACTERISTIC_UUID = '6e400003-b5a3-f393-e0a9-e50e24dcca9e' as 'notify'
UART_RX_CHARACTERISTIC_UUID = '6e400002-b5a3-f393-e0a9-e50e24dcca9e' is 'write'
This means on the client (PC) you need 0003 to be 'write' and 0002 to be 'notify'.
You say the problem is when the server does a write to 0002 you are get unknown format?
BLE will always send the data as a byte array, I suspect you need to do something like this previous answer on the client for the data you receive

Related

Serial Communication through Bluetooth ble

I am trying to communicate with my battery that has a ble Bluetooth module on it. the goal is to be able to connect to it and then communicate via serial communication by sending bytes back and forth
for example, I have a read and write uuid for it and some byte lines to send to it that it should send back bytes that I can interpret to read voltage temperature and current. like if I send the battery this EA D1 01 04 FF 02 F9 F5 in bytes it should send a byte line very similar back that tells me what the voltage is for the 4 different cells in the battery.
the things that I have are a service uuid and those bytes line commands.
I have tried using bleak as I have had some experience in this but for whatever reason it would not connect, I would get timeout errors or an os error.
I haven't been able to manually connect the Bluetooth with my Bluetooth settings on the desktop, so I haven't been able to setup a com port
I have been able to connect to it with a Arduino nano 33 but haven't been able to send and receive serial communication
I have been able to do this successfully with a Serial Bluetooth terminal on my android device and it works very nicely on that, but I would like to get it on my desktop as it would help me out a lot to automate getting that information and using it.
any help is greatly appreciated and i look forward to possibly communicating with some of you. feel free to ask for any addition information as maybe i was unclear with some things
Welcome to Stack Overflow. You want to become familiar with Bluetooth and need to understand that Bluetooth classic and Bluetooth LE are different standards that are incompatible to each other.
So while the Serial Bluetooth terminal probably uses the classic standard with SPP, other things you've tried seem to work with Bluetooth LE. The battery, for example, seems to work with Bluetooth LE and the GATT service, as indicated by the service UUID.
All in all, it looks to me as if you should first familiarise yourself with the technology before you start writing your program.

how to connect the Raspberrypi to internet with Sim7020E using 1NCE IoT FLAT

hello I work with an Rpi 4 and an Rpi Zero W and I want to activate the internet connection by a communication network using SIM7020E. I manage to establish the contact by checking the basic commands on Minicom but the connection is not activated. I must provide at the end transfer data from my Rpi by Email but without Wifi or Ethernet Cable.
i program with python
please can someone help me?
I would recommend checking out the 1NCE Developer Hub.
In the recipes section are examples of using the SIM7000G, which works very much in the same way and can be compared. You find all the recipes here: https://help.1nce.com/dev-hub/recipes and look for SIM7000G.
The main things you need to do is to configure the SIM7020E via the serial interface and send the correct commands from your python application.
Do the overall network registration of the module as shown here: https://help.1nce.com/dev-hub/recipes/sim7000g-network-registration
From there, you can already start sending data. Unfortunately, there is not a direct example for sending mail but as I expect you are using some mail delivery service that also has an API, you can follow the following guide for that: https://help.1nce.com/dev-hub/recipes/sim7000g-http-post
It will show you what commands are required for an HTTP POST call.

Accessing IOT raspberry pi outside of network using flask with Gunicorn

I’ve looked through this forum and couldn’t find a clear answer to helping my situation.
To explain: I’m currently trying to develop an IOT device that mainly consists of a raspberry pi 3. As it’s for a start up business idea I want to reduce costs and not have a server on the cloud.
I thought it would be a good idea to have flask run with gunicorn on the pi and this would be accessed by a desktop app or mobile app that would know the IP address of the pi as well as it’s flask endpoints.
I wanted the pi to be accessible from any other network (not just its local one). I have host on “0.0.0.0” and port 5000 for development but can’t access it through my mobile network.
I have seen similar posts mentioning ngrok (which might make data less secure?). I’ve seen that I might have to forward the port of the pi to the router. But I think this might involve accessing the router set up. And I don’t want to have to do this for every new client.
I have checked other posts on the forum but can’t seem to find what I’m looking for.
Can I ask this forum if my understanding is correct? Can anyone help me out?
Any advice would be greatly appreciated!
If I understood the problem correctly,
If you want to connect to your raspberry pi remotely you should know the IP address of your raspberry pi and need port forwarding. After that, you can connect to your raspberry pi with a public ip address. For example publicraspberrypiaddress:5555 (which port are you using)
Ngrok gives a random subdomain for connection so I don't know how you recognize the ngrok url when device is not near of you.
Alternatively, you can use cotunnel. It gives static url and the device appears on the cotunnel dashboard so you can manage your raspberry pi remotely with ssh terminal too.
Or you should develop your own tunnel-like service, or change your project structure. I don't know another way.

modbus error failing to connect to ModbusTcpClient when reading/writing

Recently I have been tasked with a project involving reading Modbus data, specifically with the pymodbus package. before diving into this project I wanted to simulate some reading and writing of Modbus data (without having to use a machine) with python. Using the sample data they have on their homepage, I try to write using the client but encounter a Modbus error
Ive tried looking into the matter to see what I could dig up wonder if I need a server to write or read. However I am unsure as with my experiences with socket and serial I just needed an established connection with the correct port to simply write (however I understand that Modbus is different).
Here is the code
client = ModbusTcpClient('localhost')
client.write_coil(1, True)
result = client.read_coils(1,1)
print(result.bits[0])
client.close()
and here is my error "pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(localhost:502)]"
I expect the output to simply write Modbus without needing anything no necessarily listen on the other end, but instead I keep getting an error when trying to connect/write. (sorry if this is hard to comprehend, my brain is all over the place and I am extremely new to Modbus in general).
If you want to send Modbus queries and you don't have any Modbus hardware yet you need to run a dummy Modbus server on your computer.
You can take a look at the examples.
You might also need to add a rule to your firewall for port 502. If you are on Linux you can just switch ports to a higher number like 5020 on both ends to avoid this problem.

Problems encoding string for TCP Communication in Python

collective internet,
I am a very new programmer that has given myself a specific project to teach myself coding. I work with a lot of equipment that can take TCP commands so I set out to build a system of buttons that will send different commands per each button. I got myself a Raspberry Pi 3b and took online classes on Python. I've got reasonably far on my own (I've got the buttons working how I want!) but where I've been stuck is sending TCP commands.
To be more specific: I am sending data and it is being received but the string command is not being encoded properly. The commands are functional when I execute them in a telnet session, but obviously I want them executed as part of my script. The commands don't specify that they need to be received over a telnet session and, by other means, I've had these commands work as TCP commands exterior to a telnet session. I read about a telnet module for Python but I don't think I should need it.
I verified packet delivery with wireshark. I captured the packets sent by my script and the packets sent by the telnet session and they're similar but not the same. Horseshoes and hand grenades, right? My current method has been to just preface the string (within ') with a lower case b. I also tried putting .encode() after the string (omitting the b in that situation).
The string command has the format:
setInput "InputName" Value
So for my use case, I'm setting the input named "One" to a value of 1:
setInput "One" 1
So as you can see in my script (inserted below) I ended up using:
s.sendall(b'setInput "One" 1')
But it's not quite sending the right information because it is not working and it doesn't look the same in wireshark.
TL;DR: I'm trying to send packets via TCP but they're not being encoded properly.
Ultimately, my question is if I am even headed in the right direction using these commands and just need a different means to encode the string or if I need to explore another direction entirely (perhaps the telnet module?)
Here is the script I've been using to test and the wireshark output of my script:
import socket
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.100.40', 3040))
print('connected')
time.sleep(2)
s.sendall(b'setInput "One" 1')
print('sent increase')
time.sleep(2)
s.sendall(b'setInput "One" 0')
print('sent decrease')
Wireshark log of my script
Here is the wireshark output of the telnet session that was successful:
Wireshark log of the telnet session
Any and all help is appreciated. I looked far and wide and can't seem to find any cases similar to mine.
EDITS: Sorry for the poor formatting. I appreciate the advice on how better to present posts. This is my first post here and I'm just getting the hang of it. My photos are still links due to my lack of privileges here. Sorry if I was too wordy, I just wanted to supply as much information as possible so as to help people understand my problem and, if a solution is found, to help people with a similar issue find this.
The telnet tcp data includes a carriage return and a linefeed and the end of the data. Apparently the receiving part needs this to be included to make things work. So change your Python string to
b'setInput "One" 1\r\n'

Categories

Resources