Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Why does this code work different on Arduino IDE vs PlatformIO?

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    8

    Why does this code work different on Arduino IDE vs PlatformIO?

    I made a simple test program due to issues when connection to a MQTT broker. The problem is that when I connect to the MQTT broker when it is offline I get not the expected false but the system hangs.
    So I made a small test, ran it in the Arduino IDE and it works great, but in PlatformIO it fails (same code!). I have no clue what the problem is and I am completely stuck :-(

    Only when the broker is not online I get the difference between Arduino IDE and PlatformIO. PlatformIO hangs but Arduino IDE returns a 0 (failed to connect). When the broker is online they both work as expected.

    Seems in the NativeSocket.cpp (line 196) it is stuck in this loop where s == 0:

    fnet_socket_close(socket_ptr[s]);
    while(Ethernet.socketStatus(s) != 1){

    }
    socket_ptr[s] = nullptr;


    Suggestions are very welcome!

    #include <Arduino.h>
    #include <SPI.h>
    #include <NativeEthernet.h>

    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    EthernetClient testcl;

    void connectMQTT3()
    {
    char sIp[15] = "192.168.2.44";
    IPAddress MQTTbrokerIP(192,168,2,44);
    Serial.println("initMQTT");
    int result = testcl.connect(MQTTbrokerIP, 1883);
    delay(200);
    Serial.print("result: ");
    Serial.println(result);
    }

    void setup() {
    Serial.begin(9600);
    Ethernet.begin(mac);
    }

    void loop() {

    auto link = Ethernet.linkStatus();
    Serial.print("Link status: ");
    switch (link) {
    case Unknown:
    Serial.println("Unknown");
    break;
    case LinkON:
    Serial.println("ON");
    break;
    case LinkOFF:
    Serial.println("OFF");
    break;
    }
    delay(1000);
    connectMQTT3();
    }
    Last edited by JaKe101; 11-08-2020 at 08:10 AM.

  2. #2

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    8
    Thanks CorBee for your response!

    I added the libraries in the platformio.ini but that did not make any difference.

    Inspired by your suggestion, I reinstalled the Teensy platform which solved the problem.

    Thanks CorBee :-)

  4. #4
    Senior Member CorBee's Avatar
    Join Date
    Jun 2018
    Location
    Netherlands
    Posts
    347
    Thats unfortunate, I havent used this code myself so I cant help you with the code. But its a well known "issue" that libraries used for Arduino IDE and platformIO are not always in sync and might lead to differences. You could also download the library onto your system in the lib directory of your platformIO project. If I am not wrong PlatformIO always tries to use libraries from the lib-directory (in your project !!) before trying system-based platformIO libs.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •