Difference between revisions of "Arduino: Ethernet UDP Send isi 1 Analog Input 0"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) (New page: ==Code== →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 0...) |
Onnowpurbo (talk | contribs) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 19: | Line 19: | ||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED | ||
}; | }; | ||
− | IPAddress ip(192, 168, 0, | + | IPAddress ip(192, 168, 0, 4); |
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 | ||
Line 40: | Line 40: | ||
Udp.print(sensorReading); | Udp.print(sensorReading); | ||
Udp.write("\n"); | Udp.write("\n"); | ||
− | delay( | + | Serial.println(sensorReading); |
+ | Udp.endPacket(); | ||
+ | delay(1000); | ||
} | } | ||
+ | |||
+ | ==Di sisi Server== | ||
+ | |||
+ | ===Menerima Menggunakan nc=== | ||
+ | |||
+ | ifconfig eth0 192.168.0.100 | ||
+ | |||
+ | nc -ul 8888 | ||
+ | |||
+ | Akan keluar | ||
+ | |||
+ | |||
+ | 725 | ||
+ | 725 | ||
+ | 954 | ||
+ | 1020 | ||
+ | 0 | ||
+ | 0 | ||
+ | ... | ||
+ | |||
+ | ===Menerima Menggunakan python=== | ||
+ | |||
+ | Source udp_receiver.py | ||
+ | |||
+ | import socket | ||
+ | import time | ||
+ | |||
+ | UDP_IP = "192.168.0.100" | ||
+ | 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:34:04 01/03/16 WIB | ||
+ | 725 | ||
+ | |||
+ | 09:34:05 01/03/16 WIB | ||
+ | 725 | ||
+ | |||
+ | ... |
Latest revision as of 11:59, 23 February 2016
Code
/* 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, 4); 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,100); 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); int sensorReading = analogRead(0); Udp.print(sensorReading); Udp.write("\n"); Serial.println(sensorReading); Udp.endPacket(); delay(1000); }
Di sisi Server
Menerima Menggunakan nc
ifconfig eth0 192.168.0.100
nc -ul 8888
Akan keluar
725 725 954 1020 0 0 ...
Menerima Menggunakan python
Source udp_receiver.py
import socket import time UDP_IP = "192.168.0.100" 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:34:04 01/03/16 WIB 725 09:34:05 01/03/16 WIB 725 ...