Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
system-programming-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
system-programming-team5
system-programming-project
Commits
2db04942
Commit
2db04942
authored
2 years ago
by
lee chaemin
Browse files
Options
Downloads
Patches
Plain Diff
Upload client_new.c
parent
17cb91f3
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
client_new.c
+305
-0
305 additions, 0 deletions
client_new.c
with
305 additions
and
0 deletions
client_new.c
0 → 100644
+
305
−
0
View file @
2db04942
//이채민 client_new.c
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
#include
<string.h>
#include
<arpa/inet.h>
#include
<sys/socket.h>
#include
<wiringPi.h>
#include
<wiringPiSPI.h>
#include
<stdint.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define POUT 18
#define VALUE_MAX 30
#define MAX_TIME 100
#define DHT 5
#define PIN 20
int
data
[
5
]
=
{
0
,
0
,
0
,
0
,
0
};
char
temp_msg
[
10
];
int
read_dht_data
()
{
uint8_t
laststate
=
HIGH
;
uint8_t
counter
=
0
;
uint8_t
j
=
0
,
i
;
data
[
0
]
=
data
[
1
]
=
data
[
2
]
=
data
[
3
]
=
data
[
4
]
=
0
;
pinMode
(
DHT
,
OUTPUT
);
digitalWrite
(
DHT
,
LOW
);
delay
(
18
);
pinMode
(
DHT
,
INPUT
);
for
(
i
=
0
;
i
<
MAX_TIME
;
i
++
)
{
counter
=
0
;
while
(
digitalRead
(
DHT
)
==
laststate
)
{
counter
++
;
delayMicroseconds
(
1
);
if
(
counter
==
255
)
{
break
;
}
}
laststate
=
digitalRead
(
DHT
);
if
(
counter
==
255
)
break
;
if
((
i
>=
4
)
&&
(
i
%
2
==
0
))
{
data
[
j
/
8
]
<<=
1
;
if
(
counter
>
16
)
data
[
j
/
8
]
|=
1
;
j
++
;
}
}
if
((
j
>=
40
)
&&
(
data
[
4
]
==
((
data
[
0
]
+
data
[
1
]
+
data
[
2
]
+
data
[
3
])
&
0xFF
)))
{
printf
(
"Humidity = %d.%d %% Temperature = %d.%d C
\n
"
,
data
[
0
],
data
[
1
],
data
[
2
],
data
[
3
]);
//sprintf(temp_msg,"%d\n", data[2]);
//printf("temp_msg : %s\n", temp_msg);
if
(
data
[
2
]
>=
28
)
{
printf
(
"hot! hot! hot!
\n
"
);
//temp_msg[0] = 1;
//printf("temp_msg : %s\n", temp_msg);
return
(
1
);
}
else
return
(
0
);
}
else
{
printf
(
"failed
\n
"
);
return
(
0
);
}
}
static
int
GPIOExport
(
int
pin
)
{
#define BUFFER_MAX 3
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/export"
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open export for writing!
\n
"
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
}
static
int
GPIOUnexport
(
int
pin
)
{
char
buffer
[
BUFFER_MAX
];
ssize_t
bytes_written
;
int
fd
;
fd
=
open
(
"/sys/class/gpio/unexport"
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open unexport for writing!
\n
"
);
return
(
-
1
);
}
bytes_written
=
snprintf
(
buffer
,
BUFFER_MAX
,
"%d"
,
pin
);
write
(
fd
,
buffer
,
bytes_written
);
close
(
fd
);
return
(
0
);
}
static
int
GPIODirection
(
int
pin
,
int
dir
)
{
static
const
char
s_directions_str
[]
=
"in
\0
out"
;
#define DIRECTION_MAX 35
char
path
[
DIRECTION_MAX
]
=
"/sys/class/gpio/gpio%d/direction"
;
int
fd
;
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/gpio/gpio%d/direction"
,
pin
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open gpio direction for writing!
\n
"
);
return
(
-
1
);
}
if
(
-
1
==
write
(
fd
,
&
s_directions_str
[
IN
==
dir
?
0
:
3
],
IN
==
dir
?
2
:
3
))
{
fprintf
(
stderr
,
"Failed to set direction!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
0
);
}
static
int
GPIOWrite
(
int
pin
,
int
value
)
{
static
const
char
s_values_str
[]
=
"01"
;
char
path
[
VALUE_MAX
];
int
fd
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/gpio/gpio%d/value"
,
pin
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open gpio value for writing!
\n
"
);
return
(
-
1
);
}
if
(
1
!=
write
(
fd
,
&
s_values_str
[
LOW
==
value
?
0
:
1
],
1
))
{
fprintf
(
stderr
,
"Failed to write value!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
0
);
}
static
int
GPIORead
(
int
pin
)
{
char
path
[
VALUE_MAX
];
char
value_str
[
3
];
int
fd
;
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/gpio/gpio%d/value"
,
pin
);
fd
=
open
(
path
,
O_RDONLY
);
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open gpio value for reading!
\n
"
);
return
(
-
1
);
}
if
(
-
1
==
read
(
fd
,
value_str
,
3
))
{
fprintf
(
stderr
,
"Failed to read value!
\n
"
);
close
(
fd
);
return
(
-
1
);
}
close
(
fd
);
return
(
atoi
(
value_str
));
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
sock
;
struct
sockaddr_in
serv_addr
;
char
msg
[
2
];
char
on
[
2
]
=
"1"
;
int
str_len
;
int
light
=
0
;
int
state
=
1
;
int
prev_state
=
1
;
if
(
argc
!=
3
){
printf
(
"Usage : %s <IP> <port>
\n
"
,
argv
[
0
]);
exit
(
1
);
}
/*
const *hostname;
char buf [BUFSIZE];
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host %s\n", hostname);
exit((0);
}
*/
//Enable GPIO pins
if
(
-
1
==
GPIOExport
(
POUT
))
return
(
1
);
//Set GPIO directions
if
(
-
1
==
GPIODirection
(
POUT
,
OUT
))
return
(
2
);
if
(
wiringPiSetupGpio
()
==
-
1
)
exit
(
1
);
sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
sock
==
-
1
)
error_handling
(
"socket() error"
);
memset
(
&
serv_addr
,
0
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
inet_addr
(
argv
[
1
]);
serv_addr
.
sin_port
=
htons
(
atoi
(
argv
[
2
]));
if
(
connect
(
sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==-
1
)
error_handling
(
"connect() error"
);
while
(
1
){
state
=
read_dht_data
();
//read_dht_data();
delay
(
3000
);
if
(
prev_state
==
0
&&
state
==
1
){
light
=
(
light
+
1
)
%
2
;
snprintf
(
msg
,
2
,
"%d"
,
light
);
write
(
sock
,
msg
,
sizeof
(
msg
));
printf
(
"msg = %s
\n
"
,
msg
);
//write(sock, temp_msg, sizeof(temp_msg));
//printf("temp msg=%s\n", temp_msg);
/* if (temp_msg >= "28") {
printf("28 upup");
}*/
}
prev_state
=
state
;
usleep
(
500
*
100
);
/*
str_len = read(sock, msg, sizeof(msg));
//str_len = read(sock, temp_msg, sizeof(temp_msg));
if(str_len == -1)
error_handling("read() error");
printf("Receive message from Server : %s\n", msg);
if(strncmp(on,msg,1)==0) {
light = 1;
read_dht_data();
delay(2000);
write(sock, temp_msg, sizeof(temp_msg));
}
else
light = 0;
*/
//GPIOWrite(POUT, light);
}
close
(
sock
);
//Disable GPIO pins
if
(
-
1
==
GPIOUnexport
(
POUT
))
return
(
4
);
return
(
0
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment