Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
smrt_bkshlf
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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
장 재균
smrt_bkshlf
Commits
569bb9e2
Commit
569bb9e2
authored
2 years ago
by
김 현빈
Browse files
Options
Downloads
Patches
Plain Diff
Update dc_motor/server_motor.c
parent
8a8d12a5
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
dc_motor/server_motor.c
+226
-0
226 additions, 0 deletions
dc_motor/server_motor.c
with
226 additions
and
0 deletions
dc_motor/server_motor.c
0 → 100644
+
226
−
0
View file @
569bb9e2
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<fcntl.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
<unistd.h>
#include
<string.h>
#include
<arpa/inet.h>
#include
<sys/socket.h>
#include
<wiringPi.h>
#include
<softPwm.h>
#define IN1 20
#define IN2 21
#define BUFFER_MAX 3
#define DIRECTION_MAX 45
#define VALUE_MAX 256
#define MAXTIMINGS 85
#define DHTPIN 7
int
dht11_dat
[
5
]
=
{
0
,
0
,
0
,
0
,
0
};
int
read_dht11_dat
()
{
uint8_t
laststate
=
HIGH
;
uint8_t
counter
=
0
;
uint8_t
j
=
0
,
i
;
float
f
;
/* fahrenheit */
dht11_dat
[
0
]
=
dht11_dat
[
1
]
=
dht11_dat
[
2
]
=
dht11_dat
[
3
]
=
dht11_dat
[
4
]
=
0
;
/* pull pin down for 18 milliseconds */
pinMode
(
DHTPIN
,
OUTPUT
);
digitalWrite
(
DHTPIN
,
LOW
);
delay
(
18
);
/* then pull it up for 40 microseconds */
digitalWrite
(
DHTPIN
,
HIGH
);
delayMicroseconds
(
40
);
/* prepare to read the pin */
pinMode
(
DHTPIN
,
INPUT
);
/* detect change and read data */
for
(
i
=
0
;
i
<
MAXTIMINGS
;
i
++
)
{
counter
=
0
;
while
(
digitalRead
(
DHTPIN
)
==
laststate
)
{
counter
++
;
delayMicroseconds
(
1
);
if
(
counter
==
255
)
{
break
;
}
}
laststate
=
digitalRead
(
DHTPIN
);
if
(
counter
==
255
)
break
;
/* ignore first 3 transitions */
if
(
(
i
>=
4
)
&&
(
i
%
2
==
0
)
)
{
/* shove each bit into the storage bytes */
dht11_dat
[
j
/
8
]
<<=
1
;
if
(
counter
>
50
)
dht11_dat
[
j
/
8
]
|=
1
;
j
++
;
}
}
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if
(
(
j
>=
40
)
&&
(
dht11_dat
[
4
]
==
(
(
dht11_dat
[
0
]
+
dht11_dat
[
1
]
+
dht11_dat
[
2
]
+
dht11_dat
[
3
])
&
0xFF
)
)
)
{
f
=
dht11_dat
[
2
]
*
9
.
/
5
.
+
32
;
printf
(
"Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)
\n
"
,
dht11_dat
[
0
],
dht11_dat
[
1
],
dht11_dat
[
2
],
dht11_dat
[
3
],
f
);
return
dht11_dat
[
0
];
}
else
{
printf
(
"Data not good, skip
\n
"
);
}
}
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
wiringPiSetupGpio
();
// GPIO 모드로 설정
int
serv_sock
,
clnt_sock
=-
1
;
struct
sockaddr_in
serv_addr
,
clnt_addr
;
socklen_t
clnt_addr_size
;
char
msg
[
2
];
pinMode
(
IN1
,
OUTPUT
);
pinMode
(
IN2
,
OUTPUT
);
softPwmCreate
(
IN1
,
0
,
100
);
softPwmCreate
(
IN2
,
0
,
100
);
serv_sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
serv_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
=
htonl
(
INADDR_ANY
);
serv_addr
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
if
(
bind
(
serv_sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==-
1
)
error_handling
(
"bind() error"
);
if
(
listen
(
serv_sock
,
5
)
==
-
1
)
error_handling
(
"listen() error"
);
if
(
clnt_sock
<
0
){
clnt_addr_size
=
sizeof
(
clnt_addr
);
clnt_sock
=
accept
(
serv_sock
,
(
struct
sockaddr
*
)
&
clnt_addr
,
&
clnt_addr_size
);
if
(
clnt_sock
==
-
1
)
error_handling
(
"accept() error"
);
}
if
(
wiringPiSetup
()
==
-
1
)
exit
(
1
);
while
(
1
){
int
num
=
read_dht11_dat
();
printf
(
"Humid : %d
\n
"
,
num
);
delay
(
1000
);
if
(
num
>
70
){
send
(
clnt_sock
,
"Motor Start"
,
11
,
0
);
softPwmWrite
(
IN1
,
100
);
softPwmWrite
(
IN2
,
0
);
delay
(
5000
);
// 5초 대기
// 모터 정지
softPwmWrite
(
IN1
,
0
);
softPwmWrite
(
IN2
,
0
);
// 프로그램 종료 전 핀 상태 초기화
digitalWrite
(
IN1
,
LOW
);
digitalWrite
(
IN2
,
LOW
);
send
(
clnt_sock
,
"Motor Stop"
,
10
,
0
);
delay
(
5000
);
/* wait 5sec to refresh */
}
}
close
(
clnt_sock
);
close
(
serv_sock
);
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