main/task_mqtt.c

changeset 18
12506716211c
parent 15
64028e178ff1
child 24
74609f70411e
equal deleted inserted replaced
17:1599e696d947 18:12506716211c
241 241
242 242
243 static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) 243 static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
244 { 244 {
245 char *topic = NULL; 245 char *topic = NULL;
246 nvs_handle_t my_handle;
247 246
248 switch (event->event_id) { 247 switch (event->event_id) {
249 248
250 case MQTT_EVENT_CONNECTED: 249 case MQTT_EVENT_CONNECTED:
251 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); 250 ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
285 break; 284 break;
286 285
287 case MQTT_EVENT_DATA: 286 case MQTT_EVENT_DATA:
288 ESP_LOGI(TAG, "MQTT_EVENT_DATA len=%d", event->data_len); 287 ESP_LOGI(TAG, "MQTT_EVENT_DATA len=%d", event->data_len);
289 bool gotit = false; 288 bool gotit = false;
290 esp_err_t err;
291 char data[65]; 289 char data[65];
292 if (event->data_len < 65) 290 if (event->data_len < 65)
293 snprintf(data, 64, "%.*s", event->data_len, event->data); 291 snprintf(data, 64, "%.*s", event->data_len, event->data);
294 else 292 else
295 data[0] = '\0'; 293 data[0] = '\0';
297 topic = topic_base(); 295 topic = topic_base();
298 topic = xstrcat(topic, (char *)"output/set/1"); 296 topic = xstrcat(topic, (char *)"output/set/1");
299 if (strncmp(topic, event->topic, event->topic_len) == 0) { 297 if (strncmp(topic, event->topic, event->topic_len) == 0) {
300 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); 298 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data));
301 gotit = true; 299 gotit = true;
302 if ((uint8_t)atoi(data) != Relay1) { 300 Relay1 = (uint8_t)atoi(data);
303 Relay1 = (uint8_t)atoi(data); 301 nvsio_write_u8((char *)"out1", Relay1);
304 err = nvs_open("balkon", NVS_READWRITE, &my_handle);
305 if (err == ESP_OK) {
306 nvs_set_u8(my_handle, (char *)"out1", Relay1);
307 nvs_commit(my_handle);
308 nvs_close(my_handle);
309 }
310 }
311 } 302 }
312 free(topic); 303 free(topic);
313 topic = NULL; 304 topic = NULL;
314 305
315 topic = topic_base(); 306 topic = topic_base();
316 topic = xstrcat(topic, (char *)"output/set/2"); 307 topic = xstrcat(topic, (char *)"output/set/2");
317 if (strncmp(topic, event->topic, event->topic_len) == 0) { 308 if (strncmp(topic, event->topic, event->topic_len) == 0) {
318 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); 309 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data));
319 gotit = true; 310 gotit = true;
320 if ((uint8_t)atoi(data) != Relay2) { 311 Relay2 = (uint8_t)atoi(data);
321 Relay2 = (uint8_t)atoi(data); 312 nvsio_write_u8((char *)"out2", Relay2);
322 err = nvs_open("balkon", NVS_READWRITE, &my_handle);
323 if (err == ESP_OK) {
324 nvs_set_u8(my_handle, (char *)"out2", Relay2);
325 nvs_commit(my_handle);
326 nvs_close(my_handle);
327 }
328 }
329 } 313 }
330 free(topic); 314 free(topic);
331 topic = NULL; 315 topic = NULL;
332 316
333 topic = topic_base(); 317 topic = topic_base();
334 topic = xstrcat(topic, (char *)"output/set/3"); 318 topic = xstrcat(topic, (char *)"output/set/3");
335 if (strncmp(topic, event->topic, event->topic_len) == 0) { 319 if (strncmp(topic, event->topic, event->topic_len) == 0) {
336 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); 320 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data));
337 gotit = true; 321 gotit = true;
338 if ((uint8_t)atoi(data) != Dimmer3) { 322 Dimmer3 = (uint8_t)atoi(data);
339 Dimmer3 = (uint8_t)atoi(data); 323 nvsio_write_u8((char *)"out3", Dimmer3);
340 err = nvs_open("balkon", NVS_READWRITE, &my_handle);
341 if (err == ESP_OK) {
342 nvs_set_u8(my_handle, (char *)"out3", Dimmer3);
343 nvs_commit(my_handle);
344 nvs_close(my_handle);
345 }
346 }
347 } 324 }
348 free(topic); 325 free(topic);
349 topic = NULL; 326 topic = NULL;
350 327
351 topic = topic_base(); 328 topic = topic_base();
352 topic = xstrcat(topic, (char *)"output/set/4"); 329 topic = xstrcat(topic, (char *)"output/set/4");
353 if (strncmp(topic, event->topic, event->topic_len) == 0) { 330 if (strncmp(topic, event->topic, event->topic_len) == 0) {
354 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); 331 ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data));
355 gotit = true; 332 gotit = true;
356 if ((uint8_t)atoi(data) != Dimmer4) { 333 Dimmer4 = (uint8_t)atoi(data);
357 Dimmer4 = (uint8_t)atoi(data); 334 nvsio_write_u8((char *)"out4", Dimmer4);
358 err = nvs_open("balkon", NVS_READWRITE, &my_handle);
359 if (err == ESP_OK) {
360 nvs_set_u8(my_handle, (char *)"out4", Dimmer4);
361 nvs_commit(my_handle);
362 nvs_close(my_handle);
363 }
364 }
365 } 335 }
366 free(topic); 336 free(topic);
367 topic = NULL; 337 topic = NULL;
368 338
369 if (! gotit) { 339 if (! gotit) {

mercurial