When I started searching for how to transfer data between 2 computers using MQQT, I saw articles talking about Node-RED, but I dismissed it as being some sort of toy for children. That was probably a mistake because once I installed it, I realized it was both genius and a powerful tool. It lets you do amazing, complicated things without having to get into the weeds in every little detail.
In the screenshot below, the NR session from my Raspberry Pi is on top. The first block monitors a file to see if it changed. When it sees a change, it fires the next 2 blocks, which read the data I am looking for from a file. That data is passed on to the 3rd set of blocks (in purple) which publish the data via an MQQT broker running on the same computer. (mosquitto) The green block is just a debug block and is not needed.
The NR session on the bottom is running on my desktop. A single block (MQQT in) connects to the broker running on the Raspberry Pi and reads to values.
Here is python code running on the Raspberry Pi that retrieves the values and stores them in a pseudo-file. (the /var directory in a *nix machine is actually a location in RAM, not on the HDD or SD card)
#!/usr/bin/env python3
########################################################################
# Filename : myProg2.py
# Description : write pi ADC values to file
# Author : bg
# modification: 20210504
########################################################################
from time import sleep
import myADC
mydata = [0,0,0,0,0,0,0,0] #create python list
def loop():
while(True):
for i in range(8):
mydata[i] = myADC.getValue(i) #bring ADC values into list
filename = '/var/adc/'+str(i) #create the filename
ramdisk = open(filename,'w') #open file in writeover mode
ramdisk.write(str(mydata[i])) #write data to file
ramdisk.close()
sleep(5)
if __name__ == '__main__':
print ('Program is starting ... ')
# print(myADC.getValue(0))
# print(myADC.getValue(1))
try:
loop()
except KeyboardInterrupt:
#destroy()
pass