728x90

먼저 사용한 부품은 간단합니다.

서보모터 1개

led 2개(빨강,초록)

라즈베리파이 1개

 

소켓통신을 통해서 서버는 응답을 대기하고, 클라이언트로부터 1이라는 값이 도착하면 바이킹이 5회반복하게 코딩을 하였습니다.

 

먼저 서버코드입니다.

server.c

#include <wiringPi.h>
#include <softPwm.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SERVO 2
#define led_R 0
#define led_G 1
#define MYPORT 3490
#define BACKLOG 10

#define MAXDATASIZE 256 

int main(void){

if(wiringPiSetup() == -1)
	{
		return -1;
	}
    int sockfd, new_fd;  			// listen on sock_fd, new connection on new_fd
    struct sockaddr_in server_addr; // server address information
    struct sockaddr_in client_addr; // connector's address information
    socklen_t sin_size;
    
    // ---------------
    char bufRx[MAXDATASIZE];
    char bufTx[MAXDATASIZE];
    char bufAppMsg[MAXDATASIZE];
    int numbytes;
    // ---------------
    pinMode(led_R,OUTPUT);
    pinMode(led_G,OUTPUT);
    softPwmCreate(SERVO ,0, 200);
    int yes=1;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }

    server_addr.sin_family = AF_INET;			// host byte order
    server_addr.sin_port = htons(MYPORT);     // short, network byte order
    server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
    memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct

    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }
    digitalWrite(led_R, 0);
    digitalWrite(led_G, 1);

    while(1) {  // main accept() loop

        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size)) == -1) {
            perror("accept");
            continue;
        }

        // printf("server: got connection from %s\n",inet_ntoa(client_addr.sin_addr));

        // recv() ----
         if ((numbytes=recv(new_fd, bufRx, MAXDATASIZE-1, 0)) == -1) {
             perror("recv");
             exit(1);
         }

         printf ("\n[Recv Signal to client] = %s\n", bufRx);
	// received data

        if(bufRx[0] == '1')
        {
            printf ("\n[Viking] = ON \n");
            //softPwmCreate(SERVO ,0, 200);
                        
            digitalWrite(led_R, 1);
            digitalWrite(led_G, 0);
            for(int i=0; i<5; i++)
            {    
                softPwmWrite(SERVO ,15);
                delay(500);
                softPwmWrite(SERVO ,35);
                delay(500);
            }
            for(int j=0; j<1; j++)
            {
                softPwmWrite(SERVO ,20);
                delay(500);
            }
            digitalWrite(led_R, 0);
            digitalWrite(led_G, 1);

        }
        else
        {
            digitalWrite(led_R, 0);
            digitalWrite(led_G, 0);
           
        }


         // ------------
        if (send(new_fd, bufTx, strlen(bufTx), 0) == -1)
        	perror("send");
       
        close(new_fd);
    }

}

클라이언트 코드입니다.

client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wiringPi.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>


#define PORT 3490 				// the port client will be connecting to
#define MAXDATASIZE 100 			// max number of bytes we can get at once

int main(int argc, char *argv[])
{
    int sockfd, numbytes;
    char bufRx[MAXDATASIZE];
    char bufTx[128];
    struct sockaddr_in server_addr; // server address information


    if (argc != 3) {
        fprintf(stderr,"usage: client IP_address_of_the_server num : 1\n");
        exit(1);
    }
    
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    server_addr.sin_family = AF_INET;    // host byte order
    server_addr.sin_port = htons(PORT);  // short, network byte order
    server_addr.sin_addr.s_addr = inet_addr(argv[1]); // unsign000000000ed 4-byte (32-bit) binary address of IP address string
    memset(&(server_addr.sin_zero), '\0', 8);  // zero the rest of the struct

    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
        perror("connect");
        exit(1);
    }


    //--- build my protocol data unit

        if (strcmp(argv[2],"1") == 0)	// check LED ID
            strcpy (bufTx, "1");// LED ID code : "LED0" => "0", "LED1" => "1"
        else{
            printf("ERROR : Please enter it number 1 !!\n");                            // ',' : comma-separated field
            strcpy (bufTx, "0");
        }

    
        printf ("[Send signal to Viking] = %s\n", bufTx);
        //--- send ()
        if (send(sockfd, bufTx, strlen(bufTx)+1, 0) == -1)
            perror("send");

        // ---- recv () : waiting for the processing result from the server

        if ((numbytes=recv(sockfd, bufRx, MAXDATASIZE-1, 0)) == -1) {
            perror("recv");
            exit(1);
        

        //buf[numbytes] = '\0';
        printf("%s",bufRx);
        printf("\n");
    }
    
    close(sockfd);

    return 0;
}

 

코드는 기본적인 소켓통신코드로 작성을 하였으며 클라이언트의 인자는 'ip' '값' 순으로 입력을 하시면 됩니다.

 

작동영상

 

 

 

728x90

+ Recent posts