Pi/Arduino Serial Data
Craig Hissett wrote 04/29/2016 at 20:07 • 0 pointsJust after a bit of advice...
I have an arduino reading some pH/Temp sensors and a Pi running a python webserver.
The Pi receives input from the users via the web page and sends commands via serial to switch relays and update the LCD screen etc, and one of the commands requests the temp/pH data to be displayed on the web page.
My question is:
What is the best way to package and send my sensor data?
I'm thinking of putting it together as a JSON string that can be loaded and parsed by the python server script on the Pi.
All suggestions welcome :-)
Craig
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
I didn't quite get where to and from comes the data from. Shouldn't you just be sending an array of bytes to7from your controllers?
I have been using 'struct' and 'bytearray' to read/write to the a serial port in Python and I don't understand where would I use JSON in this case.
I have used pickle, thought, but that was for communications using sockets between two python applications (no microcontroller involved).
Are you sure? yes | no
Thanks for the reply - sorry for being unclear!
My Python script sends commands to the Arduino, such as 'HeatingOn', 'HeatingOff', 'CurrentStatus' etc.
My arduino receives these fine (by collecting reading while data available). For current status I would like to compile 4 different strings of data (current temp, current pH, light status, pump status) into one string to the python script to be split into seperate values.
Currently I am requesting the 4 bits of data separately but it is tricky to get it quickly and accurately, resulting in a messy web page when it comes to putting the returned values on I ha ha!
Are you sure? yes | no
Are 'HeatingOn/Off', 'CurrentStatus' strings that you're sending or some predifined integer value (1 for HeatingOn, 2 for Heating Off, 3 for Currentstatus).
Anyway, I would just send the information as binary data with a fixed length, receive the data as a string in Python and then interprete (don't know if that's the word in english) it with slices or struct.
Assuming 2 bytes per data:
firstData = int(data[0:2].encode('hex'),16)
secondData = ....
vs
import struct
firstData = struct.unpack('>H', data[0:2])
secondData = ....
Maybe there's something easier in Python 3? (I'm using Python 2)
Are you sure? yes | no
They are currently strings buddy. I agree, I definitely need to introduce a more structured method to reduce the size of data being sent.
Thanks matey!
Are you sure? yes | no
Are you sure? yes | no
Brilliant, thank you Radomir!
I've never heard of the struct method before - I'll find some examples and have a look see :)
Are you sure? yes | no