Cloud doesn't work with magnetic sensor

I don't know whether this is a cloud issue, a sensor issue or something else, so I posted in this forum.

I have a magnetic sensor, TLx493D_W2BW. The sensor works perfectly when I use Arduino IDE. But I need to make it work in a cloud environment. My board is a MKR1010.

My loop looks like this:

      while(1)
        {
          double t, x, y, z;
          
           set_colour(CYAN);
          if (dut.getMagneticFieldAndTemperature(&x, &y, &z, &t))
          {
            magn_X = x;
            magn_Y = y;
            magn_Z = z;
            set_colour(GREEN);
            
          }
          else
          {
            set_colour(RED);
          }
          ArduinoCloud.update();
          delay(100);
          
        }

So I read the sensor. If reading is a success, the led turns green, otherwise red. It's green all the time (or actually flickers between cyan and green).
magn_X is a cloud variable, which is shown in my dashboard. I can edit its value in the dashboard.
When I start the program, the sensor is read and I get a value of magn_X (as well as of magn_Z). The values depend on a magnet and I get very reasonable readings, so that seems to work. But the value won't change! The first reading sets the value forever. Well, until I start the program again.
If I edit the value in the dashboard, a few seconds go and I get the first value back. So it's not magn_X, which is stuck. It's the x value of the sensor. The call to getMagneticFieldAndTemperature seems to be successfull, but it's the same value which is returned all the time.
What's going on here?

Code fragments are useless. Most of the code for a cloud project is created by the cloud elves. Modifying it takes more skill than non cloud projects. Show all the code in code tags. At some point it may be required to look at the details of things, devices, dashboards, triggers etc.

What is the purpose of the while(1) in a cloud sketch?

What is the purpose of a delay statement in a cloud sketch

You have been around this forum long enough to know that the error is usually in the code you did not post.

This should be all relevant code:

#include <TLx493D_inc.hpp>
#include <Servo.h>
#include <Wire.h>

using namespace ifx::tlx493d;
TLx493D_W2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Snootybug"
  https://create.arduino.cc/cloud/things/1bc1e18f-324a-496a-a5e5-8881a02a7677 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float magn_X;
  float magn_Y;
  float magn_Z;
  float x_speed;
  float y_speed;
  int lB_slide;
  int lF_slide;
  int rB_slide;
  int rF_slide;
  int slider_speed;
  int slider_time;
  bool press_E;
  bool press_N;
  bool press_NE;
  bool press_NW;
  bool press_S;
  bool press_SE;
  bool press_stop;
  bool press_SW;
  bool press_treat;
  bool press_W;
  bool switch_continuous;
  bool switch_phoneremote;
  bool switch_timed;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <WiFiNINA.h>
#include <utility/wifi_drv.h>
#include <math.h>
#include "motorclass.h"

#define RED       0x00ff00
#define AMBER     0x80ff00
#define YELLOW    0xffa000
#define GREEN     0xff0000
#define CYAN      0xff00ff
#define BLUE      0x0000ff
#define MAGENTA   0x00ffff
#define WHITE     0xffddff
#define BLACK     0x000000

void normalise_speed(void);
void set_colour(int c);
void set_colour(int r, int g, int b);

const uint8_t POWER_PIN = 8;

Servo dispenser;

float mx, my, mz;

enum modes
{
  timed,
  continuous,
  phoneremote,
  limbo
};

modes mode;
motorclass motor_LB(0, 1, 1);
motorclass motor_RB(2, 3, 1);
motorclass motor_RF(4, 5, 1);
motorclass motor_LF(6, 7, -1);

void setup() {

  // Defined in thingProperties.h
  initProperties();
   WiFiDrv::pinMode(25, OUTPUT);
   WiFiDrv::pinMode(26, OUTPUT);
   WiFiDrv::pinMode(27, OUTPUT);
  
   WiFiDrv::digitalWrite(25, 0);
   WiFiDrv::digitalWrite(26, 0);
   WiFiDrv::digitalWrite(27, 0);

  set_colour(RED);
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
   set_colour(AMBER);
  // My setups
  switch_continuous = false;
  switch_phoneremote = true;
  switch_timed = false;
  mode = phoneremote;
  x_speed = 0.;
  y_speed = 0.;
  
  dispenser.attach(20);
  Wire.begin();

  dut.begin();
  dut.setSensitivity(TLx493D_FULL_RANGE_e);
   set_colour(YELLOW);
  
}

