Hi, I managed to configure my MKR WIFI 1010 as a client to the Arduino IoT Cloud and at the same time as a Server within my local network. To manage the connection to my WiFi router I use the Arduino Cloud methods:
This is the thingProperties file:
#include "ArduinoIoTCloudTCP.h"
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char SSID[] = "myWIFI"; // Network SSID (name)
const char PASS[] = "myWiFiPassword"; // Network password (use for WPA, or use as key for WEP)
void onLed1Change();
void onLed2Change();
bool led1;
bool led2;
void initProperties(){
ArduinoCloud.addProperty(led1, READWRITE, ON_CHANGE, onLed1Change);
ArduinoCloud.addProperty(led2, READWRITE, ON_CHANGE, onLed2Change);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
And this is how I connect my board to WiFi in the sketch file:
#include <WiFiNINA.h>
#include "thingProperties.h"
int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient wifiClient;
int ledPin = 2;
int connectedToWiFi = 5;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(connectedToWiFi, OUTPUT);
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
/*
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
*/
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
ArduinoCloud.getConnection();
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
With this code my MKR Wifi 1010 can talk to the Arduino cloud and be a server within local network, the problem is that the local IP it gets is random, so if the router is restarted, I would have to look for the IP through terminal. And since I don't manage the connection through WiFiNINA library but instead I use the ArduinoCloud methods, I have not been able to make the IP static in my local network. Is this possible?
Thanks