Introduction: MKR1000 IoT Client/server Communications
This project describes how to set up two Arduino/Genuino MKR1000 devices as server and client.
The client MKR1000 will connect to your local wifi and listen for two inputs physically connected to the client; one from a button and the other from a vibration sensor.
On sensing an input the client MKR sends a GET request to the server MKR. On receiving a GET request, the server MKR is set up to turn on/off the built in LED (triggered by client button) and to fade up and down an attached LED (triggered by vibration sensor)
Step 1: Creating a Server on the MKR
Set up the MKR and breadboard as in the image.
The red LED is connected through a 1K Ohm resistor, to Pin #5. On the MKR this is a digital pin with pulse width modulation (PWM) which allows us to set a variable for brightness of the red LED. The other side of the LED is connected to ground.
The other LED used in this project is the onboard one on the MKR. This is marked "L" and is a green LED positioned close to the VCC pin.
Now download (or just copy) the code for the MKR server from here:
https://github.com/TonyCanning/MKR1000-IoT - the Arduino sketch name is "MKRServerLED.ino"
Edit this to include your wifi netwwork credentials and upload this to your MKR1000.
Once uploaded, open your serial monitor. (See image for descriptions of output) Initially this will show you little more than the IP address of the server. Take note of this address as you will need to include it in the client code too.
At this point, the server is up - we're going to set up the other MKR1000 as a client to this server. However, because it's a server you will be able to connect to it from any device on your network by typing the provided http://192.168.*.* address into any browser.
Give this a go and note that the provided page has clickable addresses to change the status of the LEDs on your MKR10000 server. Also note that the serial monitor detail updates to acknowledge these GET requests received by the server.
Note: there are libraries you may need to install, I'm pretty certain you'll have to install the Wifi101 library at the very least. Having tinkered for a long time I'm not sure what you will or won't need from a fresh install. Please refer to the wealth of info available about installing libraries or any other issues you may have with connecting/uploading etc.
Step 2: Creating a Client to Send Requests to the Server
Again, set up the breadboard as showin in the image.
In this case the button is connected to pin 9 and the vibration sensor is connected to pin 8. Bot pins are digital pins as the states for both of these inputs are binary.
Once complete you can download (or copy and paste) the client code from here:
https://github.com/TonyCanning/MKR1000-IoT - the file name is "MKRClientGET.ino"
At this point I recommend unplugging the server MKR from your PC as you'll not see any difference in naming when you are selecting COM port.
Edit the code to provide your wifi network credentials and the MKR server IP address. Make sure you look for each instance of "192" and change to your server IP address. Upload the code to the client MKR and open the serial monitor.
See the image of serial monitor output and try hitting the button and triggering the vibration sensor.
Step 3: Test It!
You should be done....
At this point you can provide power to each MKR1000 (how ever you chose to do so). Give them about 10 seconds and try triggering the client inputs to see the outputs on the server MKR.
Step 4: Troubleshooting
Before getting into troubleshooting - check the basics. Are you providing power to both MKRs? Are you sure the server code is on the server MKR and client code for client MKR?
Possible issues and solutions:
1. C:\Users\tony\Documents\Arduino\MKRClientGET\MKRClientGET.ino:11:18: fatal error: 1234.h: No such file or directory
This is an issue with a library you haven't installed. As noted in previous steps there is a wealth of info about this.
2. Server or Client not making a connection to your wifi; likely you haven't provided your wifi credentials.
3. Client serial monitor showing state changes but no reaction on server; likely caused by not providing server IP address in your client code.
4. Button not showing change state in serial monitor; check your breadboard contacts.
This is an entry in the
We have a be nice policy.
Please be positive and constructive.