Difference between revisions of "Arduino: Ethernet UDP Send isi Analog Input"

From OnnoWiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
/*
+
/*
 
   UDPSendAnalogInput:
 
   UDPSendAnalogInput:
 
   
 
   
 
   created 27 Nov 2015
 
   created 27 Nov 2015
   by Onno W. Purbo
+
   by Onno W. Purbo
 
   
 
   
 
   This code is in the public domain.
 
   This code is in the public domain.
Line 18: Line 18:
 
  };
 
  };
 
  IPAddress ip(192, 168, 0, 3);
 
  IPAddress ip(192, 168, 0, 3);
IPAddress remote(192, 168, 0, 102);
 
 
  unsigned int localPort = 8888; // local port to listen on
 
  unsigned int localPort = 8888; // local port to listen on
 
   
 
   
 
  // An EthernetUDP instance to let us send and receive packets over UDP
 
  // An EthernetUDP instance to let us send and receive packets over UDP
 
  EthernetUDP Udp;
 
  EthernetUDP Udp;
 +
IPAddress remoteIP(192,168,0,102);
 +
unsigned int remotePort = 8888;
 
   
 
   
 
  void setup() {
 
  void setup() {
Line 29: Line 30:
 
   Udp.begin(localPort);
 
   Udp.begin(localPort);
 
   Serial.begin(9600);
 
   Serial.begin(9600);
  Udp.remoteIP() = remote();
 
  Udp.remotePort() = 8888;
 
 
  }
 
  }
 
   
 
   
 
  void loop() {
 
  void loop() {
+
  Udp.beginPacket(remoteIP, remotePort);
    // send
+
  // output the value of each analog input pin
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
+
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          // output the value of each analog input pin
+
    int sensorReading = analogRead(analogChannel);
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
+
    Udp.write("analog input ");
            int sensorReading = analogRead(analogChannel);
+
    Udp.print(analogChannel);
            Udp.write("analog input ");
+
    Udp.write(" is ");
            Udp.write(analogChannel);
+
    Udp.print(sensorReading);
            Udp.write(" is ");
+
    Udp.write("\n");
            Udp.write(sensorReading);
 
            Udp.write("                                           ");
 
          }
 
    Udp.endPacket();
 
 
   }
 
   }
   delay(10);
+
  Udp.endPacket();
 +
   delay(100);
 
  }
 
  }
  
  
  
==Di komputer Server Penerima==
+
 
 +
 
 +
==Menerima Menggunakan nc==
  
 
  ifconfig eth0 192.168.0.102
 
  ifconfig eth0 192.168.0.102
  
 
  nc -ul 8888
 
  nc -ul 8888
 +
 +
 +
Akan keluar
 +
 +
analog input 0 is 1023
 +
analog input 1 is 1023
 +
analog input 2 is 995
 +
analog input 3 is 992
 +
analog input 4 is 1010
 +
analog input 5 is 967
 +
 +
==Menerima Menggunakan python==
 +
 +
Source udp_receiver.py
 +
 +
import socket
 +
import time
 +
 +
UDP_IP = "192.168.0.102"
 +
UDP_PORT = 8888
 +
 +
sock = socket.socket(socket.AF_INET, # Internet
 +
                      socket.SOCK_DGRAM) # UDP
 +
sock.bind((UDP_IP, UDP_PORT))
 +
 +
while True:
 +
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 +
    print time.strftime('%X %x %Z'), "\n", data
 +
 +
Jalankan
 +
 +
python udp_receiver.py
 +
 +
Hasil
 +
 +
09:56:18 11/27/15 WIB
 +
analog input 0 is 1023
 +
analog input 1 is 1023
 +
analog input 2 is 1014
 +
analog input 3 is 970
 +
analog input 4 is 910
 +
analog input 5 is 856

Latest revision as of 09:12, 3 January 2016

/*

 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);
unsigned int localPort = 8888; // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
IPAddress remoteIP(192,168,0,102);
unsigned int remotePort = 8888;

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

void loop() {
  Udp.beginPacket(remoteIP, 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.print(analogChannel);
    Udp.write(" is ");
    Udp.print(sensorReading);
    Udp.write("\n");
  }
  Udp.endPacket();
  delay(100);
}



Menerima Menggunakan nc

ifconfig eth0 192.168.0.102
nc -ul 8888


Akan keluar

analog input 0 is 1023
analog input 1 is 1023
analog input 2 is 995
analog input 3 is 992
analog input 4 is 1010
analog input 5 is 967

Menerima Menggunakan python

Source udp_receiver.py

import socket
import time

UDP_IP = "192.168.0.102"
UDP_PORT = 8888

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print time.strftime('%X %x %Z'), "\n", data

Jalankan

python udp_receiver.py

Hasil

09:56:18 11/27/15 WIB 
analog input 0 is 1023
analog input 1 is 1023
analog input 2 is 1014
analog input 3 is 970
analog input 4 is 910
analog input 5 is 856