Can't connect to arduino-iot-cloud with PlatformIO

Hi, I hope this is the right place to post this, because I don’t know where the problem lies.

I’ve created a iot-cloud project, but once I upload it using PlatformIO the board gets stuck on ArduinoCloud.begin(ArduinoIoTPreferredConnection) (connect to wifi). This didn’t happened while uploading the (same) code over the official Arduino IDE.
The problem could be in the Arduino_ConnectionHandler library, or in the board renesas-ra/uno_r4_wifi itself. Does anyone has a iot-cloud project on an Arduino UNO R4 wifi?
You can find the project sourcecode on rogermiranda1000/ArduinoIotCloudWeatherStation.

The first step is to turn up debug log verbosity to the absolute maximum and compare.

The ArduinoIoTCloud library does use the Arduino_DebugUtils library for optional debug output. That’s actually runtime configurable. You just have to add

Debug.setDebugLevel(DBG_VERBOSE);
Debug.setDebugOutputStream(&Serial);

at the beginning of the sketch.

Can you do that for both the Arduino IDE and PlatformIO sketch and compare them?

Adding those instructions (before initProperties()) didn’t make any difference at all (maybe setDebugMessageLevel(2) already does that?). The logs with PlatformIO were inexistent, while Arduino IDE provided:

***** Arduino IoT Cloud - configuration info *****
Device ID: [device ID]
MQTT Broker: mqtts-up.iot.arduino.cc:8884
WiFi.status(): 0
Current WiFi Firmware: 0.2.0
Connected to [SSID]
ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic could not subscribe to /a/d/[device ID]/e/i
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Connected to Arduino IoT Cloud
Thing ID: [thing ID]

Okay. After hotfixing the debugging non-working debugger, I can see strange behavior: In the .begin() call, the call to RTC.begin() fails (it goes in the else path) and then the code… just stops executing. No matter whether I type continue or step in the GDB client or press the play / step over button, the code won’t advance. I haven’t see anything like this before…

Still looking into why that is.

That’s outrageous.

