Hello,
I am using an Arduino Opta with the Arduino IoT Cloud editor. During the device configuration, I chose to setup connection via Ethernet, and it works fine.
Now: I want to be able to change the Ethernet configuration (IP, dns, gateway, subnet mask) in the main code, and not through the “Devices” panel in the Cloud. The reason for this, is because in the future I will allow the user to be able to change the parameters, if needed, without my help and without having access to the Cloud (I will prepare a function that collects the configuration from a config.txt file in a USB stick connected to the Opta).
I have found two possible solutions (Solution “A” and Solution “B”, see codes below), where I start from the code generated by default by the Cloud, include Ethernet.h, define the MAC and the network parameters, and then use “Ethernet.begin” in the setup, passing the desired parameters. The difference between my solutions “A” and “B” is only in the order of execution of the two lines “Ethernet.begin” and “ArduinoCloud.begin”. The rest is the same. Both solutions work, apparently.
My question is: are they both robust on the long term, or one of them is preferable? If both are wrong, what is the correct approach? Thank you in advance.
Matteo
Solution A:
#include "thingProperties.h"
#include <Ethernet.h>
byte ethMac[] = {0xa8, 0x61, 0x0a, 0x50, 0xa1, 0xc4}; // Factory value
IPAddress ethIp(192, 168, 1, 44);
IPAddress ethSubnet(255, 255, 255, 0);
IPAddress ethGateway(192, 168, 1, 1);
IPAddress ethDns(8, 8, 8, 8);
void setup() {
Serial.begin(9600);
delay(1500);
// Here I will make the ethIP, ethSubnet, ethGateway, ethDns be updated after reading from USB stick
initProperties();
Ethernet.begin(ethMac, ethIp, ethDns, ethGateway, ethSubnet);
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}
void loop() {
ArduinoCloud.update();
// My code here
}
Solution B:
#include "thingProperties.h"
#include <Ethernet.h>
byte ethMac[] = {0xa8, 0x61, 0x0a, 0x50, 0xa1, 0xc4}; // Factory value
IPAddress ethIp(192, 168, 1, 44);
IPAddress ethSubnet(255, 255, 255, 0);
IPAddress ethGateway(192, 168, 1, 1);
IPAddress ethDns(8, 8, 8, 8);
void setup() {
Serial.begin(9600);
delay(1500);
// Here I will make the ethIP, ethSubnet, ethGateway, ethDns be updated after reading from USB stick
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
Ethernet.begin(ethMac, ethIp, ethDns, ethGateway, ethSubnet);
}
void loop() {
ArduinoCloud.update();
// My code here
}