Tags

,

Program ini di download ke Arduino, didapat dari https://www.arduino.cc/en/Tutorial/UDPSendReceiveString

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 110); // IP dari Arduino di set di 192.168.0.110

unsigned int localPort = 9999;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
char  ReplyBuffer[] = &amp;quot;acknowledged&amp;quot;;       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print(&amp;quot;Received packet of size &amp;quot;);
    Serial.println(packetSize);
    Serial.print(&amp;quot;From &amp;quot;);
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i &amp;lt; 4; i++) {
      Serial.print(remote[i], DEC);
      if (i &amp;lt; 3) {
        Serial.print(&amp;quot;.&amp;quot;);
      }
    }
    Serial.print(&amp;quot;, port &amp;quot;);
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println(&amp;quot;Contents:&amp;quot;);
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), 9999);
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

Script di nodejs, ini script untuk mengirimkan data lewat UDP ke Arduino.

// client.js
var PORT = 9999;
var HOST = '192.168.0.110'; // IP Arduino
var dgram = require('dgram');
var message = new Buffer('0'); // data yang dikirim
var client = dgram.createSocket('udp4');

client.send(message, 0, message.length, PORT, HOST, function(err, bytes){
if (err) throw err;
console.log('UDP message sent to ' + HOST + ':' + PORT);
client.close();
});

Jika dilihat script dari arduino di atas (lihat bagian akhir), kita akan melihat bahwa arduino akan mengirimkan sinyal balik berupa pesan ‘acknowledged’, untuk mendapat pesan ini kita buat script lagi di nodejs untuk menerima pesan dari Arduino.

//server.js
var PORT = 999;
var HOST = '192.168.0.101'; // ip server
var dgram = require('dgram');
var server = dgram.createSocket('udp4');

// script ini untuk memastikan bahwa server sudah siap menerima pesan
server.on('listening', function(){
var address = server.address();
console.log('UDP server listening on ' + address.address + ':' + address.port);
});

// script ini untuk mengeluarkan pesan yang telah diterima
server.on('message', function(message, remote){
console.log(remote.address + ':' + remote.port + ' - ' + message);
});

server.bind(PORT, HOST);

Saatnya script client.js dan server.js dijalankan. Jika berhasil serial monitor di Arduino akan menghasilkan seperti ini:

serial monitor arduino udp

Dan di server.js akan seperti ini:

server nodejs udp

Save