아두이노 코드 (클라이언트)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/
 
#include <ESP8266WiFi.h>
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
 
Adafruit_7segment matrix = Adafruit_7segment();
 
#ifndef STASSID
#define STASSID "bitr39"
#define STAPSK  "bitbitr39"
#endif
 
const char* ssid     = STASSID;
const char* password = STAPSK;
 
const char* host = "192.168.1.12";
const uint16_t port = 5555;
 
void setup() {
 
Serial.begin(115200);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  matrix.begin(0x70);
}
 
void loop() {
 
 matrix.print(10000, DEC);
  matrix.writeDisplay();
  delay(500);
 
  // print a hex number
  matrix.print(0xBEEF, HEX);
  matrix.writeDisplay();
  delay(500);
 
  // print a floating point 
  matrix.print(12.34);
  matrix.writeDisplay();
  delay(500);
 
 
  
  Serial.print("connecting to ");
  Serial.print(':');
  Serial.println(port);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }
 
  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.print("hello from ESP8266");
  }
 
  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }
  //matrix.writeDisplay();
  //delay(1500);
 
  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  char ch;
  String str;
  int i;
  while (1)
  {
    str = (String)0;
    while (client.available()) {
      ch = static_cast<char>(client.read());
 
      Serial.print(ch);
      str += ch;
    }
    i = str.toInt();
    Serial.print("str:");
    Serial.println(str);
 
    matrix.println(i);
    matrix.writeDisplay();
    delay(3000);
    //matrix.println(j);
    //matrix.writeDisplay();
    //delay(5000);
  }
  // Close the connection
  Serial.println();
  Serial.println("closing connection");
 
  delay(300000); // execute once every 5 minutes, don't flood remote service
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

Jetson TX2 server.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/socket.h>
 
#include <termios.h>
#include <time.h>
#include <VL53L0X.h>
 
#define BUF_SIZE 30
void error_handling(char *message);
void read_childproc(int sig);
int getkey();
 
int main(int argc, char *argv[])
{
    VL53L0X *sensor = new VL53L0X();
    // Open the sensor
    if (!sensor->openVL53L0X()) {
        // Trouble
        printf("Unable to open VL53L0X") ;
        exit(-1) ;
    }
    sensor->init();
    sensor->setTimeout(500);
#if defined LONG_RANGE
    // lower the return signal rate limit (default is 0.25 MCPS)
    sensor.setSignalRateLimit(0.1);
    // increase laser pulse periods (defaults are 14 and 10 PCLKs)
    sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
    sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif
 
#if defined HIGH_SPEED
    // reduce timing budget to 20 ms (default is about 33 ms)
    sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
    // increase timing budget to 200 ms
    sensor.setMeasurementTimingBudget(200000);
#endif
 
 
    int serv_sock, clnt_sock;
    struct sockaddr_in serv_adr, clnt_adr;
 
    pid_t pid;
    struct sigaction act;
    socklen_t adr_sz;
    int str_len, state;
    char buf[BUF_SIZE];
    if(argc!=2) {
        printf("Usage : %s <port>\n", argv[0]);
        exit(1);
    }
 
    act.sa_handler=read_childproc;
    sigemptyset(&act.sa_mask);
    act.sa_flags=0;
    state=sigaction(SIGCHLD, &act, 0);
    serv_sock=socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_adr, 0sizeof(serv_adr));
    serv_adr.sin_family=AF_INET;
    serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_adr.sin_port=htons(atoi(argv[1]));
 
    if(bind(serv_sock, (struct sockaddr*&serv_adr, sizeof(serv_adr))==-1)
        error_handling("bind() error");
    if(listen(serv_sock, 5)==-1)
        error_handling("listen() error");
 
    while(1)
    {
        adr_sz=sizeof(clnt_adr);
        clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
        if(clnt_sock==-1)
            continue;
        else
            puts("new client connected...");
        pid=fork();
        if(pid==-1)
        {
            close(clnt_sock);
            continue;
        }
        if(pid==0)
        {
            close(serv_sock);
            //printf("child!\n");
            while(getkey() != 27){
                int distance = sensor->readRangeSingleMillimeters();
                int dis_len;
                char buf2[100];
                if (sensor->timeoutOccurred()) {
                    printf("Sensor timeout!\n");
                } else {
                    // If distance > 2000, no return received; Don't print it
                    if (distance < 2500 ) {
                        printf("\nDistance: %5d mm ",distance);
                    } else {
                        printf(".");
                    }
                    sprintf(buf2, "%d", distance);
                    dis_len=strlen(buf2);
 
                    write(clnt_sock,buf2, dis_len);
                    sleep(5);
                }
            }
 
            /*    while((str_len=read(clnt_sock, buf, BUF_SIZE))!=0)
                write(clnt_sock, buf, str_len);
             */    
            close(clnt_sock);
            puts("client disconnected...");
            return 0;
        }
        else
            close(clnt_sock);
    }
    close(serv_sock);
    return 0;
}
 
void read_childproc(int sig)
{
    pid_t pid;
    int status;
    pid=waitpid(-1&status, WNOHANG);
    printf("removed proc id: %d \n", pid);
}
 
void error_handling(char *message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}
 
int getkey() {
    int character;
    struct termios orig_term_attr;
    struct termios new_term_attr;
 
    /* set the terminal to raw mode */
    tcgetattr(fileno(stdin), &orig_term_attr);
    memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
    new_term_attr.c_lflag &= ~(ECHO|ICANON);
    new_term_attr.c_cc[VTIME] = 0;
    new_term_attr.c_cc[VMIN] = 0;
    tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
 
    /* read a character from the stdin stream without blocking */
    /*   returns EOF (-1) if no character is available */
    character = fgetc(stdin);
 
    /* restore the original terminal attributes */
    tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
 
    return character;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

'딥러닝 기반 영상인식 개발 전문가 과정 > 딥러닝' 카테고리의 다른 글

과제1 , 과제2  (0) 2019.08.30
IoT Node mcu 구현 과제  (0) 2019.08.30
CNN(Convolution Netural Network)  (0) 2019.08.26
차량 분류 보고서  (0) 2019.08.22
보고서 추가 내용  (0) 2019.08.21

+ Recent posts