HTTP Web Client

HTTP Web Client

This tutorial shows how to create an HTTP web client on the NEXTuino BASE that connects to a web server and sends periodic HTTP GET requests.

Prerequisites

  • Arduino IDE installed
  • NEXTuino BASE board

Example Code

The complete example is available on GitHub:
WebClientRepeating.ino

Step-by-Step Guide

Step 1: Setup the Arduino Environment

  1. Connect your MICRO to your computer.
  2. Open the Arduino IDE.
  3. Ensure the appropriate board and port are selected in the Tools menu.

Step 2: Understanding the Code

Initialization

  • MAC Address: A unique identifier for your network interface (mac[] array).
  • IP and DNS: Optionally set a static IP and DNS server if DHCP fails.

Setup Function

  • Initialize serial communication for debugging.
  • Attempt to start Ethernet with DHCP; fall back to static IP if DHCP fails.

Loop Function

  • Handle Incoming Data: Print any data received from the server to the Serial Monitor.
  • Reconnect and Send Requests: Every 10 seconds (postingInterval), call httpRequest() to send a new GET request.

HTTP Request Function

  • Connect to Server: Attempt to connect to the specified server on port 80.
  • Send HTTP GET Request: If connected, send the HTTP request headers.
  • Handle Failure: Print an error message if the connection fails.

Step 3: Customization

  1. Modify the MAC Address: Change the mac[] array if needed.
  2. Set Static IP (Optional): Modify ip and myDns for a static network configuration.
  3. Change Server Address: Replace server[] with the target web server address.
  4. Upload and Monitor: Upload the sketch and open the Serial Monitor to see responses.

Conclusion

You now have a basic HTTP web client running on your NEXTuino BASE. The client connects to a specified web server, sends periodic HTTP GET requests, and prints the response to the Serial Monitor. Expand this by modifying the request type, parsing the response, or integrating it into a larger automation project.