Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Siavoosh.Azad
/
ESP32_Sensor_data_to_Cumulocity
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
489bbf0e
authored
Mar 15, 2019
by
Siavoosh.Azad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
5b645d4f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
303 additions
and
0 deletions
main/main.c
main/main.c
0 → 100644
View file @
489bbf0e
/* HTTP GET Example using plain POSIX sockets
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_attr.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "esp_tls.h"
#include "lwip/err.h"
#include "apps/sntp/sntp.h"
static
void
initialize_sntp
(
void
);
static
void
obtain_time
(
void
);
int
average_temp
=
23
;
uint8_t
temprature_sens_read
();
/* The examples use simple WiFi configuration that you can set via
'make menuconfig'.
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID "BV7000 Pro"
#define EXAMPLE_WIFI_PASS "sia12345"
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static
EventGroupHandle_t
wifi_event_group
;
/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
const
int
CONNECTED_BIT
=
BIT0
;
/* Constants that aren't configurable in menuconfig */
// #define WEB_SERVER "example.com"
#define WEB_SERVER "c8y.ttu.ee"
#define WEB_PORT 80
#define WEB_URL "http://example.com/"
static
const
char
*
TAG
=
"WIFI"
;
static
esp_err_t
event_handler
(
void
*
ctx
,
system_event_t
*
event
)
{
switch
(
event
->
event_id
)
{
case
SYSTEM_EVENT_STA_START
:
esp_wifi_connect
();
break
;
case
SYSTEM_EVENT_STA_GOT_IP
:
xEventGroupSetBits
(
wifi_event_group
,
CONNECTED_BIT
);
break
;
case
SYSTEM_EVENT_STA_DISCONNECTED
:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect
();
xEventGroupClearBits
(
wifi_event_group
,
CONNECTED_BIT
);
break
;
default:
break
;
}
return
ESP_OK
;
}
static
void
initialise_wifi
(
void
)
{
tcpip_adapter_init
();
wifi_event_group
=
xEventGroupCreate
();
ESP_ERROR_CHECK
(
esp_event_loop_init
(
event_handler
,
NULL
)
);
wifi_init_config_t
cfg
=
WIFI_INIT_CONFIG_DEFAULT
();
ESP_ERROR_CHECK
(
esp_wifi_init
(
&
cfg
)
);
ESP_ERROR_CHECK
(
esp_wifi_set_storage
(
WIFI_STORAGE_RAM
)
);
wifi_config_t
wifi_config
=
{
.
sta
=
{
.
ssid
=
EXAMPLE_WIFI_SSID
,
.
password
=
EXAMPLE_WIFI_PASS
,
},
};
ESP_LOGI
(
TAG
,
"Setting WiFi configuration SSID %s..."
,
wifi_config
.
sta
.
ssid
);
ESP_ERROR_CHECK
(
esp_wifi_set_mode
(
WIFI_MODE_STA
)
);
ESP_ERROR_CHECK
(
esp_wifi_set_config
(
ESP_IF_WIFI_STA
,
&
wifi_config
)
);
ESP_ERROR_CHECK
(
esp_wifi_start
()
);
}
static
void
http_get_task
(
void
*
pvParameters
)
{
const
struct
addrinfo
hints
=
{
.
ai_family
=
AF_INET
,
.
ai_socktype
=
SOCK_STREAM
,
};
//-------------------------------------------------------------------------------------------
// setting up the system clock!
//-------------------------------------------------------------------------------------------
struct
addrinfo
*
res
;
struct
in_addr
*
addr
;
int
s
,
r
;
char
recv_buf
[
128
];
time_t
now
;
struct
tm
timeinfo
;
time
(
&
now
);
localtime_r
(
&
now
,
&
timeinfo
);
// Is time set? If not, tm_year will be (1970 - 1900).
ESP_LOGI
(
TAG
,
"year: %d"
,
timeinfo
.
tm_year
);
if
(
timeinfo
.
tm_year
<
(
2019
-
1900
))
{
ESP_LOGI
(
TAG
,
"Time is not set yet. Connecting to WiFi and getting time over NTP."
);
obtain_time
();
// update 'now' variable with current time
time
(
&
now
);
}
char
strftime_buf
[
64
];
setenv
(
"TZ"
,
"UTC"
,
1
);
// set time to UTC will fix it later with offset!
tzset
();
localtime_r
(
&
now
,
&
timeinfo
);
strftime
(
strftime_buf
,
sizeof
(
strftime_buf
),
"%c"
,
&
timeinfo
);
ESP_LOGI
(
TAG
,
"The current date/time in UTC is: %s"
,
strftime_buf
);
while
(
1
)
{
/* Wait for the callback to set the CONNECTED_BIT in the
event group.
*/
xEventGroupWaitBits
(
wifi_event_group
,
CONNECTED_BIT
,
false
,
true
,
portMAX_DELAY
);
ESP_LOGI
(
TAG
,
"Connected to AP"
);
int
err
=
getaddrinfo
(
WEB_SERVER
,
"80"
,
&
hints
,
&
res
);
if
(
err
!=
0
||
res
==
NULL
)
{
ESP_LOGE
(
TAG
,
"DNS lookup failed err=%d res=%p"
,
err
,
res
);
vTaskDelay
(
1000
/
portTICK_PERIOD_MS
);
continue
;
}
/* Code to print the resolved IP.
Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
addr
=
&
((
struct
sockaddr_in
*
)
res
->
ai_addr
)
->
sin_addr
;
ESP_LOGI
(
TAG
,
"DNS lookup succeeded. IP=%s"
,
inet_ntoa
(
*
addr
));
s
=
socket
(
res
->
ai_family
,
res
->
ai_socktype
,
0
);
if
(
s
<
0
)
{
ESP_LOGE
(
TAG
,
"... Failed to allocate socket."
);
freeaddrinfo
(
res
);
vTaskDelay
(
1000
/
portTICK_PERIOD_MS
);
continue
;
}
ESP_LOGI
(
TAG
,
"... allocated socket"
);
if
(
connect
(
s
,
res
->
ai_addr
,
res
->
ai_addrlen
)
!=
0
)
{
ESP_LOGE
(
TAG
,
"... socket connect failed errno=%d"
,
errno
);
close
(
s
);
freeaddrinfo
(
res
);
vTaskDelay
(
4000
/
portTICK_PERIOD_MS
);
continue
;
}
ESP_LOGI
(
TAG
,
"... connected"
);
freeaddrinfo
(
res
);
time
(
&
now
);
localtime_r
(
&
now
,
&
timeinfo
);
// time format: 2019-03-11T13:06:14.000+02:00
ESP_LOGI
(
TAG
,
"%d-%d-%dT%d:%d:%d.000+02:00"
,
timeinfo
.
tm_year
+
1900
,
timeinfo
.
tm_mon
+
1
,
timeinfo
.
tm_mday
,
timeinfo
.
tm_hour
,
timeinfo
.
tm_min
,
timeinfo
.
tm_sec
);
const
char
*
initialString
=
"POST /measurement/measurements HTTP/1.1
\n
Host: c8y.ttu.ee
\n
Content-Length: 370
\n
Authorization: Basic aW1sYWJzdHVkZW50QGdtYWlsLmNvbTpkYXNkOTEyOFNES0dzZGEhZDI=
\n
Content-Type: application/json
\n\n
{
\n
\"
classroom
\"
: {
\n
\"
Temperature
\"
: {
\n
\"
value
\"
: "
;
const
char
*
middlestring
=
",
\n
\"
unit
\"
:
\"
C
\"
}
\n
,
\n
\"
Humidity
\"
: {
\n
\"
value
\"
: 80,
\n
\"
unit
\"
:
\"
%
\"
}
\n
,
\n
\t\"
Speed
\"
: {
\n
\"
value
\"
: 10,
\n
\"
unit
\"
:
\"
km/h
\"
}
\n
},
\n
\"
time
\"
:
\"
"
;
const
char
*
finalString
=
"
\"
,
\n
\"
source
\"
: {
\n
\"
id
\"
:
\"
480186
\"
},
\n
\"
type
\"
:
\"
classroom
\"\n
}
\n
"
;
char
*
REQUEST
=
malloc
(
strlen
(
initialString
)
+
strlen
(
middlestring
)
+
strlen
(
finalString
)
+
31
);
sprintf
(
REQUEST
,
"%s%d%s%d-%d-%dT%d:%d:%d.000+02:00%s"
,
initialString
,
average_temp
,
middlestring
,
timeinfo
.
tm_year
+
1900
,
timeinfo
.
tm_mon
+
1
,
timeinfo
.
tm_mday
,
timeinfo
.
tm_hour
,
timeinfo
.
tm_min
,
timeinfo
.
tm_sec
,
finalString
);
ESP_LOGI
(
TAG
,
"%s"
,
REQUEST
);
if
(
write
(
s
,
REQUEST
,
strlen
(
REQUEST
))
<
0
)
{
ESP_LOGE
(
TAG
,
"... socket send failed"
);
close
(
s
);
vTaskDelay
(
4000
/
portTICK_PERIOD_MS
);
continue
;
}
ESP_LOGI
(
TAG
,
"... socket send success"
);
struct
timeval
receiving_timeout
;
receiving_timeout
.
tv_sec
=
5
;
receiving_timeout
.
tv_usec
=
0
;
r
=
setsockopt
(
s
,
SOL_SOCKET
,
SO_RCVTIMEO
,
&
receiving_timeout
,
sizeof
(
receiving_timeout
));
if
(
r
<
0
)
{
ESP_LOGE
(
TAG
,
"... failed to set socket receiving timeout"
);
close
(
s
);
vTaskDelay
(
4000
/
portTICK_PERIOD_MS
);
continue
;
}
ESP_LOGI
(
TAG
,
"... set socket receiving timeout success"
);
/* Read HTTP response */
do
{
bzero
(
recv_buf
,
sizeof
(
recv_buf
));
r
=
read
(
s
,
recv_buf
,
sizeof
(
recv_buf
)
-
1
);
for
(
int
i
=
0
;
i
<
r
;
i
++
)
{
putchar
(
recv_buf
[
i
]);
}
}
while
(
r
>
0
);
ESP_LOGI
(
TAG
,
"... done reading from socket. Last read return=%d errno=%d
\r\n
"
,
r
,
errno
);
close
(
s
);
for
(
int
countdown
=
10
;
countdown
>=
0
;
countdown
--
)
{
ESP_LOGI
(
TAG
,
"%d... "
,
countdown
);
vTaskDelay
(
1000
/
portTICK_PERIOD_MS
);
}
ESP_LOGI
(
TAG
,
"Starting again!"
);
}
}
static
void
initialize_sntp
(
void
)
{
ESP_LOGI
(
TAG
,
"Initializing SNTP"
);
sntp_setoperatingmode
(
SNTP_OPMODE_POLL
);
sntp_setservername
(
0
,
"pool.ntp.org"
);
sntp_init
();
}
void
get_data
(
void
*
parameter
)
{
int
counter
=
0
;
float
temp
;
float
sum
=
0
;
while
(
1
){
counter
+=
1
;
temp
=
(
temprature_sens_read
()
-
32
)
/
1
.
8
;
// convert temperature from F' to C'
//printf("ESP32 core Temperature = %f\n", temp);
vTaskDelay
(
3000
/
portTICK_PERIOD_MS
);
sum
+=
temp
;
if
(
counter
==
10
){
average_temp
=
sum
/
10
;
sum
=
0
;
counter
=
0
;
printf
(
"Task 2:: updated the average core Temperature: %d
\n
"
,
average_temp
);
}
}
}
static
void
obtain_time
(
void
)
{
initialise_wifi
();
xEventGroupWaitBits
(
wifi_event_group
,
CONNECTED_BIT
,
false
,
true
,
portMAX_DELAY
);
initialize_sntp
();
// wait for time to be set
time_t
now
=
0
;
struct
tm
timeinfo
=
{
0
};
int
retry
=
0
;
const
int
retry_count
=
10
;
while
(
timeinfo
.
tm_year
<
(
2019
-
1900
)
&&
++
retry
<
retry_count
)
{
ESP_LOGI
(
TAG
,
"year: %d"
,
timeinfo
.
tm_year
);
ESP_LOGI
(
TAG
,
"Waiting for system time to be set... (%d/%d)"
,
retry
,
retry_count
);
vTaskDelay
(
2000
/
portTICK_PERIOD_MS
);
time
(
&
now
);
localtime_r
(
&
now
,
&
timeinfo
);
}
// ESP_ERROR_CHECK( esp_wifi_stop() );
}
void
app_main
()
{
ESP_ERROR_CHECK
(
nvs_flash_init
()
);
xTaskCreate
(
&
http_get_task
,
"http_get_task"
,
4096
,
NULL
,
1
,
NULL
);
xTaskCreate
(
&
get_data
,
"get_data"
,
10000
,
NULL
,
2
,
NULL
);
}
Write
Preview
Markdown
is supported
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