Commit f72e80afbb2cd387f2f664b85e492ab26955248d
1 parent
08e2e9ee
--no commit message
Showing
2 changed files
with
155 additions
and
12 deletions
Project/applications/smartcities/json.c
@@ -26,7 +26,7 @@ char* prepare_json_register_statement(module mod, sensor sens) | @@ -26,7 +26,7 @@ char* prepare_json_register_statement(module mod, sensor sens) | ||
26 | char ID[MODULE_ID_LENGTH+3]; | 26 | char ID[MODULE_ID_LENGTH+3]; |
27 | length = 23; | 27 | length = 23; |
28 | strcpy(ID,mod.ID); | 28 | strcpy(ID,mod.ID); |
29 | - sprintf(ID+MODULE_ID_LENGTH,"%d",sens.ID); | 29 | + sprintf(ID+MODULE_ID_LENGTH,"%X",sens.ID); |
30 | str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); | 30 | str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); |
31 | length += MODULE_ID_LENGTH+2; | 31 | length += MODULE_ID_LENGTH+2; |
32 | str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); | 32 | str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); |
@@ -98,23 +98,21 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen | @@ -98,23 +98,21 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen | ||
98 | if(sensor_ID == NULL) //Register sensor | 98 | if(sensor_ID == NULL) //Register sensor |
99 | { | 99 | { |
100 | URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); | 100 | URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); |
101 | - strcpy(URL,"http://"); | ||
102 | - strcpy(URL+7,SERVER_HOSTNAME); | 101 | + strcpy(URL,SERVER_HOSTNAME); |
103 | PATH = (char*) chHeapAlloc(NULL,10+strlen(provider_ID)); | 102 | PATH = (char*) chHeapAlloc(NULL,10+strlen(provider_ID)); |
104 | strcpy(PATH,"/catalog/"); | 103 | strcpy(PATH,"/catalog/"); |
105 | strcpy(PATH+9,provider_ID); | 104 | strcpy(PATH+9,provider_ID); |
106 | } | 105 | } |
107 | else //Post data | 106 | else //Post data |
108 | { | 107 | { |
109 | - URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); | ||
110 | - strcpy(URL,"http://"); | ||
111 | - strcpy(URL+7,SERVER_HOSTNAME); | ||
112 | - URL[7+strlen(SERVER_HOSTNAME)] = '\0'; | ||
113 | - PATH = (char*) chHeapAlloc(NULL,8+strlen(provider_ID)+strlen(sensor_ID)); | ||
114 | - strcpy(PATH,"/data/"); | ||
115 | - strcpy(PATH+6,provider_ID); | ||
116 | - strcpy(PATH+6+strlen(provider_ID),sensor_ID); | ||
117 | - PATH[7+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; | 108 | + URL = (char*) chHeapAlloc(NULL,1+strlen(SERVER_HOSTNAME)); |
109 | + strcpy(URL,SERVER_HOSTNAME); | ||
110 | + URL[strlen(SERVER_HOSTNAME)] = '\0'; | ||
111 | + PATH = (char*) chHeapAlloc(NULL,22+strlen(provider_ID)+strlen(sensor_ID)); | ||
112 | + strcpy(PATH,"/data/modularupload/"); | ||
113 | + strcpy(PATH+20,provider_ID); | ||
114 | + strcpy(PATH+20+strlen(provider_ID),sensor_ID); | ||
115 | + PATH[21+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; | ||
118 | } | 116 | } |
119 | struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; | 117 | struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; |
120 | response_code = httpRequest(server, statement, length); | 118 | response_code = httpRequest(server, statement, length); |
Project/applications/smartcities/main_demo.c
0 → 100644
1 | +#include "libwismart.h" | ||
2 | +#include "libwismart_irqs.h" /* implement irq handlers */ | ||
3 | +#include "lwip/inet.h" | ||
4 | +#include "globals.h" | ||
5 | +#include "httpClient.h" | ||
6 | +#include "callbacks.h" | ||
7 | +//#include "buffer.h" | ||
8 | +#include "json.h" | ||
9 | +#include "ntp.h" | ||
10 | +#include "i2c.h" | ||
11 | + | ||
12 | +#define WIFI_MODE WIFI_MODE_CLIENT | ||
13 | +#define NETWORK_SSID "linksys" | ||
14 | +#define NETWORK_KEY "" | ||
15 | +#define WPA_USER "smartcities" | ||
16 | +#define WPA_PASS "superpass" | ||
17 | + | ||
18 | +uint8_t connected = 0; | ||
19 | + | ||
20 | +void initLibwismart(void) | ||
21 | +{ | ||
22 | + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); | ||
23 | + libwismart_Init(hwif); | ||
24 | +} | ||
25 | + | ||
26 | +int main(void) | ||
27 | +{ | ||
28 | + struct wpa_param wpa; | ||
29 | + wpa.eap_method = WISMART_EAP_METHOD_TTLS; | ||
30 | + wpa.u.ttls.identity=WPA_USER; | ||
31 | + wpa.u.ttls.password=WPA_PASS; | ||
32 | + wpa.u.ttls.ca_cert=NULL; | ||
33 | + initLibwismart(); | ||
34 | + libwismart_PowerSave_Enable(); | ||
35 | + libwismart_PowerSave_HigherProfile(TRUE); | ||
36 | + libwismart_RegisterDhcpCB(dhcp_connect_result_cb); | ||
37 | + libwismart_WiFiInit(); | ||
38 | + libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb); | ||
39 | + | ||
40 | + printf("WiSmart initialized\r\n"); | ||
41 | + | ||
42 | + while(!connected) | ||
43 | + {chThdSleepMilliseconds(500);} | ||
44 | + | ||
45 | + uint16_t received_data; | ||
46 | + char answer[10]; | ||
47 | + | ||
48 | + I2C_init(); | ||
49 | + printf("I2C Initialized\r\n"); | ||
50 | + /*I2C_start(I2C1,0x77 << 1, I2C_Direction_Transmitter); | ||
51 | + I2C_write(I2C1, 0xF4); | ||
52 | + I2C_write(I2C1, 0x2E); | ||
53 | + I2C_stop(I2C1); | ||
54 | + printf("I2C Temperature measurment started\r\n"); | ||
55 | + | ||
56 | + I2C_start(I2C1,0x77 << 1, I2C_Direction_Transmitter); | ||
57 | + I2C_write(I2C1, 0xF6); | ||
58 | + I2C_stop(I2C1); | ||
59 | + printf("I2C Temperature measurment request sent\r\n"); | ||
60 | + | ||
61 | + I2C_start(I2C1, 0x77<<1, I2C_Direction_Receiver); | ||
62 | + printf("I2C Read request sent\r\n"); | ||
63 | + received_data = I2C_read_ack(I2C1); | ||
64 | + received_data = received_data << 8; | ||
65 | + received_data = received_data | I2C_read_ack(I2C1); | ||
66 | + printf("I2C data received\r\n"); | ||
67 | + | ||
68 | + sprintf(answer,"%d",received_data); | ||
69 | + printf("Data: %s\r\n",answer);*/ | ||
70 | + | ||
71 | + I2C_start(I2C1,0x39 << 1, I2C_Direction_Transmitter); | ||
72 | + I2C_write(I2C1, 0x0C); | ||
73 | + I2C_stop(I2C1); | ||
74 | + printf("I2C Temperature measurment request sent\r\n"); | ||
75 | + | ||
76 | + I2C_start(I2C1, 0x39<<1, I2C_Direction_Receiver); | ||
77 | + printf("I2C Read request sent\r\n"); | ||
78 | + received_data = I2C_read_ack(I2C1); | ||
79 | + received_data = received_data << 8; | ||
80 | + received_data = received_data | I2C_read_ack(I2C1); | ||
81 | + printf("I2C data received\r\n"); | ||
82 | + | ||
83 | + sprintf(answer,"%d",received_data); | ||
84 | + printf("Data: %s\r\n",answer); | ||
85 | + | ||
86 | + char **buffer; | ||
87 | + buffer = chHeapAlloc(NULL,sizeof (char *) * 1); | ||
88 | + | ||
89 | + udpNTP_Setup(); | ||
90 | + Date time=getDate(); | ||
91 | + | ||
92 | + libwismart_EnableBsdSocketAPI(); | ||
93 | + | ||
94 | + buffer[0] = timestamp_data(answer,time); | ||
95 | + | ||
96 | + printf("Init JSON statement\r\n"); | ||
97 | + char* statement = prepare_json_observation_statement(buffer,1); | ||
98 | + chHeapFree(buffer[0]); | ||
99 | + printf("Statement: "); | ||
100 | + printf("%s\r\n",statement); | ||
101 | + uint8_t result = send_json(statement, strlen(statement), "bmp", "085"); | ||
102 | + if(result == JSON_POST_OK) | ||
103 | + { | ||
104 | + printf("JSON_POST_OK\r\n"); | ||
105 | + } | ||
106 | + else if(result == JSON_COMM_ERROR) | ||
107 | + { | ||
108 | + printf("JSON_COMM_ERROR\r\n"); | ||
109 | + } | ||
110 | + else if(result == JSON_OTHER_ERROR) | ||
111 | + { | ||
112 | + printf("JSON_OTHER_ERROR\r\n"); | ||
113 | + } | ||
114 | + else | ||
115 | + { | ||
116 | + printf("WTF I don't even\r\n"); | ||
117 | + } | ||
118 | + | ||
119 | + for(;;) | ||
120 | + { | ||
121 | + /*// i2c get info= data | ||
122 | + char* data=NULL; | ||
123 | + put_message(data, buffer ,&ind); | ||
124 | + int res=check_memory(); | ||
125 | + if(res==SOFT_REACHED){ | ||
126 | + | ||
127 | + int ok=send(buffer, ind, "", ""); | ||
128 | + if(ok==JSON_COMM_ERROR) | ||
129 | + { | ||
130 | + printf("wismart is not connected\r\n"); | ||
131 | + } | ||
132 | + else if( ok==JSON_OTHER_ERROR){ | ||
133 | + printf("some error ocurred\r\n"); | ||
134 | + } | ||
135 | + else if(ok ==JSON_POST_OK){ | ||
136 | + printf(" send OK \r\n"); | ||
137 | + } | ||
138 | + } | ||
139 | + else if(res==HARD_REACHED){ | ||
140 | + destroy(buffer); | ||
141 | + }*/ | ||
142 | + chThdSleepMilliseconds(500); | ||
143 | + } | ||
144 | +} | ||
145 | + |