Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
charger-node
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
ThiefSil
charger-node
Commits
543d5f49
Commit
543d5f49
authored
6 months ago
by
최 민서
Browse files
Options
Downloads
Patches
Plain Diff
feat: 충전기 노드
parents
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
charger.cpp
+253
-0
253 additions, 0 deletions
charger.cpp
with
253 additions
and
0 deletions
charger.cpp
0 → 100644
+
253
−
0
View file @
543d5f49
#include
<stdio.h>
#include
<string.h>
#include
<fcntl.h>
#include
<unistd.h>
#include
<pthread.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
<linux/i2c-dev.h>
#include
<arpa/inet.h>
#include
<sys/ioctl.h>
#include
"MFRC522.h"
#define I2C_ADDR 0x23
#define LCD_CHR 1
#define LCD_CMD 0
#define LINE1 0x80
#define LINE2 0xC0
#define LCD_BACKLIGHT 0x08
#define ENABLE 0b00000100
#define BUFFER_SIZE 1024
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
int
lcd_fd
;
int
client_socket
;
int
battery_percentage
=
50
;
int
charging
=
0
;
int
fire_detected
=
0
;
int
car_parked
=
0
;
void
lcd_init
(
int
fd
);
void
lcd_byte
(
int
fd
,
int
bits
,
int
mode
);
void
lcd_toggle_enable
(
int
fd
,
int
bits
);
void
lcd_loc
(
int
fd
,
int
line
);
void
lcd_clear
(
int
fd
);
void
typeln
(
int
fd
,
const
char
*
s
);
void
*
listen_to_server
(
void
*
arg
);
void
*
update_battery_status
(
void
*
arg
);
void
*
rfid_tag_listener
(
void
*
arg
);
int
main
()
{
pthread_t
server_thread
,
battery_thread
,
rfid_thread
;
// Initialize LCD
const
char
*
i2c_device
=
"/dev/i2c-1"
;
lcd_fd
=
open
(
i2c_device
,
O_RDWR
);
if
(
lcd_fd
<
0
)
{
perror
(
"Failed to open I2C device"
);
return
1
;
}
if
(
ioctl
(
lcd_fd
,
I2C_SLAVE
,
I2C_ADDR
)
<
0
)
{
perror
(
"Failed to acquire bus access or talk to slave"
);
close
(
lcd_fd
);
return
1
;
}
lcd_init
(
lcd_fd
);
client_socket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
client_socket
<
0
)
{
perror
(
"Socket creation failed"
);
return
1
;
}
struct
sockaddr_in
server_address
;
server_address
.
sin_family
=
AF_INET
;
server_address
.
sin_port
=
htons
(
SERVER_PORT
);
inet_pton
(
AF_INET
,
SERVER_IP
,
&
server_address
.
sin_addr
);
if
(
connect
(
client_socket
,
(
struct
sockaddr
*
)
&
server_address
,
sizeof
(
server_address
))
<
0
)
{
perror
(
"Connection to server failed"
);
return
1
;
}
// Create threads
pthread_create
(
&
server_thread
,
NULL
,
listen_to_server
,
NULL
);
pthread_create
(
&
battery_thread
,
NULL
,
update_battery_status
,
NULL
);
pthread_create
(
&
rfid_thread
,
NULL
,
rfid_tag_listener
,
NULL
);
pthread_join
(
server_thread
,
NULL
);
pthread_join
(
battery_thread
,
NULL
);
pthread_join
(
rfid_thread
,
NULL
);
close
(
client_socket
);
close
(
lcd_fd
);
return
0
;
}
void
lcd_init
(
int
fd
)
{
lcd_byte
(
fd
,
0x33
,
LCD_CMD
);
lcd_byte
(
fd
,
0x32
,
LCD_CMD
);
lcd_byte
(
fd
,
0x06
,
LCD_CMD
);
lcd_byte
(
fd
,
0x0C
,
LCD_CMD
);
lcd_byte
(
fd
,
0x28
,
LCD_CMD
);
lcd_byte
(
fd
,
0x01
,
LCD_CMD
);
usleep
(
5000
);
}
void
lcd_byte
(
int
fd
,
int
bits
,
int
mode
)
{
int
bits_high
=
mode
|
(
bits
&
0xF0
)
|
LCD_BACKLIGHT
;
int
bits_low
=
mode
|
((
bits
<<
4
)
&
0xF0
)
|
LCD_BACKLIGHT
;
write
(
fd
,
&
bits_high
,
1
);
lcd_toggle_enable
(
fd
,
bits_high
);
write
(
fd
,
&
bits_low
,
1
);
lcd_toggle_enable
(
fd
,
bits_low
);
}
void
lcd_toggle_enable
(
int
fd
,
int
bits
)
{
usleep
(
500
);
int
bits_enable
=
bits
|
ENABLE
;
int
bits_disable
=
bits
&
~
ENABLE
;
write
(
fd
,
&
bits_enable
,
1
);
write
(
fd
,
&
bits_disable
,
1
);
usleep
(
500
);
}
void
lcd_loc
(
int
fd
,
int
line
)
{
lcd_byte
(
fd
,
line
,
LCD_CMD
);
}
void
lcd_clear
(
int
fd
)
{
lcd_byte
(
fd
,
0x01
,
LCD_CMD
);
lcd_byte
(
fd
,
0x02
,
LCD_CMD
);
}
void
typeln
(
int
fd
,
const
char
*
s
)
{
while
(
*
s
)
{
lcd_byte
(
fd
,
*
(
s
++
),
LCD_CHR
);
}
}
void
*
listen_to_server
(
void
*
arg
)
{
char
buffer
[
BUFFER_SIZE
];
while
(
1
)
{
int
bytes_received
=
recv
(
client_socket
,
buffer
,
BUFFER_SIZE
,
0
);
if
(
bytes_received
<=
0
)
{
perror
(
"Server connection lost"
);
break
;
}
buffer
[
bytes_received
]
=
'\0'
;
printf
(
"%s
\n
"
,
buffer
);
fire_detected
=
strstr
(
buffer
,
"-FIRE"
)
!=
NULL
;
if
(
fire_detected
)
{
charging
=
0
;
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"FIRE ALERT!"
);
lcd_loc
(
lcd_fd
,
LINE2
);
typeln
(
lcd_fd
,
"EVACUATE AREA!"
);
}
else
if
(
strcmp
(
buffer
,
"0-PARK"
)
==
0
)
{
car_parked
=
1
;
}
else
if
(
strcmp
(
buffer
,
"0-EXIT"
)
==
0
)
{
car_parked
=
1
;
car_parked
=
0
;
battery_percentage
=
50
;
charging
=
0
;
lcd_clear
(
lcd_fd
);
}
else
if
(
strcmp
(
buffer
,
"CLEAR"
)
==
0
)
{
car_parked
=
1
;
fire_detected
=
0
;
lcd_clear
(
lcd_fd
);
}
}
return
NULL
;
}
void
*
update_battery_status
(
void
*
arg
)
{
while
(
1
)
{
if
(
charging
&&
!
fire_detected
&&
car_parked
)
{
if
(
battery_percentage
<
100
)
{
battery_percentage
++
;
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
char
msg
[
16
];
snprintf
(
msg
,
sizeof
(
msg
),
"Charging: %d%%"
,
battery_percentage
);
typeln
(
lcd_fd
,
msg
);
char
server_msg
[
16
];
snprintf
(
server_msg
,
sizeof
(
server_msg
),
"0-%d"
,
battery_percentage
);
send
(
client_socket
,
server_msg
,
strlen
(
server_msg
),
0
);
}
else
{
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"CHARGE COMPLETE"
);
sleep
(
1
);
}
sleep
(
1
);
}
else
{
usleep
(
500000
);
}
}
return
NULL
;
}
void
*
rfid_tag_listener
(
void
*
arg
)
{
MFRC522
rfid
(
RPI_V2_GPIO_P1_24
,
RPI_V2_GPIO_P1_15
);
rfid
.
PCD_Init
();
uint8_t
card_id
[
4
];
while
(
1
)
{
if
(
!
fire_detected
)
{
if
(
!
car_parked
)
{
// 주차 대기 상황
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"NO CAR PARKED"
);
sleep
(
2
);
}
else
if
(
charging
)
{
// 충전중 (충전 중지 감지)
if
(
rfid
.
PICC_IsNewCardPresent
()
&&
rfid
.
PICC_ReadCardSerial
())
{
memcpy
(
card_id
,
rfid
.
uid
.
uidByte
,
4
);
charging
=
0
;
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"Charging Stopped"
);
sleep
(
2
);
}
}
else
if
(
!
charging
)
{
// 충전 대기중 (충전 시작 감지)
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"Scan your card"
);
if
(
rfid
.
PICC_IsNewCardPresent
()
&&
rfid
.
PICC_ReadCardSerial
())
{
memcpy
(
card_id
,
rfid
.
uid
.
uidByte
,
4
);
charging
=
1
;
lcd_clear
(
lcd_fd
);
lcd_loc
(
lcd_fd
,
LINE1
);
typeln
(
lcd_fd
,
"Card Accepted"
);
char
uid_message
[
64
];
snprintf
(
uid_message
,
sizeof
(
uid_message
),
"0-d%02X%02X%02X%02X"
,
card_id
[
0
],
card_id
[
1
],
card_id
[
2
],
card_id
[
3
]);
send
(
client_socket
,
uid_message
,
strlen
(
uid_message
),
0
);
sleep
(
2
);
}
}
usleep
(
100000
);
}
}
return
NULL
;
}
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