Commit ce796b71c09b9ba20752a5aa836d432aa93ebc83
1 parent
8e00635f
--no commit message
Showing
3 changed files
with
273 additions
and
39 deletions
Project/applications/smartcities/include/sensors.h
... | ... | @@ -5,22 +5,59 @@ |
5 | 5 | #include <stdio.h> |
6 | 6 | #include "i2c.h" |
7 | 7 | #include "libwismart.h" |
8 | +#include "module.h" | |
9 | +#include "json.h" | |
8 | 10 | |
11 | +#define LIGHT_ADDR 0x39 | |
12 | +#define DISTANCE_ADDR 0x01 | |
13 | +#define PRESSURE_ADDR 0x77 | |
9 | 14 | |
10 | 15 | typedef struct { |
11 | 16 | uint8_t ID; |
12 | 17 | char* description; |
13 | 18 | char* type; |
14 | 19 | char* unit; |
20 | + char* additional_data; | |
15 | 21 | } sensor; |
16 | 22 | |
17 | -// PRESSURE SENSOR FUNCTIONS | |
23 | +uint8_t register_sensor(sensor sens); | |
18 | 24 | |
25 | +//SENSOR DEFINITIONS | |
26 | +extern sensor light_sensor; | |
27 | +extern sensor ultrasound_sensor; | |
19 | 28 | extern sensor pressure_sensor; |
29 | + | |
30 | +//SENSOR FUNCTIONS | |
31 | + | |
32 | +//LIGHT SENSOR | |
33 | +uint32_t get_light_data(void); | |
34 | +uint16_t get_light_ch0(void); | |
35 | +uint16_t get_light_ch1(void); | |
36 | + | |
37 | +//ULTRASONIC SENSOR | |
38 | +uint16_t get_distance_data(void); | |
39 | +void init_ultrasound(void); | |
40 | +uint8_t get_distance_low(void); | |
41 | +uint8_t get_distance_high(void); | |
42 | + | |
43 | +//PRESSURE SENSOR | |
44 | +typedef struct { | |
45 | + uint16_t AC1; | |
46 | + uint16_t AC2; | |
47 | + uint16_t AC3; | |
48 | + uint16_t AC4; | |
49 | + uint16_t AC5; | |
50 | + uint16_t AC6; | |
51 | + uint16_t B1; | |
52 | + uint16_t B2; | |
53 | + uint16_t MB; | |
54 | + uint16_t MC; | |
55 | + uint16_t MD; | |
56 | +} bmp085_callibration; | |
57 | + | |
20 | 58 | uint16_t get_pressure_data(void); |
21 | -void pressure_start_conversion(void); | |
22 | -void pressure_get_coefficients(void); | |
23 | -uint16_t pressure_read_pressure_data(void); | |
24 | -uint16_t pressure_process_data(uint16_t data); | |
59 | +bmp085_callibration get_callibration_data(void); | |
60 | +char* callibration_data_csv(bmp085_callibration parameters); | |
61 | +void init_pressure(void); | |
25 | 62 | |
26 | 63 | #endif |
27 | 64 | \ No newline at end of file | ... | ... |
Project/applications/smartcities/json.c
... | ... | @@ -53,6 +53,12 @@ char* prepare_json_register_statement(module mod, sensor sens) |
53 | 53 | str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); |
54 | 54 | chHeapFree(str_aux2); |
55 | 55 | length += strlen(mod.geoloc); |
56 | + if(sens.additional_info != NULL) | |
57 | + { | |
58 | + str_aux2 = join_strings(str_aux,sens.additional_info,length,strlen(sens.additional_info),JOIN_NO_FREE); | |
59 | + chHeapFree(str_aux2); | |
60 | + length += strlen(sens.additional_info); | |
61 | + } | |
56 | 62 | json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); |
57 | 63 | chHeapFree(str_aux); |
58 | 64 | return json_statement; | ... | ... |
Project/applications/smartcities/sensors.c
1 | 1 | #include "sensors.h" |
2 | 2 | |
3 | -sensor pressure_sensor = {0xC0, "Presure sensor", "pressure", "KPa"}; /* append 0/1 flag for read write */ | |
3 | +sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux",NULL}; | |
4 | +sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm",NULL}; | |
5 | +sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "kPa",NULL}; | |
4 | 6 | |
5 | -uint16_t get_pressure_data() | |
7 | +uint8_t register_sensor(sensor sens) | |
6 | 8 | { |
7 | - I2C_init(); | |
8 | - printf("I2C Initialized\r\n"); | |
9 | - pressure_start_conversion(); | |
10 | - chThdSleepMilliseconds(10000); | |
11 | - pressure_get_coefficients(); | |
12 | - return pressure_process_data(pressure_read_pressure_data()); | |
9 | + uint8_t result; | |
10 | + if(sens.ID == PRESSURE_ADDR) | |
11 | + { | |
12 | + sens.additional_data = callibration_pressure_data_csv(get_pressure_callibration_data()); | |
13 | + } | |
14 | + char *statement = prepare_json_register_statement(mod,sens); | |
15 | + chHeapFree(sens.additional_data); | |
16 | + sens.additional_data = NULL; | |
17 | + result = send_json(statement,strlen(statement),mod.ID,sens.ID); | |
18 | + chHeapFree(statement); | |
19 | + return result; | |
13 | 20 | } |
14 | 21 | |
15 | -void pressure_start_conversion() | |
22 | +uint32_t get_light_data(void) | |
16 | 23 | { |
17 | - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); | |
18 | - I2C_write(I2C1, 0x12); | |
19 | - I2C_stop(I2C1); | |
20 | - printf("I2C Pressure measurment request sent\r\n"); | |
24 | + uint32_t data = 0; | |
25 | + | |
26 | + data = get_light_ch1(); | |
27 | + data = data << 16; | |
28 | + data = data | get_light_ch0(); | |
29 | + | |
30 | + return data; | |
21 | 31 | } |
32 | +uint16_t get_light_ch0(void) | |
33 | +{ | |
34 | + uint16_t data = 0; | |
35 | + | |
36 | + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | |
37 | + I2C_write(I2C1, 0x0D); | |
38 | + I2C_stop(I2C1); | |
39 | + | |
40 | + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | |
41 | + data = I2C_read_ack(I2C1); | |
42 | + data = data << 8; | |
43 | + | |
44 | + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | |
45 | + I2C_write(I2C1, 0x0C); | |
46 | + I2C_stop(I2C1); | |
47 | + | |
48 | + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | |
49 | + data = data | I2C_read_ack(I2C1); | |
50 | + I2C_stop(I2C1); | |
22 | 51 | |
23 | -void pressure_get_coefficients() | |
52 | + return data; | |
53 | +} | |
54 | +uint16_t get_light_ch1(void) | |
24 | 55 | { |
56 | + uint16_t data = 0; | |
57 | + | |
58 | + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | |
59 | + I2C_write(I2C1, 0x0F); | |
60 | + I2C_stop(I2C1); | |
61 | + | |
62 | + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | |
63 | + data = I2C_read_ack(I2C1); | |
64 | + data = data << 8; | |
65 | + | |
66 | + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | |
67 | + I2C_write(I2C1, 0x0E); | |
68 | + I2C_stop(I2C1); | |
25 | 69 | |
70 | + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | |
71 | + data = data | I2C_read_ack(I2C1); | |
72 | + I2C_stop(I2C1); | |
73 | + | |
74 | + return data; | |
26 | 75 | } |
27 | 76 | |
28 | -uint16_t pressure_read_pressure_data() | |
77 | +uint16_t get_distance_data(void) | |
29 | 78 | { |
30 | - uint16_t received_data; | |
31 | - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); | |
79 | + init_ultrasound(); | |
80 | + uint16_t data = get_distance_high(); | |
81 | + data = data << 8; | |
82 | + data = data | get_distance_low(); | |
83 | + return data; | |
84 | +} | |
85 | +void init_ultrasound(void) | |
86 | +{ | |
87 | + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | |
32 | 88 | I2C_write(I2C1, 0x00); |
33 | 89 | I2C_stop(I2C1); |
34 | - printf("I2C Read MSB pressure data sent\r\n"); | |
35 | 90 | |
36 | - I2C_start(I2C1, pressure_sensor.ID << 1, I2C_Direction_Receiver); | |
37 | - printf("I2C Read request sent\r\n"); | |
38 | - received_data = I2C_read_ack(I2C1); | |
39 | - received_data = received_data << 8; | |
40 | - printf("I2C data received\r\n"); | |
41 | - | |
42 | - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); | |
43 | - I2C_write(I2C1, 0x01); | |
91 | + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | |
92 | + I2C_write(I2C1, 0x50); | |
44 | 93 | I2C_stop(I2C1); |
45 | - printf("I2C Read LSB pressure data sent\r\n"); | |
46 | 94 | |
47 | - I2C_start(I2C1, pressure_sensor.ID << 1, I2C_Direction_Receiver); | |
48 | - printf("I2C Read request sent\r\n"); | |
49 | - received_data = received_data | I2C_read_ack(I2C1); | |
50 | - printf("I2C data received\r\n"); | |
51 | - return received_data; | |
95 | + chThdSleepMilliseconds(70); | |
96 | +} | |
97 | +uint8_t get_distance_low(void) | |
98 | +{ | |
99 | + uint8_t data; | |
100 | + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | |
101 | + I2C_write(I2C1, 0x03); | |
102 | + I2C_stop(I2C1); | |
103 | + | |
104 | + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | |
105 | + data = I2C_read_ack(I2C1); | |
106 | + I2C_stop(I2C1); | |
107 | + return data; | |
108 | +} | |
109 | +uint8_t get_distance_high(void) | |
110 | +{ | |
111 | + uint8_t data; | |
112 | + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | |
113 | + I2C_write(I2C1, 0x02); | |
114 | + I2C_stop(I2C1); | |
115 | + | |
116 | + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | |
117 | + data = I2C_read_ack(I2C1); | |
118 | + I2C_stop(I2C1); | |
119 | + return data; | |
52 | 120 | } |
53 | 121 | |
54 | -uint16_t pressure_process_data(uint16_t data) | |
122 | +uint16_t get_pressure_data(void) | |
55 | 123 | { |
124 | + uint16_t data; | |
125 | + init_pressure(); | |
126 | + | |
127 | + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | |
128 | + I2C_write(I2C1, 0xF6); | |
129 | + I2C_stop(I2C1); | |
130 | + | |
131 | + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | |
132 | + data = I2C_read_ack(I2C1); | |
133 | + data = data << 8; | |
134 | + data = data | I2C_read_nack(I2C1); | |
56 | 135 | return data; |
57 | -} | |
58 | 136 | \ No newline at end of file |
137 | +} | |
138 | + | |
139 | +bmp085_callibration get_pressure_callibration_data(void) | |
140 | +{ | |
141 | + bmp085_callibration calib_data; | |
142 | + | |
143 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
144 | + I2C_write(I2C1, 0xAA); | |
145 | + I2C_stop(I2C1); | |
146 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
147 | + calib_data.AC1 = I2C_read_ack(I2C1); | |
148 | + I2C_stop(I2C1); | |
149 | + | |
150 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
151 | + I2C_write(I2C1, 0xAC); | |
152 | + I2C_stop(I2C1); | |
153 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
154 | + calib_data.AC2 = I2C_read_ack(I2C1); | |
155 | + I2C_stop(I2C1); | |
156 | + | |
157 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
158 | + I2C_write(I2C1, 0xAE); | |
159 | + I2C_stop(I2C1); | |
160 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
161 | + calib_data.AC3 = I2C_read_ack(I2C1); | |
162 | + I2C_stop(I2C1); | |
163 | + | |
164 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
165 | + I2C_write(I2C1, 0xB0); | |
166 | + I2C_stop(I2C1); | |
167 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
168 | + calib_data.AC4 = I2C_read_ack(I2C1); | |
169 | + I2C_stop(I2C1); | |
170 | + | |
171 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
172 | + I2C_write(I2C1, 0xB2); | |
173 | + I2C_stop(I2C1); | |
174 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
175 | + calib_data.AC5 = I2C_read_ack(I2C1); | |
176 | + I2C_stop(I2C1); | |
177 | + | |
178 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
179 | + I2C_write(I2C1, 0xB4); | |
180 | + I2C_stop(I2C1); | |
181 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
182 | + calib_data.AC6 = I2C_read_ack(I2C1); | |
183 | + I2C_stop(I2C1); | |
184 | + | |
185 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
186 | + I2C_write(I2C1, 0xB6); | |
187 | + I2C_stop(I2C1); | |
188 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
189 | + calib_data.B1 = I2C_read_ack(I2C1); | |
190 | + I2C_stop(I2C1); | |
191 | + | |
192 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
193 | + I2C_write(I2C1, 0xB8); | |
194 | + I2C_stop(I2C1); | |
195 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
196 | + calib_data.B2 = I2C_read_ack(I2C1); | |
197 | + I2C_stop(I2C1); | |
198 | + | |
199 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
200 | + I2C_write(I2C1, 0xBA); | |
201 | + I2C_stop(I2C1); | |
202 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
203 | + calib_data.MB = I2C_read_ack(I2C1); | |
204 | + I2C_stop(I2C1); | |
205 | + | |
206 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
207 | + I2C_write(I2C1, 0xBC); | |
208 | + I2C_stop(I2C1); | |
209 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
210 | + calib_data.MC = I2C_read_ack(I2C1); | |
211 | + I2C_stop(I2C1); | |
212 | + | |
213 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
214 | + I2C_write(I2C1, 0xBE); | |
215 | + I2C_stop(I2C1); | |
216 | + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | |
217 | + calib_data.MD = I2C_read_ack(I2C1); | |
218 | + I2C_stop(I2C1); | |
219 | + | |
220 | + return calib_data; | |
221 | +} | |
222 | +char* callibration_pressure_data_csv(bmp085_callibration parameters) | |
223 | +{ | |
224 | + uint8_t i; | |
225 | + char *str = chHeapAlloc(NULL,sizeof(char)*(4*11-1)); | |
226 | + memset(str,0x00,sizeof(char)*(4*11-1)); | |
227 | + sprintf(str + strlen(str),"%d,",parameters.AC1); | |
228 | + sprintf(str + strlen(str),"%d,",parameters.AC2); | |
229 | + sprintf(str + strlen(str),"%d,",parameters.AC3); | |
230 | + sprintf(str + strlen(str),"%d,",parameters.AC4); | |
231 | + sprintf(str + strlen(str),"%d,",parameters.AC5); | |
232 | + sprintf(str + strlen(str),"%d,",parameters.AC6); | |
233 | + sprintf(str + strlen(str),"%d,",parameters.B1); | |
234 | + sprintf(str + strlen(str),"%d,",parameters.B2); | |
235 | + sprintf(str + strlen(str),"%d,",parameters.MB); | |
236 | + sprintf(str + strlen(str),"%d,",parameters.MC); | |
237 | + sprintf(str + strlen(str),"%d",parameters.MD); | |
238 | + return str; | |
239 | +} | |
240 | + | |
241 | +void init_pressure(void) | |
242 | +{ | |
243 | + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | |
244 | + I2C_write(I2C1, 0xF4); | |
245 | + I2C_write(I2C1, 0x34); | |
246 | + I2C_stop(I2C1); | |
247 | + | |
248 | + chThdSleepMilliseconds(5); | |
249 | +} | ... | ... |