Arduino Flower Type Lighting continued

Source: Internet
Author: User
Tags htons

Author: huaqing visionary lecturer

Before we use Arduino to achieve the lighting, flashing lights, serial lighting and the combination with the upper computer lighting. This time we will implement a UDP server to accept the command to implement the lighting, and to use the Intel Edison Development Board to achieve the lighting.

First, the implementation of UDP lighting

A UDP server is a normal way for network traffic to take. Use UDP protocol to achieve fast network communication. Only three functions are required on the server side to deploy.

Udp_socket = socket (af_inet, SOCK_DGRAM, 0);

The first parameter of this function is the macro that represents the scope of the IPv4, the second parameter is the type of the datagram, and the user datagram format used by UDP. The function return value refers to the file descriptor, which is used for further action.

int bind (int sockfd, const struct SOCKADDR *addr, socklen_t Addrlen);

The BIND function binds the socket to the local address information.

struct sockaddr_in seraddr;

Seraddr.sin_family=af_inet;

Seraddr.sin_port=htons (50000);

SERADDR.SIN_ADDR.S_ADDR=INET_ADDR ("0.0.0.0");

SERADDR This structure needs to fill in the scope of the IPv4, as well as native address and local port.

Use the Recfrom function to accept information

Recvfrom (Fd,buf,sizeof (BUF), 0,null,null);

Combined with the previous serial functions, you can accept the datagram to open the serial port to the next machine Arduino send information:

Code:

#include #include

/* The NOTES/* #include

#include

#include

#include//#include//#include

#include

#include

#include

#include

#include

#include

int Serialfd;int Uart_device_open () {

struct Termios options;

if (Serialfd=open ("/dev/ttyusb0", o_rdwr| o_noctty| O_ndelay)) <0)

{

Perror ("Open failed");

return-1;

}

Tcgetattr (SERIALFD, &options);

Options.c_cflag |= (clocal | Cread);

Options.c_cflag &= ~csize;

Options.c_cflag &= ~crtscts;

Options.c_cflag |= CS8;

Options.c_cflag &= ~CSTOPB;

Options.c_iflag |= Ignpar;

Options.c_iflag &= ~ (Brkint | INPCK | Istrip | ICRNL | Ixon);

Options.c_cc[vmin] = 12;

Options.c_oflag = 0;

Options.c_lflag = 0;

Cfsetispeed (&options, B9600);

Cfsetospeed (&options, B9600);

Tcsetattr (serialfd,tcsanow,&options);

printf ("ZigBee serial ok!\n");

return 0;

}

int main (int argc, const char *argv[]) {

int FD;

Fd=socket (af_inet,sock_dgram,0);

if (fd<0)

{

printf ("fd error\n");

return-1;

}

Socket

struct sockaddr_in seraddr;

Seraddr.sin_family=af_inet;

Seraddr.sin_port=htons (50000);

SERADDR.SIN_ADDR.S_ADDR=INET_ADDR ("0.0.0.0");

Host to network small htons

Bind 2 sockaddr <--sockaddr_in

int ret_bind;

Ret_bind=bind (FD, (struct sockaddr*) &seraddr,sizeof (SERADDR));

if (ret_bind<0)

{

printf ("Fail to bind\n");

return-1;

}

Recvfrom

Char buf[128];

Uart_device_open ();

while (1)

{

Bzero (buf,sizeof (BUF));

Recvfrom (Fd,buf,sizeof (BUF), 0,null,null);

printf ("buf:%s\n", Buf);

if (buf[0]== ' 1 ' | | buf[0]== ' B ' | | buf[0]== ' 0 ')

{

Write (SERIALFD, buf, 1);

printf ("Success to send%c\n", buf[0]);

}

}

Close (FD);

return 0;

}

Second, the Intel Edison to achieve lighting

Edison is an intelligent development platform launched by Intel, compatible with Arduino, and also a high-performance x86 low power Development Board.

Let's first configure Intel Edison:

Once the connection is successful, Edison starts normally, and a letter appears in Windows. After the installation of Arduino several development kits.

Use the previous Arduino code to burn to the Development Board, using the method referenced by the Arduino;

This enables the Arduino lighting program for Intel Edison.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.