void loop() {
  
  // Your code here 
  float length, alfa;
  int channel_r;
  int channel_g;
  int channel_b;

            
  dispenser.write(15);
  do
    {
       set_colour(GREEN);
  
    while(1)
        {
          double t, x, y, z;
          
          
           set_colour(CYAN);
          if (dut.getMagneticFieldAndTemperature(&x, &y, &z, &t))
          {
            magn_X = x;
            magn_Y = y;
            magn_Z = z;
            set_colour(GREEN);
            
          }
          else
          {
            set_colour(RED);
          }
          ArduinoCloud.update();
          delay(100);
          
        }  
      
  
}

/*
  Since PressN is READ_WRITE variable, onPressNChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressNChange()  {
  // Add your code here to act upon PressN change
  if (!press_N) return;
  y_speed += 5.;
  normalise_speed();
  
}

/*
  Since PressNE is READ_WRITE variable, onPressNEChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressNEChange()  {
  // Add your code here to act upon PressNE change
  if (!press_NE) return;
  y_speed += 3.53553390593;
  x_speed += 3.53553390593;
  normalise_speed();
}

/*
  Since PressE is READ_WRITE variable, onPressEChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressEChange()  {
  // Add your code here to act upon PressE change
  if (!press_E) return;
  x_speed += 5.;
  normalise_speed();
}

/*
  Since PressSE is READ_WRITE variable, onPressSEChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressSEChange()  {
  // Add your code here to act upon PressSE change
  if (!press_SE) return;
  y_speed -= 3.53553390593;
  x_speed += 3.53553390593;
  normalise_speed();
}

/*
  Since PressS is READ_WRITE variable, onPressSChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressSChange()  {
  // Add your code here to act upon PressS change
  if (!press_S) return;
  y_speed -= 5.;
  normalise_speed();
}

/*
  Since PressSW is READ_WRITE variable, onPressSWChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressSWChange()  {
  // Add your code here to act upon PressSW change
  if (!press_SW) return;
  y_speed -= 3.53553390593;
  x_speed -= 3.53553390593;
  normalise_speed();
}

/*
  Since PressW is READ_WRITE variable, onPressWChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressWChange()  {
  // Add your code here to act upon PressW change
  if (!press_W) return;
  x_speed -= 5;
  normalise_speed();
}

/*
  Since PressNW is READ_WRITE variable, onPressNWChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressNWChange()  {
  // Add your code here to act upon PressNW change
  if (!press_NW) return;
  y_speed += 3.53553390593;
  x_speed -= 3.53553390593;
  normalise_speed();
}

/*
  Since PressStop is READ_WRITE variable, onPressStopChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressStopChange()  {
  // Add your code here to act upon PressStop change
  y_speed = 0.;
  x_speed = 0.;
  if (press_stop)
  {
          motor_LF.set_speed(-150); // 
          motor_RF.set_speed(150);
          motor_LB.set_speed(150);
          motor_RB.set_speed(-150);

          delay(1500);  
  
          motor_LF.set_speed(0); // 
          motor_RF.set_speed(0);
          motor_LB.set_speed(0);
          motor_RB.set_speed(0);

          delay(1500);
  }
  
}

/*
  Since PressTreat is READ_WRITE variable, onPressTreatChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPressTreatChange()  {
  // Add your code here to act upon PressTreat change
  if (press_treat)
  {
          dispenser.write(70);
          delay(1000);
          dispenser.write(15);
    
  }
}

/*
  Since SwitchTime is READ_WRITE variable, onSwitchTimeChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSwitchTimeChange()  {
  // Add your code here to act upon SwitchTime change
  
}

/*
  Since SwitchContinuous is READ_WRITE variable, onSwitchContinuousChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSwitchContinuousChange()  {
  // Add your code here to act upon SwitchContinuous change
  if (switch_continuous)
  {
    mode = continuous;
    switch_timed = false;
    switch_phoneremote = false;
    WiFiDrv::digitalWrite(25, 1);
    WiFiDrv::digitalWrite(26, 0);
    WiFiDrv::digitalWrite(27, 0);

  }
  else
  {
    if (mode == continuous)
    {
      mode = limbo;
      WiFiDrv::digitalWrite(25, 0);
    }
  }
}


/*
  Since SwitchTimed is READ_WRITE variable, onSwitchTimedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSwitchTimedChange()  {
  // Add your code here to act upon SwitchTimed change
  if (switch_timed)
  {
    mode = timed;
    switch_continuous = false;
    switch_phoneremote = false;
    WiFiDrv::digitalWrite(25, 0);
    WiFiDrv::digitalWrite(26, 1);
    WiFiDrv::digitalWrite(27, 0);
  }
  else
  {
    
    if (mode == timed)
    {
      mode = limbo;
      WiFiDrv::digitalWrite(26, 0);
    }
  }
}

/*
  Since SwitchPhoneremote is READ_WRITE variable, onSwitchPhoneremoteChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSwitchPhoneremoteChange()  {
  // Add your code here to act upon SwitchPhoneremote change
  if (switch_phoneremote)
  {
    mode = phoneremote;
    switch_continuous = false;
    switch_timed = false;
    WiFiDrv::digitalWrite(25, 0);
    WiFiDrv::digitalWrite(26, 0);
    WiFiDrv::digitalWrite(27, 1);
  }
  else
  {
    if (mode == phoneremote)
    {
      mode = limbo;
      WiFiDrv::digitalWrite(27, 0);
    }
  }
}

/*
  Since SliderTime is READ_WRITE variable, onSliderTimeChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSliderTimeChange()  {
  // Add your code here to act upon SliderTime change
}

/*
  Since SliderSpeed is READ_WRITE variable, onSliderSpeedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSliderSpeedChange()  {
  // Add your code here to act upon SliderSpeed change
}

/*
  Since YSpeed is READ_WRITE variable, onYSpeedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onYSpeedChange()  {
  // Add your code here to act upon YSpeed change
}

/*
  Since XSpeed is READ_WRITE variable, onXSpeedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onXSpeedChange()  {
  // Add your code here to act upon XSpeed change
}

void normalise_speed(void)
{
  float speed, ratio;
  speed = x_speed * x_speed + y_speed * y_speed;
  if (speed <= 2500)
    return;
  ratio = 2500. / speed;
  x_speed *= ratio;
  y_speed *= ratio;
}

/*
  Since PhoneAx is READ_WRITE variable, onPhoneAxChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPhoneAxChange()  {
  // Add your code here to act upon PhoneAx change
}


/*
  Since PhoneAy is READ_WRITE variable, onPhoneAyChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPhoneAyChange()  {
  // Add your code here to act upon PhoneAy change
}


/*
  Since PhoneAz is READ_WRITE variable, onPhoneAzChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPhoneAzChange()  {
  // Add your code here to act upon PhoneAz change
}


/*
  Since GyroZ is READ_WRITE variable, onGyroZChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onGyroZChange()  {
  // Add your code here to act upon GyroZ change
}

void set_colour(int c)
{
  set_colour(c >> 16, (c >> 8) & 255, c & 255);
}

void set_colour(int r, int g, int b)
{
    
    WiFiDrv::analogWrite(25, r);
    WiFiDrv::analogWrite(26, g);
    WiFiDrv::analogWrite(27, b);
  
}




/*
  Since LFSlide is READ_WRITE variable, onLFSlideChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLFSlideChange()  {
  // Add your code here to act upon LFSlide change
}

/*
  Since RFSlide is READ_WRITE variable, onRFSlideChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRFSlideChange()  {
  // Add your code here to act upon RFSlide change
}

/*
  Since LBSlide is READ_WRITE variable, onLBSlideChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLBSlideChange()  {
  // Add your code here to act upon LBSlide change
}

/*
  Since RBSlide is READ_WRITE variable, onRBSlideChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRBSlideChange()  {
  // Add your code here to act upon RBSlide change
}

/*
  Since MagnX is READ_WRITE variable, onMagnXChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMagnXChange()  {
  // Add your code here to act upon MagnX change
  mx = magn_X;
}

/*
  Since MagnY is READ_WRITE variable, onMagnYChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMagnYChange()  {
  // Add your code here to act upon MagnY change
  my = magn_Y;
}

/*
  Since MagnZ is READ_WRITE variable, onMagnZChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMagnZChange()  {
  // Add your code here to act upon MagnZ change
  mz = magn_Z;
}

You started at the wrong end of the project, with code that has lots of irrelevant sections.

The methodical approach is to start from code that works flawlessly with the sensor, and add the lines required to send data to the cloud, one step at a time, testing as you go.

I give up.

You're right. Maybe I started at the wrong end. I thought I would be able to focus on one thing at a time by separating it inside a while(1) loop. I had lots of stuff working in my cloud project, the built in RGB, four motors, one servo, everything reacting to commands I gave from a Cloud dashboard. And I had a working magnetic sensor I tested in Arduino IDE. Then I tried to copy paste its code to my cloud project. And for some reason the magnetic sensor delivers one reading for a start and then gets stuck with that same value.

I kind of did what you suggested, but I started with all the other gadgets.

Does this compile?
The while should be at the end and outside of the do while code block...

Please line out your code (ctrl-t) and post again...

This color change will not be visable (as the color is immediately set to cyan...)

That was just a leftover from a previous test run, where I tried to follow where the program hung up. Irrelevant to what I now struggle with. What is relevant is in the wile(1) loop. Or so I think. That loop runs infinitely and the function calling the sensor returns the one and the same value all the time.

Yes. My bad, the indentation was wrong. while(1) starts an inner loop. It's not the end of the do section.

Where is the while that corresponds with the do?
Maybe a do without while is valid C...
But it does certainly not improve code readability...

There was a "while(true);" near the end, but I must have deleted it while I deleted a bunch of irrelevant stuff.

I'll return with a better code listing...

EDIT

More troubles. My MKR 1010 has stopped making contact to my Wifi, so right now I have no way of going further with anything before I get the Wifi problem sorted.