main_demo.c
2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "libwismart.h"
#include "libwismart_irqs.h" /* implement irq handlers */
#include "lwip/inet.h"
#include "globals.h"
#include "httpClient.h"
#include "callbacks.h"
//#include "buffer.h"
#include "json.h"
#include "ntp.h"
#include "i2c.h"
#define WIFI_MODE WIFI_MODE_CLIENT
#define NETWORK_SSID "linksys"
#define NETWORK_KEY ""
#define WPA_USER "smartcities"
#define WPA_PASS "superpass"
uint8_t connected=0;
uint8_t timeout=0;
uint8_t retries=0;
void initLibwismart(void)
{
wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
libwismart_Init(hwif);
}
int main(void)
{
struct wpa_param wpa;
wpa.eap_method = WISMART_EAP_METHOD_TTLS;
wpa.u.ttls.identity=WPA_USER;
wpa.u.ttls.password=WPA_PASS;
wpa.u.ttls.ca_cert=NULL;
initLibwismart();
libwismart_PowerSave_Enable();
libwismart_PowerSave_HigherProfile(TRUE);
libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
libwismart_WiFiInit();
libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb);
printf("WiSmart initialized\r\n");
while(!connected)
{chThdSleepMilliseconds(500);}
uint16_t received_data;
char answer[10];
I2C_init();
printf("I2C Initialized\r\n");
I2C_start(I2C1,0x39 << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x0C);
I2C_stop(I2C1);
printf("I2C Temperature measurment request sent\r\n");
I2C_start(I2C1, 0x39<<1, I2C_Direction_Receiver);
printf("I2C Read request sent\r\n");
received_data = I2C_read_ack(I2C1);
received_data = received_data << 8;
received_data = received_data | I2C_read_ack(I2C1);
printf("I2C data received\r\n");
sprintf(answer,"%d",received_data);
printf("Data: %s\r\n",answer);
char **buffer;
buffer = chHeapAlloc(NULL,sizeof (char *) * 1);
Date time=getDate(getSecsSince1900());
libwismart_EnableBsdSocketAPI();
buffer[0] = timestamp_data(answer,time);
printf("Init JSON statement\r\n");
char* statement = prepare_json_observation_statement(buffer,1);
chHeapFree(buffer[0]);
printf("Statement: ");
printf("%s\r\n",statement);
uint8_t result = send_json(statement, strlen(statement), "bmp", "085");
if(result == JSON_POST_OK)
{
printf("JSON_POST_OK\r\n");
}
else if(result == JSON_COMM_ERROR)
{
printf("JSON_COMM_ERROR\r\n");
}
else if(result == JSON_OTHER_ERROR)
{
printf("JSON_OTHER_ERROR\r\n");
}
else
{
printf("WTF I don't even\r\n");
}
for(;;)
{
chThdSleepMilliseconds(500);
}
}