Arduino: Ethernet UDP Send isi Analog Input

From OnnoWiki
Revision as of 08:31, 27 November 2015 by Onnowpurbo (talk | contribs) (New page: →‎UDPSendAnalogInput: created 27 Nov 2015 by Onno W. Purbo This code is in the public domain.: #include <SPI.h> // needed for Arduino versions later than 0018 #inclu...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
/*
 UDPSendAnalogInput:

 created 27 Nov 2015
 by Onno W. Purbo

 This code is in the public domain.
*/

#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, 3);
IPAddress remote(192, 168, 0, 102);
unsigned int localPort = 8888; // local port to listen on

// 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);
  Udp.remoteIP() = remote();
  Udp.remotePort() = 8888;
}

void loop() {
    // send 
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
         // output the value of each analog input pin
         for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
           int sensorReading = analogRead(analogChannel);
           Udp.write("analog input ");
           Udp.write(analogChannel);
           Udp.write(" is ");
           Udp.write(sensorReading);
           Udp.write("                                            ");
         }
    Udp.endPacket();
  }
  delay(10);
}


Di komputer Server Penerima

ifconfig eth0 192.168.0.102
nc -ul 8888