title: Arduino MQTT synchronicity
body: <p>I try to establish a MQTT-connection between Arduino WifiRev2  and
  a python script on an ubuntu20-system.</p>
  <p>For test purposes the arduino is connected with 3 ultrasonic sensors and a gyro sensor.</p>
  <p>The mqtt-connection seems to work fine, arduino does publish the sensor data and
  also receives commands from the python scripts.</p>
  <p>But the sensor data are not sent synchronously.
  The sensor sent first is received much more frequent (about faktor 4)
  than the other sensor data.</p>
  <p>The script is adopted from this example code:
  <a href="https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device" rel="nofollow noreferrer">https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device</a></p>
  <p>Here is the loop in my arduino script:</p>
  <pre><code>const long interval = 30;
  unsigned long previousMillis = 0;
  void loop() {
  
    mqttClient.poll();
  
    unsigned long currentMillis = millis();
  
    if (currentMillis - previousMillis &gt;= interval) {
      // save the last time a message was sent
      previousMillis = currentMillis;
  
      //record random value from A0, A1 and A2
      //int Rvalue = analogRead(A0);
      int gyroSCL = digitalRead(gryoSCL_PIN);
      int gyroSDA = digitalRead(gryoSDA_PIN);
      int l_distance = getUSensDistance(USens_L_Trig_Pin,USens_L_Echo_Pin);
      int r_distance = getUSensDistance(USens_R_Trig_Pin,USens_R_Echo_Pin);
      int f_distance = getUSensDistance(USens_F_Trig_Pin,USens_F_Echo_Pin);
      int g_value = getGyro();
  
      mqttClient.beginMessage(topicL);
      mqttClient.print(l_distance);
      mqttClient.endMessage();
  
      mqttClient.beginMessage(topicR);
      mqttClient.print(r_distance);
      mqttClient.endMessage();
  
      mqttClient.beginMessage(topicF);
      mqttClient.print(f_distance);
      mqttClient.endMessage();
  
      mqttClient.beginMessage(topicG);
      mqttClient.print(g_value);
      mqttClient.endMessage();
  
      //Serial.println();
    }
  }
  </code></pre>
  <p>The python script is based on this example:
  <a href="https://www.emqx.com/en/blog/how-to-use-mqtt-in-python" rel="nofollow noreferrer">https://www.emqx.com/en/blog/how-to-use-mqtt-in-python</a></p>
  <p>A print example from the python subscriber is like this:</p>
  <pre><code>Connected to MQTT Broker!
  Received `44` from `usens_l` topic (timeDiff: 678024
  Received `25` from `usens_f` topic (timeDiff: 882899
  Received `44` from `usens_l` topic (timeDiff: 87380
  Received `49` from `usens_l` topic (timeDiff: 274183
  Received `44` from `usens_l` topic (timeDiff: 501763
  Received `44` from `usens_l` topic (timeDiff: 702241
  Received `44` from `usens_l` topic (timeDiff: 911118
  Received `44` from `usens_l` topic (timeDiff: 113206
  Received `44` from `usens_l` topic (timeDiff: 316174
  Received `45` from `usens_l` topic (timeDiff: 521477
  Received `45` from `usens_l` topic (timeDiff: 725778
  Received `45` from `usens_l` topic (timeDiff: 930363
  Received `-1` from `gyro` topic (timeDiff: 135167
  Received `45` from `usens_l` topic (timeDiff: 354054
  Received `54` from `usens_l` topic (timeDiff: 647140
  Received `42` from `usens_r` topic (timeDiff: 647306
  Received `41` from `usens_r` topic (timeDiff: 852423
  Received `55` from `usens_l` topic (timeDiff: 58828
  Received `55` from `usens_l` topic (timeDiff: 261107
  Received `55` from `usens_l` topic (timeDiff: 465823
  Received `55` from `usens_l` topic (timeDiff: 671458
  </code></pre>
  <p>So obviously some sensor data are not sent (or not received).
  I tried with different intervals, but in any case it seems
  that usens_l is receveid much more frequent than the other data.</p>
  <p>There could be a more simple way to send the data synchronously
  but building a string or calculate to a single value to integrate
  the data and send at once. Maybe this is the solution for
  my current issue. But I would like to understand, why this
  communication does not work as expected.</p>
  <p>Thank you for some hints!</p>