---
title: "Hit the API with Node.js Express using Arduino Due Ethernet shield2"
url: "https://maker.wiznet.io/teju/projects/Hit-the-API-with-Nodejs-Express-using-Arduino-Due-Ethernet-shield2/"
markdown_url: "https://maker.wiznet.io/teju/projects/Hit-the-API-with-Nodejs-Express-using-Arduino-Due-Ethernet-shield2/md"
type: "UCC: User Created Content"
author: "teju"
author_url: "https://maker.wiznet.io/teju/"
original_author: "teju"
original_url: "https://qiita.com/minwinmin/items/58c177f76a837c7337f9"
published: "2022-07-25"
language: "en"
hardware: ["Ethernet shield 2", "Arduino Due"]
likes: 0
views: 204
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Hit the API with Node.js Express using Arduino Due Ethernet shield2

> Hit the API with Node.js Express using Arduino Due Ethernet shield2

Original author: teju (source: https://qiita.com/minwinmin/items/58c177f76a837c7337f9)

## Components

- **Ethernet shield 2** x 1
- **Arduino Due** x 1 ([docs](https://www.farnell.com/datasheets/1682211.pdf))
- Software: **Arduino IDE** ([docs](https://www.arduino.cc/en/software))

## Article

## Introduction

Recently, I've just made something by doing WiFi communication with ESP32. By the way, I haven't had many connections to the Internet by connecting to a wired LAN. So, I will connect a wired LAN and hit the API using Node.js and Express from Arduino Due.

## What you need, environment

- Arduino Due (Arduino Uno is fine as long as Ethernet shield 2 can be used)

- Ethernet shield2

- Node.js (v12.13.0)

- Express

## program

### Program on Node.js side

It is assumed that node.js is installed. Execute the following in an appropriate directory.?Then write the program in index.js.

```
npm init
npm i express
touch index.js
```

```
var express = require('express');
const bodyParser = require('body-parser');
var http = require("http"),
    url  = require("url"),
    server;

var app = express();
app.use(express.json());
const port = process.env.PORT || 3200;
app.listen(port, () => console.log(`Listening on port ${port}...`));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/', (req, res) => {
   res.setHeader('Content-Type', 'text/plain');
   res.json("Hello World !!");
   //res.sendStatus(200);
});

app.post('/api', (req, res) => {
  //Arduinoからの送られたデ?タvalをコンソ?ル?面に表示する
  console.log(req.query.val);
  res.setHeader('Content-Type', 'text/plain');
  res.json("Hello");
  //res.sendStatus(200);
});
```

### Arduino side program

```
#include <SPI.h>
#include <Ethernet2.h>

// Arduino Due : SerialUSB
// Arduino Uno : Serial
// ?考
// https://www.arduino.cc/reference/en/language/functions/communication/serial/
#define SERIAL SerialUSB

// Ethernet shield2のMACアドレス
// シ?ルドの裏面に記載してある番?の前に0xをつける
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED 
};

// Arduinoの固定IP
 // ここを適?なIPアドレス（ル?タ??で空いているもの）に?更
IPAddress ip(192, 168, 1, 177);

// PCのIPアドレスを入力
char server[] = "192.168.11.66"; 
// クライアント
EthernetClient client;

int val  = 0;

// POSTする間隔
int INTERVAL = 500;

bool post(int val) {
  //デ?タを送るのでメソッドはPOST
  String header = "POST /api/?val=";

  header += String(val);
  header += " HTTP/1.1";

  SERIAL.println("connecting...");

  //今回は3200番のポ?ト番?を使った
  if (client.connect(server, 3200)) {
    SERIAL.println("connected");

    client.println(header);
    client.println("Host: 192.168.11.66");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("User-Agent: Arduino Post Client");
    client.println("Connection: close");
    client.println();
    client.print("");
    client.stop();
    SERIAL.println("disconnecting.");

    return true;
  } else {
    SERIAL.println("connection failed");

    return false;
  }
}

void setup() {
  SERIAL.begin(115200);

  while (!SERIAL) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  if (Ethernet.begin(mac) == 0) {
    SERIAL.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
}

void loop() {
  val = random(255);

  SERIAL.println(val);

  //ランダムな?をPOST
  post(val);

  delay(INTERVAL);
}
```

### Operation check

Node.js index.js

```
node index.js
```

If you run it with, write a program to Arduino Due, turn on the power, a random number will be POSTed to the console screen. [![image.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F212276%2F67ca966a-f43a-1f18-4ec9-5aeb09f4eb33.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=9088998e681d9441d2e4d92cae7d148c)](https://camo.qiitausercontent.com/03ff9d4d96083e5cf7a8a87525baf24326ede4c3/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3231323237362f36376361393636612d663433612d316631382d346563392d3561656230396634656233332e706e67)

## **Impressions

It seems easy to implement GET and other methods as well as POST.

## reference

Send data from Arduino by POST and receive it with Node.js --Mikazuki Blog Part 3

https://kimizuka.hatenablog.com

---

Source: https://maker.wiznet.io/teju/projects/Hit-the-API-with-Nodejs-Express-using-Arduino-Due-Ethernet-shield2/
