0

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'])
Brits
  • 14,829
  • 2
  • 18
  • 31
ez.oez
  • 1
  • 1
  • [Edit](https://stackoverflow.com/posts/75793493/edit) the question to show your code, without knowing what you are currently doing it's not possible to tell you what to change. – hardillb Mar 20 '23 at 18:12
  • The message body is binary so you can send whatever you want (deciding how to encode the data is up to you). For instance you might decide to send a comma delimitated string (e.g. "123,456,321,231"). Please clarify what you want to send; multiple different readings returned from one call to `get_values()` or the same value from multiple calls (e.g. call `get_values()` every second, store the result, and only send a message when you have 10 results). – Brits Mar 20 '23 at 19:48
  • The sensor receives more than 100 values per second. I want to publish eg. 10 values at once to the broker. So whenever the sensor mesured 100 values it should publish. – ez.oez Mar 20 '23 at 20:53
  • Then you write code which publishes every 10 values or 100 values or whatever it is you need, and you decide as @Brits said how to represent that. Are you asking us to write the code for you? – romkey Mar 20 '23 at 21:01
  • okay, thank you all! This is my code and it works: werte= mpu.get_values() # Create an array data_array = [] for i in range(10): data_array.append(str(werte['AcY'])) # Add values to the array for werte in data_array: werte= mpu.get_values() message = str(werte['AcY']) client.publish(topic, str(data_array)) #publish print(data_array) – ez.oez Mar 20 '23 at 22:21
  • Please don't past code into the comments (it's unreadable). Edit the question instead. Glad you found a solution. – Brits Mar 20 '23 at 22:56

1 Answers1

0

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.