I am currently working on a project where I need to control an Arduino board via Ethernet, specifically turning an LED on and off using Intuiface. My goal is to press a button within an Intuiface experience and trigger an action that sends an API request to the Arduino to control the LED, without using serial communication.
I understand that Intuiface can integrate with external devices via HTTP and API calls, and I would like to explore how to send an API request from Intuiface to the Arduino Ethernet module to control the LED.
Could anyone share how to set up this integration using the API Explorer or any other recommended approach? Specifically, I am looking for guidance on:
Configuring the Arduino Ethernet module to receive and process API requests.
Setting up Intuiface to send HTTP requests to the Arduino.
Any example code or resources that could help me get started with this integration.
About the configuration of an Arduino to be able to receive API requests, I can’t really help as I’ve never done that before, and it’s not really Intuiface related.
I did ask ChatGPT though and will put its answer at the bottom of this message.
Once you have an HTTP Server set on your Arduino and you know its API address, you can then use the API Explorer in Composer to send a message to your Arduino. Because you’re sending information from Intuiface to your Arduino server, I’d suggest using a POST request here.
Once that Interface Asset is created, you can then use any triggers in your XP to call the Post action you have created on that Interface asset.
Designing the API (what endpoints? what parameters?) is also up to you but if you have only 1 LED that can be either turned ON or OFF, I’d go with 2 simple endpoints with no parameters
Let us know if that gets you in the right direction for your project.
Seb
ChatGPT Answer about Arduino / Ethernet Module
Setting up an HTTP server on an Arduino that accepts REST API requests is possible if you’re using an Ethernet shield/module. Here’s a basic outline for creating a REST API server on an Arduino:
Requirements:
Arduino board (e.g., Uno or Mega)
Ethernet Shield or Ethernet Module (e.g., W5100 or ENC28J60)
Arduino IDE
Ethernet library (built-in for W5100, or UIPEthernet library for ENC28J60)
Steps:
Install Required Libraries:
For Arduino Ethernet shields using the W5100 chipset, the built-in Ethernet library works.
If you have an ENC28J60 Ethernet module, install the UIPEthernet library from the Arduino Library Manager.
Connect Ethernet Shield to Network:
Plug in the Ethernet module to the Arduino and connect it to your router using an Ethernet cable.
Code for Arduino HTTP Server:
This code sets up a basic HTTP server that listens for API calls and responds to simple requests.
#include <SPI.h>
#include <Ethernet.h>
// MAC address and IP address for your Arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
// Initialize Ethernet server on port 80
EthernetServer server(80);
void setup() {
// Start the Ethernet connection
Ethernet.begin(mac, ip);
// Start the server
server.begin();
Serial.begin(9600);
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// Listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("New client connected");
// Buffer to store request
String request = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
// Detect end of request headers
if (request.endsWith("\r\n\r\n")) {
// Parse request
handleRequest(client, request);
break;
}
}
}
// Allow time for response to be sent
delay(1);
// Close connection
client.stop();
Serial.println("Client disconnected");
}
}
void handleRequest(EthernetClient& client, String request) {
Serial.println("Request:");
Serial.println(request);
// SEB's Note: Change the code below to handle the turnOn / turnOff requests and control your LED
// Check if the request is a GET or POST
if (request.indexOf("GET /api/hello") >= 0) {
// Send a JSON response
sendJsonResponse(client, "{ \"message\": \"Hello, World!\" }");
} else if (request.indexOf("POST /api/data") >= 0) {
// Send response indicating data was received
sendJsonResponse(client, "{ \"status\": \"Data received\" }");
} else {
// Send 404 for any unknown request
send404(client);
}
}
void sendJsonResponse(EthernetClient& client, String jsonResponse) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.println(jsonResponse);
}
void send404(EthernetClient& client) {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/plain");
client.println("Connection: close");
client.println();
client.println("404 Not Found");
}
Explanation of the Code:
Setup Network: The Arduino is set to a static IP in the Ethernet.begin() line.
Server Initialization: The EthernetServer object listens on port 80.
Handling Requests: The server reads incoming HTTP requests and checks for specific routes like /api/hello or /api/data. Based on the request, it sends an appropriate JSON response.
Sending Responses: JSON responses are sent using helper functions (sendJsonResponse and send404).
Upload and Test:
Compile and upload the code to your Arduino.
Open the Serial Monitor to see connection logs.
Test the API by accessing http://192.168.1.177/api/hello in a browser or using a tool like Postman.
Testing Using curl:
To test with curl, run the following commands in your terminal:
curl http://192.168.1.177/api/hello
curl -X POST http://192.168.1.177/api/data
This setup allows your Arduino to act as a simple REST server, responding to specific GET and POST requests with JSON data.