The disassembly is plain wrong. You see that in the code

   0x1a75a <_ZN7RTClock5beginEv+10>:	movs	r3, #1
   0x1a75c <_ZN7RTClock5beginEv+12>:	strb	r3, [r4, #0]
=> 0x1a75e <_ZN7RTClock5beginEv+14>:	movs	r3, #0
   0x1a760 <_ZN7RTClock5beginEv+16>:	strb	r3, [r4, #0]

the first line loads r3 with 1, aka the value for true. It then does a store byte (strb) of the r3 value into the memory addresses (r4 + 0), aka r4. After that, is_initialized = true. But then it unconditionally loads r3 with 0 again (false) and stores that again in r4. So it gets set to false again. Then, the b.n is an unconditional branch that takes the execution back to the begining of the code where it calls openRtc().

What the hell. The compiler output is just insane.

Also, the function has an obvious bug: It is supposed to return bool, but there is not a single return statement in that function. There should be a return is_initialized; at the end. I’ll correct that and see if the compiler’s head clears up.

Oh and what do you know, after adding the missing return, the compiler output is correct!

Now, after the strb, it correctly loads the just-saved value into r0, which is the “return value register” and does a pop {r4, pc} to cleanup the stack and return from the freaking function.

And what do you know, now it also prints stuff to the serial monitor.

See

For now, I’d just advise you to apply the fix manually in

C:\Users\<user>\.platformio\packages\framework-arduinorenesas-uno\libraries\RTC\src\RTC.cpp

and recompile the project.

1 Like

Thanks for making all the debugging, now it does output text, but it seems that it crashes in some other point:

--- Terminal on COM3 | 9600 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
***** Arduino IoT Cloud - configuration info *****
Device ID: [device id]
MQTT Broker: mqtts-up.iot.arduino.cc:8884
WiFi.status(): 0
Current WiFi Firmware: 0.2.0
Connected to [SSID]
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-up.iot.arduino.cc:8884
ArduinoIoTCloudTCP::handle_ConnectMqttBroker 1 connection attempt at tick time 45512

Firmware name: PROJECT_NAME, compiled on: Aug  1 2023
Fault on interrupt or bare metal(no OS) environment
===== Thread stack information =====
  addr: 20007e28    data: 00017915
  addr: 20007e2c    data: 0001772f
  addr: 20007e30    data: 20000300
  addr: 20007e34    data: 20007e58
  addr: 20007e38    data: 20000300
  addr: 20007e3c    data: 0000b28c
  addr: 20007e40    data: 0000a500
  addr: 20007e44    data: ffffffff
  addr: 20007e48    data: 20003064
  addr: 20007e4c    data: 05000000
  addr: 20007e50    data: c302ea8c
  addr: 20007e54    data: 00008bd5
  addr: 20007e58    data: 200009f0
  addr: 20007e5c    data: 20007e80
  addr: 20007e60    data: 00000001
  addr: 20007e64    data: 00000000
  addr: 20007e68    data: 20000010
  addr: 20007e6c    data: 20000010
  addr: 20007e70    data: 00000000
  addr: 20007e74    data: 00000000
  addr: 20007e78    data: 00000000
  addr: 20007e7c    data: 0001698f
  addr: 20007e80    data: 0001b24a
  addr: 20007e84    data: 000007d0
  addr: 20007e88    data: 20003af0
  addr: 20007e8c    data: 20000d48
  addr: 20007e90    data: 20000d48
  addr: 20007e94    data: 000089d7
  addr: 20007e98    data: 00000000
  addr: 20007e9c    data: 20000d48
  addr: 20007ea0    data: 20000d48
  addr: 20007ea4    data: 00008aaf
  addr: 20007ea8    data: 20000d48
  addr: 20007eac    data: 000003e8
  addr: 20007eb0    data: 200008f8
  addr: 20007eb4    data: 20000d48
  addr: 20007eb8    data: 00000000
  addr: 20007ebc    data: 00008add
  addr: 20007ec0    data: 20000a4c
  addr: 20007ec4    data: 00006a5d
  addr: 20007ec8    data: 20007f40
  addr: 20007ecc    data: 00004411
  addr: 20007ed0    data: 20007f40
  addr: 20007ed4    data: 0000a895
  addr: 20007ed8    data: 0001a249
  addr: 20007edc    data: 40046f00
  addr: 20007ee0    data: 00000000
  addr: 20007ee4    data: 0000a8d3
  addr: 20007ee8    data: 0001a249
  addr: 20007eec    data: 00018dd9
  addr: 20007ef0    data: 0001a249
  addr: 20007ef4    data: 0000f873
  addr: 20007ef8    data: 0000f869
  addr: 20007efc    data: 00002599
====================================
=================== Registers information ====================
  R0 : ffffffff  R1 : 000000f8  R2 : 20003aec  R3 : 00000000
  R12: 00000001  LR : 00017893  PC : 00017926  PSR: 81000000
==============================================================
Usage fault is caused by attempts to execute an undefined instruction
Show more call stack info by run: addr2line -e PROJECT_NAME.elf -a -f 00017926 00017892 0001772e 00008bd4 0001698e 000089d6 00008aae 00008adc 00006a5c 00004410 0000a894 0000a8d2 00018dd8 0000f872 0000f868

Oof okay.

I’m having a hard time reproducing this without actually setting upn Arduino IoT account I think, but I’ll check.

A shot in the dark: There are known problems with memory corruption per analogReference: fix heap corruption by facchinm · Pull Request #86 · arduino/ArduinoCore-renesas · GitHub.

Can you download https://raw.githubusercontent.com/arduino/ArduinoCore-renesas/96963d8574956e6fc02e56666ecdbc215dbfdf84/cores/arduino/analog.cpp and use that to completely replace

C:\Users\<user>\.platformio\packages\framework-arduinorenesas-uno\core\arduino\analog.cpp

? Is the bug still present after that?

I’m afraid nothing changes.
Edit: some addresses from the report change, but I mean that the error is still there.

WAKE ME UP

Wake me up insideeeee

It’s the same bug again. The compiler is generating garbage code because there is no return in that function, execution falls through in a place where it’s not supposed to be and it ends up decoding very weird instructions.

If I change the function to

/* -------------------------------------------------------------------------- */
bool WiFiUDP::read_needed(size_t s) {
/* -------------------------------------------------------------------------- */
   if((size_t)rx_buffer.available() < s) {
      _read();
      return true;
   }
   return false;
}

that is, in the file

C:\Users\<user>\.platformio\packages\framework-arduinorenesas-uno\libraries\WiFiS3\src\WiFiUdp.cpp

The crash disappears and it shows

***** Arduino IoT Cloud - configuration info *****
Device ID: My Device Login Name
MQTT Broker: mqtts-up.iot.arduino.cc:8884
WiFi.status(): 0
Current WiFi Firmware: 0.1.0
Connected to "Vodafone-XXXX"
I2C error.
ArduinoIoTCloudTCP::handle_ConnectMqttBroker In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current 0.1.0
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-up.iot.arduino.cc:8884
ArduinoIoTCloudTCP::handle_ConnectMqttBroker 1 connection attempt at tick time 20030
I2C error.
I2C error.
I2C error.

So it at least keeps going.

1 Like

Are you sure about that? After the fix I get two I2C error, and then nothing (just like the fatal crash from the beginning).
Note: getting I2C error means that you don’t have the sensors I have, don’t worry about that part.

Seems that it works now. It was indeed read_needed, but from WiFiSSLClient (not WiFiUDP). @maxgerhardt would you propose a PR, just like you did with the first “missing return”?

Yeah

is broken as well, but I can’t PR a fix into that because it’s already been fixed in the 1.0.2 version of the framework. PlatformIO currently uses 1.0.1.

1.0.2 does have the bug in RTC.begin(), but not in the WiFi library anymore. I’ve also added that as info to the opened issue. Hope PlatformIO can get out a package update soon.

FYI, fixes have been pushed to the repository, so if you delete

C:\Users\<user>\.platformio\packages\framework-arduinorenesas-uno

And in the platformio.ini set

platform = https://github.com/platformio/platform-renesas-ra.git#a0d14d9b9f53808da8f80095aba368044ef00d94

(or, @1.0.1 I hope, soon).

Then you should have framework version 1.0.2 (fixes the bugs in WiFiUDP/WiFiSSLClient library) that has on top that of my RTC fix included. Then no manual file modifications should be needed.

Can you test whether that then works out-of-the-box?