The network-configuration should work out of the box, but if you have a network with fixed IP-addresses for example or just want to get some information about the state of your connection or change some of the default network settings, you will get what you need here. To change those settings you should use the ConnectinoNet-module from our Library
In every example we added a debug-output, that of course is not needed to work with the module. It is always essential to include the ConnectinoNet-module, to initialize an instance of the module and to setup the StxSerial. The software automatically starts the start()
-function and runs the code, and you can use the loop()
to continuously execute functions or test parameters. For our examples we just want to run our functions once.
Be aware! If you misconfigure your network-settings it is possible you can’t connect to your Connectino anymore. In those cases you can upload your corrected sketch via USB. A factory reset won’t help you there, as long as the wrong Sketch is on the ATMega, because the factory reset resets the IoT controller, but not the ATMega.
Link status
The following code shows how to check for the status of an network interface.
Download Example: ConnectinoNetwork.ino
#include <ConnectinoNet.h> // include the ConnectinoNet-library #include <SoftwareSerial.h> // include to generate some output // initialize the network-module on the UART ConnectinoNet net = ConnectinoNet(StxSerial); // initialize the outputpins for serial outputs SoftwareSerial debug = SoftwareSerial(8,9); uint8_t = state; void setup(){ // enable the uart and add communication via net-module StxSerial.begin(); StxSerial.add(net); debug.begin(9600); // start the serial output net.getLinkStatus(INTERFACE_ST, &state); // get the status of the interface (INTERFACE_ST = wlan as station, // INTERFACE_AP = wlan as access point) if(state) debug.println("Interface is on"); else debug.println("Interface is off"); } void loop(){ }
The essential line beside the initializing of the module is the net.getLinkStatus(interface,&state);
.
Wlan station IP configuration
Here you see how to set static IP-configuration and return to dhcp.
#include <ConnectinoNet.h> // include the ConnectinoNet-library #include <SoftwareSerial.h> // include to generate some output // initialize the network-module on the UART ConnectinoNet net = ConnectinoNet(StxSerial); // initialize the outputpins for serial outputs SoftwareSerial debug = SoftwareSerial(8,9); // ip configuration structure ConnectinoNet::ipConfig_t ipConfSet; ConnectinoNet::ipConfig_t ipConfGet; // static IP-address ipConfSet.ipAddress = {192, 168, 43, 170}; // subnetmask for static IP-address ipConfSet.subnet = {255, 255, 255, 0}; // gateway for static IP-address ipConfSet.gateway = {192, 168, 43, 1}; // little helper to print an ip struct void printIp(uint8_t ip[4]){ for(int i = 0; i < 4; i++){ debug.print(ip[i]); debug.print("."); } debug.println(); } void setup(){ // enable the uart and add communication via net-module StxSerial.begin(); StxSerial.add(net); debug.begin(9600); // start the serial output delay(30); // sometimes needed if Arduino starts faster then IoT-Controller net.getIpConfig(INTERFACE_ST, &ipMode, &ipConfGet); // on your station get the ip mode (dhcp or manual) and // the ip config (ip address, subnetmask and gateway) debug.println(ipMode); printIp(ipConfGet.ipAddress); printIp(ipConfGet.subnet); printIp(ipConfGet.gateway); net.setIpConfig(INTERFACE_ST, IPMODE_MANUAL, &ipConfSet); // on your station Interface set a static ip with the given addresses // net.setIpConfig(INTERFACE_ST, IPMODE_DHCP, NULL); to reenable dhcp } void loop(){ // enjoy your network }
The definition of the configuration is on the lines 15-21. The actual setting of the configuration happens on line 48. On line 40 you see how to get the active configuration.
Be aware of the ” ,
” on the IP-address-arrays those are not ” .
“!
Station DNS configuration
The following code shows how to list and how to change the DNS configuration.
#include <ConnectinoNet.h> // include the ConnectinoNet-library #include <SoftwareSerial.h> // include to generate some output // initialize the network-module on the UART ConnectinoNet net = ConnectinoNet(StxSerial); // initialize the outputpins for serial outputs SoftwareSerial debug = SoftwareSerial(8,9); // buffer for ip addresses uint8_t dnsIpAddress[4]; uint8_t gotIpAddress[4]; // little helper to print an ip struct void printIp(uint8_t ip[4]){ for(int i = 0; i < 4; i++){ debug.print(ip[i]); debug.print("."); } debug.println(); } void setup(){ // enable the uart and add communication via net-module StxSerial.begin(); StxSerial.add(net); debug.begin(9600); // start the serial output // set dns server ip address to 9.9.9.9 dnsIpAddress = {9,9,9,9}; // get primary dns server ip address net.getDnsConfig(INTERFACE_ST, gotIpAddress); printIp(gotIpAddress); // get secondary dns server ip address net.getDnsConfig(INTERFACE_ST, gotIpAddress, DNS_SECONDARY); printIp(gotIpAddress); net.setDnsConfig(INTERFACE_ST, dnsIpAddress); net.getDnsConfig(INTERFACE_ST, gotIpAddress, DNS_PRIMARY); printIp(gotIpAddress); } void loop(){ // have fun with your DNS server }
The primary DNS server IP address is defined on line 31 and set on line 39. You can set the secondary address by appending the argument DNS_SECONDARY
to the function as seen on the getDNSconfig()
.
Be aware of the “,” on the IP-address-arrays those are not “.”!
Access Point DHCP
Here you see how to change the dhcp settings for the Connectino access point.
#include <ConnectinoNet.h> // include the ConnectinoNet-library #include <SoftwareSerial.h> // include to generate some output // initialize the network-module on the UART ConnectinoNet net = ConnectinoNet(StxSerial); // initialize the outputpins for serial outputs SoftwareSerial debug = SoftwareSerial(8,9); uint8_t getState; uint8_t getStartRange[4]; uint32_t getLeaseTime; uint8_t getMaxClients; uint8_t startRange[4]; uint32_t leaseTime; uint8_t maxClients; // little helper to print an ip struct void printIp(uint8_t ip[4]){ for(int i = 0; i < 4; i++){ debug.print(ip[i]); debug.print("."); } debug.println(); } void setup(){ // enable the uart and add communication via net-module StxSerial.begin(); StxSerial.add(net); debug.begin(9600); // start the serial output startRange = {192.168.14.100}; // get the access point dhcp configuration net.getDhcpConfig( INTERFACE_AP, &getState, getStartRange, &getLeaseTime, &getMaxClients); debug.println(getState); printIp(getStartRange); debug.println(getLeaseTime, DEC); debug.println(getMaxClients, DEC); // disable dhcp on access point net.setDhcpConfig(INTERFACE_AP, DHCP_STATE_OFF,NULL,NULL,NULL); // reenable dhcp on access point // leasing IP-addresses from startRange lasting 3600 seconds // with a maximum of 7 clients net.setDhcpConfig(INTERFACE_AP, DHCP_STATE_ON, startRange,3600,7); } void (){ }
The startRange
on line 30 defines, where to start giving IP-address-leases on dhcp. All remaining configuration is directly inserted on line 54, where it is reenabled.