I'm working in a small code to receive multiple arguments from an MQTT server and use them to predict another value. I'm showing a simplified code here just to get some help. To pass the arguments to the script for executing the prediction, first part is the creation of a numpy array, then pass the arguments to the script using sys.argv[], then the indexing to position the incoming values.
import numpy as np
import sys
# creating empty numpy array for feature values
X = np.empty(2).reshape(1, 2)
#storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[2]
#displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
# set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[2]
print(X)
However, I have an issue with the second argument as I get an error:
exit code: 1, Traceback (most recent call last): File "numpy-array.py", line 10, in azimuth_cos=sys.argv[2] IndexError: list index out of range
The only way to avoid that error is if I set both arguments to: sys.arg[1]
#storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[1]
#displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
# set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[1]
print(X)
Then I get two consecutive outputs:
azimuth_sin : -0.9152180545267792 azimuth_cos : -0.9152180545267792 Number of arguments : 2 [[-0.91521805 -0.91521805]]
and:
azimuth_sin : 0.40295894662883136 azimuth_cos : 0.40295894662883136 Number of arguments : 2 [[0.40295895 0.40295895]]
which are actually the values of the two arguments printed, but repeated twice: sin = -0.9152180545267792 and cos = 0.40295894662883136
If I put the arguments in one line:
#storing the arguments
azimuth_sin, azimuth_cos = sys.argv[1:2]
The error is:
exit code: 1, Traceback (most recent call last): File "numpy-array-t1.py", line 10, in azimuth_sin, azimuth_cos = sys.argv[1:2] ValueError: not enough values to unpack (expected 2, got 1)
I've tried many ways to fix this without success, I'd appreciate any help or suggestions. Thank you in advance.
Start with something simple to validate the data you are receiving.
Do something like:
It may be that you are trying to process a numpy object on the command line
Note:
Do you have access to the MQTT server?
It might be easier to pick a channel (topic) to use for this data transfer.
You could get the MQTT server to publish this data on a channel and have this script subscribe to that channel.
Then you could make sending information as easy as a function call on your MQTT system.