How to send more than one value to my MQTT Broker

214 Views Asked by At

I am using an ESP32 microcontroller to read data via the sensor MPU6050.

I am sending the data directly to my MQTT broker and then to a database (using Node-red).

My Question is, how can I send 100 values at once to the broker?

I tried the time.sleep but then only one value is published every 1 second. I want that e.g. every 10 seconds the values to be published, like a batch of values are published.

Code:

from umqttsimple import MQTTClient

i2c= SoftI2C(scl=Pin(21), sda=Pin(22))
mpu= mpu6050.accel(i2c)


client = MQTTClient(b"bruecke", mqtt_server, 1883, user=mqtt_username, password=mqtt_password)
client.connect()
    

while True:
    werte= mpu.get_values()
    
    print(werte['AcY'])
    client.publish(topic, str(werte['AcY']))
    time.sleep(1)
                   
print(werte['AcY'])
1

There are 1 best solutions below

2
EaswarD Sharma On

if you want to read a certain number of values over a certain period of time, read them at equal intervals while storing them locally. esp32 has 520kb ram and much more rom. i believe you can make a datatype of an array or object to store. after a time lapse publish this data. you can write a separate piece of code for this as it sends values one by one while in this time-lapse another code reads and stores locally.