
Arduino (25)
/*
Web client
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,1,199); // numeric IP for Google (no DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /FF0101 HTTP/1.1");
client.println("Host: 192.168.1.206");
client.println("Authorization: Basic YWRtaW46YWRtaW4=");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
1. Download the Arduino Software:
http://www.arduino.cc/en/Main/Software
2. Download UIPEthernet Arduino Ethernet library for ENC28J60:
https://github.com/ntruchsess/arduino_uip
https://github.com/ntruchsess/arduino_uip/archive/master.zip
3.
4.
Master (Inputs) Code
#include "EtherShield.h"
uint8_t mymac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB9}; // Master MAC
uint8_t myip[4] = {192,168,1,26}; // Master IP
uint8_t udpmac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB8}; // Slave MAC
uint8_t udpip[4] = {192,168,1,25}; // Slave IP
#define DEST_PORT_L 0x34
#define DEST_PORT_H 0x12
const char iphdr[] PROGMEM ={0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total len on ip, 0x20 is ttl (time to live)
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
char reply[]={0xFF, 0x00, 0x00, 0x00, 0x00};
EtherShield es=EtherShield();
uint16_t plen, dat_p, a;
static uint32_t timetosend;
///----------------------------------------------------------
void setup(){
es.ES_enc28j60Init(mymac);
es.ES_init_ip_arp_udp_tcp(mymac,myip, 80);
} // end setup
///----------------------------------------------------------
void loop(){
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
if(millis() - timetosend > 100) // every 10 seconds //
{
timetosend = millis();
if ( check() )
{
reply[1] = digitalRead(A5) ? 1 : 0;
reply[2] = digitalRead(A4) ? 1 : 0;
reply[3] = digitalRead(A3) ? 1 : 0;
reply[4] = digitalRead(A2) ? 1 : 0;
UDP_Send();
}
}
} // end loop
void UDP_Send( void ) {
uint8_t i=0;
uint16_t ck;
// Setup the MAC addresses for ethernet header
while(i<6){
buf[ETH_DST_MAC +i]= udpmac[i];
buf[ETH_SRC_MAC +i]= mymac[i];
i++;
}
buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
es.ES_fill_buf_p(&buf[IP_P],9,iphdr);
// IP Header
buf[IP_TOTLEN_L_P]=28+sizeof(reply);
buf[IP_PROTO_P]=IP_PROTO_UDP_V;
i=0;
while(i<4){
buf[IP_DST_P+i]=udpip[i];
buf[IP_SRC_P+i]=myip[i];
i++;
}
es.ES_fill_ip_hdr_checksum(buf);
buf[UDP_DST_PORT_H_P]=DEST_PORT_H;
buf[UDP_DST_PORT_L_P]=DEST_PORT_L;
buf[UDP_SRC_PORT_H_P]=0x12;
buf[UDP_SRC_PORT_L_P]=0x34; // lower 8 bit of src port
buf[UDP_LEN_H_P]=0;
buf[UDP_LEN_L_P]=8+sizeof(reply); // fixed len
// zero the checksum
buf[UDP_CHECKSUM_H_P]=0;
buf[UDP_CHECKSUM_L_P]=0;
// copy the data:
i=0;
// most fields are zero, here we zero everything and fill later
uint8_t* b = (uint8_t*)&reply;
while(i< sizeof( reply ) ){
buf[UDP_DATA_P+i]=*b++;
i++;
}
// Create correct checksum
ck=es.ES_checksum(&buf[IP_SRC_P], 16 + sizeof( reply ),1);
buf[UDP_CHECKSUM_H_P]=ck>>8;
buf[UDP_CHECKSUM_L_P]=ck& 0xff;
es.ES_enc28j60PacketSend(42 + sizeof( reply ), buf);
}
int check()
{
if (digitalRead(A5) != reply[1] ) return(1);
if (digitalRead(A4) != reply[2] ) return(1);
if (digitalRead(A3) != reply[3] ) return(1);
if (digitalRead(A2) != reply[4] ) return(1);
return 0;
}
Slave (Relays) code
#include "EtherShield.h"
uint8_t mymac[6] = {0xCF,0x70,0x7C,0xE4,0x8A,0xB8};
uint8_t myip[4] = {192,168,1,25};
uint16_t MYWWWPORT = 80;
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
EtherShield es=EtherShield();
uint16_t plen, dat_p;
///----------------------------------------------------------
void setup(){
Serial.begin(9600);
Setup_Pins();
es.ES_enc28j60Init(mymac);
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
} // end setup
///----------------------------------------------------------
void loop(){
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
if (buf[IP_PROTO_P]==IP_PROTO_UDP_V){
Relay_Control();
buf[IP_PROTO_P]=0;
}
} // end loop
///----------------------------------------------------------
void Setup_Pins(){
pinMode(5, OUTPUT); digitalWrite(5, 0);
pinMode(6, OUTPUT); digitalWrite(6, 0);
pinMode(7, OUTPUT); digitalWrite(7, 0);
pinMode(8, OUTPUT); digitalWrite(8, 0);
} // end Setup_Pins
///----------------------------------------------------------
void Relay_Control(){
Serial.print(buf[43], HEX);
//if (buf[42]==0xFF)
// {
//Serial.print(buf[43], HEX);
digitalWrite(8, buf[43]);
digitalWrite(7, buf[44]);
digitalWrite(6, buf[45]);
digitalWrite(5, buf[46]);
// }
} // end Relay_Control
UDP Test Software
UDP Test Software (C#)
KMTronic UDP Test Software.zip
Source code (C#)
KMTronic UDP Test Software Source Code.zip
2 channels UDP Relay board
8 channels UDP Relay board
---------------------------------------------------------------------------------------------------------------------------
Windows OS
Download SOCAT from:
http://blog.gentilkiwi.com/programmes/socat
First relay ON command:
echo FF0101 | socat - udp-datagram:192.168.1.199:12345
First relay OFF command:
echo FF0100 | socat - udp-datagram:192.168.1.199:12345
---------------------------------------------------------------------------------------------------------------------------
Linux, AIX, BSD, HP-UX, Solaris e.a. (UNIX)
Download SOCAT from:
http://www.dest-unreach.org/socat/
First relay ON command:
echo FF0101 | socat - udp-datagram:192.168.1.199:12345
First relay OFF command:
echo FF0100 | socat - udp-datagram:192.168.1.199:12345
---------------------------------------------------------------------------------------------------------------------------
KMTronic LAN Ethernet IP 8 channels UDP Relay board
Written by Super User
Manufacturer:
KMTronic LTD
Features:
- Each relay has an LED to indicate when it is operated.
- Relay outputs can be used to turn ON/OFF lights, motors and other devices.
Specifications:
Relays have Normally Open (NO) and
Normally Closed (NC) Contacts each capable of switching max:
- 12VDC/15A
- 24VDC/15A
- 125VAC/15A
- 250VAC/10A
Supply:
Requires external 12 volt DC / 1000mA power supply
Operation Range:
Industrial (0°C to +80°C)
Dimensions:
108 mm / 150 mm / 25 mm (connectors mounted)
Default:
IP address: 192.168.1.199
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.1.1
UDP Port: 12345
UDP Commands:
FF0000 - Status Read command
FF0100 - Relay 1 OFF command
FF0101 - Relay 1 ON command
FF0200 - Relay 2 OFF command
FF0201 - Relay 2 ON command
FF0300 - Relay 3 OFF command
FF0301 - Relay 3 ON command
FF0400 - Relay 4 OFF command
FF0401 - Relay 4 ON command
FF0500 - Relay 5 OFF command
FF0501 - Relay 5 ON command
FF0600 - Relay 6 OFF command
FF0601 - Relay 6 ON command
FF0700 - Relay 7 OFF command
FF0701 - Relay 7 ON command
FF0800 - Relay 8 OFF command
FF0801 - Relay 8 ON command
FFE000 - All relays OFF command
FFE0FF - All relays ON command
UDP Test Software example including source code (C#):
KMTronic UDP Test Software.zip
KMTronic UDP Test Software Source Code.zip
KMTronic LAN Ethernet IP 2 channels WEB Relay board
Written by Super User
Manufacturer:
KMTronic LTD
Features:
- Each relay has an LED to indicate when it is operated.
- Relay outputs can be used to turn ON/OFF lights, motors and other devices.
Specifications:
Relays have Normally Open (NO) and
Normally Closed (NC) Contacts each capable of switching max:
- 12VDC/15A
- 24VDC/15A
- 125VAC/15A
- 250VAC/10A
Supply:
Requires external 12 volt DC / 500mA power supply
Operation Range:
Industrial (0°C to +80°C)
Dimensions:
105 mm / 51 mm / 20 mm
Default:
IP address: 192.168.1.199
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.1.1
Username: admin
Password: admin
Installation:
(1)
(2)
(3)
Library:
www.info.kmtronic.com/software/DINo/UDP_Examp...erShield_1.7.UDP.zip
==================================================================================
#include "EtherShield.h"
uint8_t mymac[6] = {0x00,0xE0,0x4C,0x55,0x83,0x78};
uint8_t myip[4] = {192,168,10,25};
uint16_t MYWWWPORT = 80;
uint8_t udpmac[6] = {0x00,0x1D,0x0F,0xD4,0xB2,0xD9};
uint8_t udpip[4] = {88,87,29,148};
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
char response[]="00000000";
EtherShield es=EtherShield();
uint16_t dat_p;
///----------------------------------------------------------
void setup(){
Serial.begin(9600);
es.ES_enc28j60Init(mymac);
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
} // end setup
///----------------------------------------------------------
void loop(){
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
if (Serial.available()) {
response[0] = Serial.read();
es.ES_send_udp(buf,response,8,12345, udpip, 12345, udpmac);
}
} // end loop
How to upload HEX files directly to Arduino
The program is called XLoader and you can download at
http://xloader.russemotto.com/
Now you need to attach the Arduino board (select device)
Select the COM port in my case is COM3. Then you have to navigate to the HEX file and select it.
Click upload and you are ready.
back to Arduino DINo projects
The Souliss project started in the middle of 2011 as extension of the Chibiduino stack used with a event-based communication protocol called MaCaco. In late 2011 it moved to a structured communication layer known as vNet, written to include more driver and communication media. It was announced on the Arduino forum as library for the Arduino based boards.
Link:
http://www.souliss.net/search/label/dino