Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladislav Rykov
THSO.gateway
Commits
622bcc0b
Commit
622bcc0b
authored
Mar 26, 2020
by
Vladislav Rykov
Browse files
db + data_send added
parent
9a0c0023
Changes
4
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
622bcc0b
CC
=
gcc
CC
=
gcc
INCLUDES
=
-I
lib
/gateway_protoco
l
INCLUDES
=
-I
/usr/include/postgresq
l
LFLAGS
=
-Llib
LFLAGS
=
-Llib
LDIR
=
lib
LDIR
=
lib
LIBS
=
$(LDIR)
/gateway_protocol/gateway_protocol
LIBS
=
$(LDIR)
/gateway_protocol/gateway_protocol
LIBD
=
-pthread
LIBD
=
-pthread
-lpq
SRC
=
src/gateway.c
SRC
=
src/gateway.c
...
@@ -18,7 +18,7 @@ MAIN = gateway
...
@@ -18,7 +18,7 @@ MAIN = gateway
$(LDIR)/gateway_protocol/gateway_protocol.o
:
$(LDIR)/gateway_protocol/gateway_protocol.o
:
$(CC)
-c
$(LIBS)
/gateway_protocol.c
-o
$(OBJ)
/gateway_protocol.o
-I
$(LIBS)
$(CC)
-c
$(LIBS)
/gateway_protocol.c
-o
$(OBJ)
/gateway_protocol.o
-I
$(LIBS)
$(CC)
$(SRC)
$(OBJ)
/gateway_protocol.o
-o
$(MAIN)
-I
$(LIBS)
$(LIBD)
$(CC)
$(SRC)
$(OBJ)
/gateway_protocol.o
-o
$(MAIN)
-I
$(LIBS)
$(LIBD)
$(INCLUDES)
all
:
$(MAIN)
all
:
$(MAIN)
...
...
gateway
View file @
622bcc0b
No preview for this file type
postgre/gateway_db.sql
0 → 100644
View file @
622bcc0b
DROP
DATABASE
IF
EXISTS
gateway
;
CREATE
DATABASE
gateway
;
\
c
gateway
ALTER
DATABASE
gateway
OWNER
TO
root
;
CREATE
TABLE
esp32
(
utc
numeric
(
10
,
0
),
timedate
character
varying
(
100
),
dht22_t_esp
real
,
dht22_h_esp
real
,
sht85_t_esp
real
,
sht85_h_esp
real
,
hih8121_t_esp
real
,
hih8121_h_esp
real
,
tmp36_0_esp
real
,
tmp36_1_esp
real
,
tmp36_2_esp
real
,
hih4030_esp
real
,
hh10d_esp
real
,
dht22_t_wis
real
,
dht22_h_wis
real
,
sht85_t_wis
real
,
sht85_h_wis
real
,
hih8121_t_wis
real
,
hih8121_h_wis
real
,
tmp102_wis
real
,
hh10d_wis
real
,
dht22_t_mkr
real
,
dht22_h_mkr
real
,
sht85_t_mkr
real
,
sht85_h_mkr
real
,
hih8121_t_mkr
real
,
hih8121_h_mkr
real
,
hh10d_mkr
real
);
ALTER
TABLE
esp32
OWNER
TO
root
;
--
-- Name: pend_msgs; Type: TABLE; Schema: public; Owner: root
--
CREATE
TABLE
pend_msgs
(
dev_id
numeric
(
3
,
0
)
NOT
NULL
,
msg
bytea
);
ALTER
TABLE
pend_msgs
OWNER
TO
root
;
src/gateway.c
View file @
622bcc0b
...
@@ -12,7 +12,44 @@
...
@@ -12,7 +12,44 @@
#include<sys/time.h>
#include<sys/time.h>
#include<libpq-fe.h>
typedef
struct
{
uint32_t
utc
;
char
timedate
[
32
];
float
dht22_t_esp
;
float
dht22_h_esp
;
float
sht85_t_esp
;
float
sht85_h_esp
;
float
hih8121_t_esp
;
float
hih8121_h_esp
;
float
tmp36_0_esp
;
float
tmp36_1_esp
;
float
tmp36_2_esp
;
float
hih4030_esp
;
float
hh10d_esp
;
float
dht22_t_mkr
;
float
dht22_h_mkr
;
float
sht85_t_mkr
;
float
sht85_h_mkr
;
float
hih8121_t_mkr
;
float
hih8121_h_mkr
;
float
hh10d_mkr
;
float
dht22_t_wis
;
float
dht22_h_wis
;
float
sht85_t_wis
;
float
sht85_h_wis
;
float
hih8121_t_wis
;
float
hih8121_h_wis
;
float
tmp102_wis
;
float
hh10d_wis
;
}
sensor_data_t
;
void
*
connection_handler
(
void
*
args
);
void
*
connection_handler
(
void
*
args
);
uint8_t
gateway_protocol_data_send_payload_decode
(
sensor_data_t
*
sensor_data
,
const
uint8_t
*
payload
,
const
uint8_t
payload_length
);
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
int
server_desc
,
client_desc
;
int
server_desc
,
client_desc
;
...
@@ -24,6 +61,12 @@ int main (int argc, char **argv) {
...
@@ -24,6 +61,12 @@ int main (int argc, char **argv) {
uint8_t
payload_length
=
0
;
uint8_t
payload_length
=
0
;
int
sock_len
;
int
sock_len
;
PGconn
*
conn
=
PQconnectdb
(
"user=root dbname=gateway"
);
if
(
PQstatus
(
conn
)
==
CONNECTION_BAD
)
{
printf
(
"connection to db error: %s
\n
"
,
PQerrorMessage
(
conn
));
return
EXIT_FAILURE
;
}
if
((
server_desc
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
))
<
0
)
{
if
((
server_desc
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
))
<
0
)
{
perror
(
"socket creation error"
);
perror
(
"socket creation error"
);
return
EXIT_FAILURE
;
return
EXIT_FAILURE
;
...
@@ -39,6 +82,7 @@ int main (int argc, char **argv) {
...
@@ -39,6 +82,7 @@ int main (int argc, char **argv) {
}
}
while
(
1
)
{
while
(
1
)
{
buf_len
=
0
;
printf
(
"listenninig...
\n
"
);
printf
(
"listenninig...
\n
"
);
if
((
buf_len
=
recvfrom
(
server_desc
,
(
char
*
)
buf
,
1024
,
MSG_WAITALL
,
(
struct
sockaddr
*
)
&
client
,
&
sock_len
))
<
0
)
{
if
((
buf_len
=
recvfrom
(
server_desc
,
(
char
*
)
buf
,
1024
,
MSG_WAITALL
,
(
struct
sockaddr
*
)
&
client
,
&
sock_len
))
<
0
)
{
perror
(
"socket receive error"
);
perror
(
"socket receive error"
);
...
@@ -74,8 +118,53 @@ int main (int argc, char **argv) {
...
@@ -74,8 +118,53 @@ int main (int argc, char **argv) {
gettimeofday
(
&
tv
,
NULL
);
gettimeofday
(
&
tv
,
NULL
);
memcpy
(
&
buf
[
buf_len
],
&
tv
.
tv_sec
,
sizeof
(
uint32_t
));
memcpy
(
&
buf
[
buf_len
],
&
tv
.
tv_sec
,
sizeof
(
uint32_t
));
buf_len
+=
sizeof
(
uint32_t
);
buf_len
+=
sizeof
(
uint32_t
);
}
else
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_DATA_SEND
)
{
sensor_data_t
sensor_data
;
printf
(
"DATA SEND received
\n
"
);
if
(
gateway_protocol_data_send_payload_decode
(
&
sensor_data
,
payload
,
payload_length
))
{
snprintf
(
buf
,
sizeof
(
buf
),
"INSERT INTO esp32 VALUES("
"%lu, %s, "
"%.2f, %.2f, "
// esp
"%.2f, %.2f, "
"%.2f, %.2f, "
"%.2f, %.2f, %.2f, "
"%.2f, "
"%.2f, "
"%.2f, %.2f, "
// mkr
"%.2f, %.2f, "
"%.2f, %.2f, "
"%.2f, "
"%.2f, %.2f, "
// wis
"%.2f, %.2f, "
"%.2f, %.2f, "
"%.2f, "
"%.2f)"
,
sensor_data
.
utc
,
sensor_data
.
timedate
,
sensor_data
.
dht22_t_esp
,
sensor_data
.
dht22_h_esp
,
sensor_data
.
sht85_t_esp
,
sensor_data
.
sht85_h_esp
,
sensor_data
.
hih8121_t_esp
,
sensor_data
.
hih8121_h_esp
,
sensor_data
.
tmp36_0_esp
,
sensor_data
.
tmp36_1_esp
,
sensor_data
.
tmp36_2_esp
,
sensor_data
.
hih4030_esp
,
sensor_data
.
hh10d_esp
,
sensor_data
.
dht22_t_mkr
,
sensor_data
.
dht22_h_mkr
,
sensor_data
.
sht85_t_mkr
,
sensor_data
.
sht85_h_mkr
,
sensor_data
.
hih8121_t_mkr
,
sensor_data
.
hih8121_h_mkr
,
sensor_data
.
hh10d_mkr
,
sensor_data
.
dht22_t_wis
,
sensor_data
.
dht22_h_wis
,
sensor_data
.
sht85_t_wis
,
sensor_data
.
sht85_h_wis
,
sensor_data
.
hih8121_t_wis
,
sensor_data
.
hih8121_h_wis
,
sensor_data
.
tmp102_wis
,
sensor_data
.
hh10d_wis
);
printf
(
"%.2f, %.2f, %.2f
\n
"
,
sensor_data
.
hih8121_h_wis
,
sensor_data
.
tmp102_wis
,
sensor_data
.
hh10d_wis
);
printf
(
"%s
\n
"
,
buf
);
}
else
{
printf
(
"payload decode error
\n
"
);
}
}
else
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_REQ
)
{
}
else
{
}
else
{
p
error
(
"packet type error"
);
p
rintf
(
"packet type error
\n
"
);
}
}
if
(
sendto
(
server_desc
,
(
char
*
)
buf
,
buf_len
,
0
,
(
struct
sockaddr
*
)
&
client
,
sock_len
)
<
0
)
{
if
(
sendto
(
server_desc
,
(
char
*
)
buf
,
buf_len
,
0
,
(
struct
sockaddr
*
)
&
client
,
sock_len
)
<
0
)
{
...
@@ -87,10 +176,107 @@ int main (int argc, char **argv) {
...
@@ -87,10 +176,107 @@ int main (int argc, char **argv) {
}
}
close
(
server_desc
);
close
(
server_desc
);
PQfinish
(
conn
);
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
}
}
uint8_t
gateway_protocol_data_send_payload_decode
(
sensor_data_t
*
sensor_data
,
const
uint8_t
*
payload
,
const
uint8_t
payload_length
)
{
uint8_t
p_len
=
0
;
memcpy
(
&
sensor_data
->
utc
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
utc
));
p_len
+=
sizeof
(
sensor_data
->
utc
);
memcpy
(
&
sensor_data
->
timedate
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
timedate
));
//p_len += sizeof(sensor_data->timedate);
p_len
+=
32
;
memcpy
(
&
sensor_data
->
dht22_t_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_t_esp
));
p_len
+=
sizeof
(
sensor_data
->
dht22_t_esp
);
memcpy
(
&
sensor_data
->
dht22_h_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_h_esp
));
p_len
+=
sizeof
(
sensor_data
->
dht22_h_esp
);
memcpy
(
&
sensor_data
->
sht85_t_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_t_esp
));
p_len
+=
sizeof
(
sensor_data
->
sht85_t_esp
);
memcpy
(
&
sensor_data
->
sht85_h_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_h_esp
));
p_len
+=
sizeof
(
sensor_data
->
sht85_h_esp
);
memcpy
(
&
sensor_data
->
hih8121_t_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_t_esp
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_t_esp
);
memcpy
(
&
sensor_data
->
hih8121_h_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_h_esp
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_h_esp
);
memcpy
(
&
sensor_data
->
tmp36_0_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
tmp36_0_esp
));
p_len
+=
sizeof
(
sensor_data
->
tmp36_0_esp
);
memcpy
(
&
sensor_data
->
tmp36_1_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
tmp36_1_esp
));
p_len
+=
sizeof
(
sensor_data
->
tmp36_1_esp
);
memcpy
(
&
sensor_data
->
tmp36_2_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
tmp36_2_esp
));
p_len
+=
sizeof
(
sensor_data
->
tmp36_2_esp
);
memcpy
(
&
sensor_data
->
hih4030_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih4030_esp
));
p_len
+=
sizeof
(
sensor_data
->
hih4030_esp
);
memcpy
(
&
sensor_data
->
hh10d_esp
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hh10d_esp
));
p_len
+=
sizeof
(
sensor_data
->
hh10d_esp
);
memcpy
(
&
sensor_data
->
dht22_t_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_t_mkr
));
p_len
+=
sizeof
(
sensor_data
->
dht22_t_mkr
);
memcpy
(
&
sensor_data
->
dht22_h_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_h_mkr
));
p_len
+=
sizeof
(
sensor_data
->
dht22_h_mkr
);
memcpy
(
&
sensor_data
->
sht85_t_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_t_mkr
));
p_len
+=
sizeof
(
sensor_data
->
sht85_t_mkr
);
memcpy
(
&
sensor_data
->
sht85_h_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_h_mkr
));
p_len
+=
sizeof
(
sensor_data
->
sht85_h_esp
);
memcpy
(
&
sensor_data
->
hih8121_t_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_t_mkr
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_t_mkr
);
memcpy
(
&
sensor_data
->
hih8121_h_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_h_mkr
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_h_mkr
);
memcpy
(
&
sensor_data
->
hh10d_mkr
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hh10d_mkr
));
p_len
+=
sizeof
(
sensor_data
->
hh10d_mkr
);
memcpy
(
&
sensor_data
->
dht22_t_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_t_wis
));
p_len
+=
sizeof
(
sensor_data
->
dht22_t_wis
);
memcpy
(
&
sensor_data
->
dht22_h_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
dht22_h_wis
));
p_len
+=
sizeof
(
sensor_data
->
dht22_h_wis
);
memcpy
(
&
sensor_data
->
sht85_t_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_t_wis
));
p_len
+=
sizeof
(
sensor_data
->
sht85_t_wis
);
memcpy
(
&
sensor_data
->
sht85_h_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
sht85_h_wis
));
p_len
+=
sizeof
(
sensor_data
->
sht85_h_wis
);
memcpy
(
&
sensor_data
->
hih8121_t_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_t_wis
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_t_wis
);
memcpy
(
&
sensor_data
->
hih8121_h_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hih8121_h_wis
));
p_len
+=
sizeof
(
sensor_data
->
hih8121_h_wis
);
memcpy
(
&
sensor_data
->
tmp102_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
tmp102_wis
));
p_len
+=
sizeof
(
sensor_data
->
tmp102_wis
);
memcpy
(
&
sensor_data
->
hh10d_wis
,
&
payload
[
p_len
],
sizeof
(
sensor_data
->
hh10d_wis
));
p_len
+=
sizeof
(
sensor_data
->
hh10d_wis
);
printf
(
"p_len = %d, payload_length = %d (float size %d)
\n
"
,
p_len
,
payload_length
,
sizeof
(
float
));
return
(
p_len
==
payload_length
);
}
void
*
connection_handler
(
void
*
args
)
{
void
*
connection_handler
(
void
*
args
)
{
int
client_desc
=
*
(
int
*
)
args
;
int
client_desc
=
*
(
int
*
)
args
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment