Пример #1
0
void ICACHE_FLASH_ATTR publishTemperature(int idx) {
	struct Temperature *t;

	if (mqttIsConnected()) {
		char *topic = (char*) os_malloc(100), *data = (char*) os_malloc(100);
		if (topic == NULL || data == NULL) {
			ERRORP("malloc err %s/%s\n", topic, data);
			startFlash(-1, 50, 50); // fast
			return;
		}
		if (getUnmappedTemperature(idx, &t)) {
			os_sprintf(topic, (const char*) "/Raw/%s/%s/info", sysCfg.device_id, t->address);
			os_sprintf(data, (const char*) "{ \"Type\":\"Temp\", \"Value\":\"%c%d.%02d\"}", t->sign,
					t->val, t->fract);
			if (!MQTT_Publish(mqttClient, topic, data, os_strlen(data), 0, 0))
				printMQTTstate();
			INFOP("%s=>%s\n", topic, data);
		}
		checkMinHeap();
		os_free(topic);
		os_free(data);
	}
}
Пример #2
0
void ICACHE_FLASH_ATTR publishOutput(uint8 idx, uint8 val) {
	if (mqttIsConnected()) {
		char *topic = (char*) os_malloc(100), *data = (char*) os_malloc(200);
		if (topic == NULL || data == NULL) {
			ERRORP("malloc err %s/%s\n", topic, data);
			startFlash(-1, 20, 20); // fast
			return;
		}
		os_sprintf(topic, (const char*) "/Raw/%s/%d/info", sysCfg.device_id,
				idx + OUTPUT_SENSOR_ID_START);
		os_sprintf(data,
				(const char*) "{\"Name\":\"OP%d\", \"Type\":\"Output\", \"Value\":\"%d\"}", idx,
				val);
		INFOP("%s-->%s", topic, data);
		if (!MQTT_Publish(mqttClient, topic, data, os_strlen(data), 0, 0))
			printMQTTstate();
		checkMinHeap();
		os_free(topic);
		os_free(data);
	} else {
		INFOP("o/p %d--->%d\n", idx, val);
	}
}
Пример #3
0
void ICACHE_FLASH_ATTR publishDeviceReset(char *version, int lastAction) {
	if (mqttIsConnected()) {
		char *topic = (char *) os_zalloc(50);
		char *data = (char *) os_zalloc(200);
		int idx;
		if (topic == NULL || data == NULL) {
			ERRORP("malloc err %s/%s\n", topic, data);
			startFlash(-1, 50, 50); // fast
			return;
		}

		os_sprintf(topic, "/Raw/%10s/reset", sysCfg.device_id);
		os_sprintf(data,
			"{\"Name\":\"%s\", \"Location\":\"%s\", \"Version\":\"%s\", \"Reason\":%d, \"LastAction\":%d}",
			sysCfg.deviceName, sysCfg.deviceLocation, version, system_get_rst_info()->reason, lastAction);
		if (!MQTT_Publish(mqttClient, topic, data, os_strlen(data), 0, false))
			printMQTTstate();
		INFOP("%s=>%s\n", topic, data);
		checkMinHeap();
		os_free(topic);
		os_free(data);
	}
}
Пример #4
0
void ICACHE_FLASH_ATTR publishAllTemperatures(void) {
	struct Temperature *t;
	int idx;

	if (checkClient("publishAllTemperatures")) {
		char *topic = (char*) os_malloc(100), *data = (char*) os_malloc(100);
		if (!checkAlloc(topic, data)) return;

		for (idx = 0; idx < MAX_TEMPERATURE_SENSOR; idx++) {
			if (getUnmappedTemperature(idx, &t)) {
				os_sprintf(topic, (const char*) "/Raw/%s/%s/info", sysCfg.device_id, t->address);
				os_sprintf(data, (const char*) "{ \"Type\":\"Temp\", \"Value\":\"%c%d.%02d\"}",
						t->sign, t->val, t->fract);
				if (!MQTT_Publish(mqttClient, topic, data, os_strlen(data), 0, 0))
					printMQTTstate();
				TESTP("%s=>%s\n", topic, data);
			}
		}
		checkMinHeap();
		os_free(topic);
		os_free(data);
	}
}
Пример #5
0
void ICACHE_FLASH_ATTR otb_ds18b20_callback(void *arg)
{
  int chars;
  bool rc;
  otbDs18b20DeviceAddress *addr;
  char *sensor_loc;
  
  DEBUG("DS18B20: otb_ds18b20_callback entry");

  addr = (otbDs18b20DeviceAddress *)arg;
  DEBUG("DS18B20:    Device: %s", addr->friendly);
  DEBUG("DS18B20: timer_int: %d", addr->timer_int);
  DEBUG("DS18B20:     index: %d", addr->index);

  if (addr->timer_int != OTB_DS18B20_REPORT_INTERVAL)
  {
    // This must be the first time we were scheduled - we stagger first schedule.
    // So now reschedule on the right timer.
    addr->timer_int = OTB_DS18B20_REPORT_INTERVAL;
    os_timer_disarm((os_timer_t*)(otb_ds18b20_timer + addr->index));
    os_timer_setfn((os_timer_t*)(otb_ds18b20_timer + addr->index), (os_timer_func_t *)otb_ds18b20_callback, arg);
    // Set repeat to 1!
    os_timer_arm((os_timer_t*)(otb_ds18b20_timer + addr->index), OTB_DS18B20_REPORT_INTERVAL, 1);
    // Note this won't match the last of N DS18B20s - that will have this interval
    // already set, and when set that was tested and set to repeat.
  }

  // Read all this sensor.
  otb_ds18b20_prepare_to_read();
  rc = otb_ds18b20_request_temp(addr->addr,
                                otb_ds18b20_last_temp_s[addr->index]);
  if (!rc)
  {
    os_strcpy(otb_ds18b20_last_temp_s[addr->index], OTB_DS18B20_INTERNAL_ERROR_TEMP);
  }
    
  INFO("DS18B20: Device: %s temp: %s", addr->friendly, otb_ds18b20_last_temp_s[addr->index]);

  if (otb_mqtt_client.connState == MQTT_DATA)
  {
    DEBUG("DS18B20: Log sensor data");

    // Could send (but may choose not to), so reset disconnectedCounter
    otb_ds18b20_mqtt_disconnected_counter = 0;

    if (strcmp(otb_ds18b20_last_temp_s[addr->index], "-127.00") &&
        strcmp(otb_ds18b20_last_temp_s[addr->index], OTB_DS18B20_INTERNAL_ERROR_TEMP) &&
        strcmp(otb_ds18b20_last_temp_s[addr->index], "85.00"))
    {
      // Decided on reporting using a single format to reduce require MQTT buffer
      // size.
      // Setting qos = 0 (don't care if gets lost), retain = 1 (always retain last
      // publish)
      // XXX Should replace with otb_mqtt_publish call
      
      // Put friendly name for sensor in as well, if exists.
      sensor_loc = otb_ds18b20_get_sensor_name(addr->friendly, NULL);
      if (sensor_loc != NULL)
      {
        DEBUG("DS18B20: Sensor location: %s", sensor_loc);
        os_snprintf(otb_mqtt_scratch,
                    OTB_MQTT_MAX_MSG_LENGTH,
                    "%s/%s",
                    sensor_loc,
                    addr->friendly);
        sensor_loc = otb_mqtt_scratch;            
      }
      else
      {
        sensor_loc = addr->friendly;
      }
      
      os_snprintf(otb_mqtt_topic_s,
                  OTB_MQTT_MAX_TOPIC_LENGTH,
                  "/%s/%s/%s/%s/%s/%s/%s",
                  OTB_MQTT_ROOT,
                  OTB_MQTT_LOCATION_1,
                  OTB_MQTT_LOCATION_2,
                  OTB_MQTT_LOCATION_3,
                  OTB_MAIN_CHIPID,
                  OTB_MQTT_TEMPERATURE,
                  sensor_loc);
      DEBUG("DS18B20: Publish topic: %s", otb_mqtt_topic_s);
      DEBUG("DS18B20:       message: %s", otb_ds18b20_last_temp_s[addr->index]);
      chars = strlen(otb_ds18b20_last_temp_s[addr->index]);
      MQTT_Publish(&otb_mqtt_client, otb_mqtt_topic_s, otb_ds18b20_last_temp_s[addr->index], chars, 0, 1);
    }
  }
  else
  {
    WARN("DS18B20: MQTT not connected, so not sending");
    otb_ds18b20_mqtt_disconnected_counter += 1;
  }

  if ((otb_ds18b20_mqtt_disconnected_counter * OTB_DS18B20_REPORT_INTERVAL) >=
                                                    OTB_MQTT_DISCONNECTED_REBOOT_INTERVAL)
  {
    ERROR("DS18B20: MQTT disconnected %d ms so resetting", 
    otb_ds18b20_mqtt_disconnected_counter * OTB_DS18B20_REPORT_INTERVAL);
    otb_reset(otb_ds18b20_callback_error_string);
  }

  DEBUG("DS18B20: otb_ds18b20_callback exit");

  return;
}
Пример #6
0
bool MQTT::publish(const char* topic, const char* buf, uint32_t buf_len, int qos, int retain)
{
	return MQTT_Publish(&mqttClient, topic, buf, buf_len, qos, retain);
}
Пример #7
0
void avr_msg_cb (char *msg, int len) {
     os_printf("Ext: %s, %d\r\n", msg, len);
     if (mqttConnected)
	  MQTT_Publish(&mqttClient, "/clipdev", msg, len, 0, 0);
}
Пример #8
0
void ICACHE_FLASH_ATTR
mqttConnectedCb(uint32_t *args) {
  MQTT_Client* client = (MQTT_Client*)args;
  MQTT_Publish(client, "announce/all", "Hello World!", 0, 0);
}
Пример #9
0
//===========================================================
//		Kosio implementations
//===========================================================
int ICACHE_FLASH_ATTR ds18b20()
{
	int r, i, tempLength=0;
	uint8_t addr[8], data[12];
	ds_init();

	r = ds_search(addr);
	if(r)
	{
		console_printf("Found Device @ %02x %02x %02x %02x %02x %02x %02x %02x\r\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
		if(crc8(addr, 7) != addr[7])
			console_printf( "CRC mismatch, crc=%xd, addr[7]=%xd\r\n", crc8(addr, 7), addr[7]);

		switch(addr[0])
		{
		case 0x10:
			console_printf("Device is DS18S20 family.\r\n");
			break;

		case 0x28:
			console_printf("Device is DS18B20 family.\r\n");
			break;

		default:
			console_printf("Device is unknown family.\r\n");
			return 1;
		}
	}
	else {
		console_printf("No DS18B20 detected, sorry.\r\n");
		return 1;
	}
	// perform the conversion
	reset();
	select(addr);

	write(DS1820_CONVERT_T, 1); // perform temperature conversion

	sleepms(1000); // sleep 1s

	console_printf("Scratchpad: ");
	reset();
	select(addr);
	write(DS1820_READ_SCRATCHPAD, 0); // read scratchpad

	for(i = 0; i < 9; i++)
	{
		data[i] = read();
		console_printf("%2x ", data[i]);
	}
	console_printf("\r\n");

	int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
	LowByte = data[0];
	HighByte = data[1];
	TReading = (HighByte << 8) + LowByte;
	SignBit = TReading & 0x8000;  // test most sig bit
	if (SignBit) // negative
		TReading = (TReading ^ 0xffff) + 1; // 2's comp

	Whole = TReading >> 4;  // separate off the whole and fractional portions
	Fract = (TReading & 0xf) * 100 / 16;
	//MQTT stuff here
	console_printf("DS18B20/Temperature: %c%d.%d Celsius\r\n", SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract);
	if(SignBit){
		os_sprintf(DS18B20_Temperature, "%c%d.%d", SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract );
		MQTT_Publish(&mqttClient,DS18B20_MQTT_Temperature,DS18B20_Temperature,5,0,0);
	} else {
		os_sprintf(DS18B20_Temperature, "%d.%d", Whole, Fract < 10 ? 0 : Fract );
		MQTT_Publish(&mqttClient,DS18B20_MQTT_Temperature,DS18B20_Temperature,4,0,0);
	}
	// MANUAL MODE LOGICS HERE
	if (dDEVICE_MODE){
		if (Whole>=dBR_BOILER_SET&&GPIO_INPUT_GET(PIN_GPIO14)) {
			GPIO_OUTPUT_SET(PIN_GPIO14,0);
		}
	}
	return r;
}
Пример #10
0
// same as callback function in Arduino
void ICACHE_FLASH_ATTR mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
	char topicBuf[64], dataBuf[64];
	MQTT_Client* client = (MQTT_Client*)args;

	os_memcpy(topicBuf, topic, topic_len);
	topicBuf[topic_len] = 0;

	os_memcpy(dataBuf, data, data_len);
	dataBuf[data_len] = 0;

	// HERE STARTS THE BASIC LOGIC BY KONSTANTIN
// GPIO2 handling here
	if (strcmp(topicBuf,PIN_GPIO2_TOPIC)==0) {
		if (strcmp(dataBuf,"OFF")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO2, 0);
		INFO("GPIO2 set to OFF\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO2_TOPIC_CB,"OFF",3,0,0);
		currGPIO2State=0;
	} else if (strcmp(dataBuf,"ON")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO2, 1);
		INFO("GPIO2 set to ON\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO2_TOPIC_CB,"ON",2,0,0);
		currGPIO2State=1;
		}
	}
// GPIO4 handling here
	if (strcmp(topicBuf,PIN_GPIO4_TOPIC)==0) {
		if (strcmp(dataBuf,"OFF")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO4, 0);
		INFO("GPIO4 set to OFF\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO4_TOPIC_CB,"OFF",3,0,0);
		currGPIO4State=0;
	} else if (strcmp(dataBuf,"ON")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO4, 1);
		INFO("GPIO4 set to ON\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO4_TOPIC_CB,"ON",2,0,0);
		currGPIO4State=1;
		}
	}
// GPIO12 handling here
	if (strcmp(topicBuf,PIN_GPIO12_TOPIC)==0) {
		if (strcmp(dataBuf,"OFF")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO12, 0);
		INFO("GPIO12 set to OFF\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO12_TOPIC_CB,"OFF",3,0,0);
		currGPIO4State=0;
	} else if (strcmp(dataBuf,"ON")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO12, 1);
		INFO("GPIO12 set to ON\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO12_TOPIC_CB,"ON",2,0,0);
		currGPIO12State=1;
		}
	}
// GPIO5 handling here
	if (strcmp(topicBuf,PIN_GPIO5_TOPIC)==0) {
		if (strcmp(dataBuf,"OFF")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO5, 0);
		INFO("GPIO5 set to OFF\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO5_TOPIC_CB,"OFF",3,0,0);
		currGPIO4State=0;
	} else if (strcmp(dataBuf,"ON")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO5, 1);
		INFO("GPIO5 set to ON\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO5_TOPIC_CB,"ON",2,0,0);
		currGPIO5State=1;
		}
	}
// GPIO14 handling here
	if (strcmp(topicBuf,PIN_GPIO14_TOPIC)==0) {
		if (strcmp(dataBuf,"OFF")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO14, 0);
		INFO("GPIO14 set to OFF\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO14_TOPIC_CB,"OFF",3,0,0);
		currGPIO4State=0;
	} else if (strcmp(dataBuf,"ON")==0) {
		GPIO_OUTPUT_SET(PIN_GPIO14, 1);
		INFO("GPIO14 set to ON\r\n");
		MQTT_Publish(&mqttClient,PIN_GPIO14_TOPIC_CB,"ON",2,0,0);
		currGPIO14State=1;
		}
	}
//	BR_BOILER_SET
	if (strcmp(topicBuf,BR_BOILER_SET)==0) {
		dBR_BOILER_SET=atoi(dataBuf);
		INFO("BOILER SET TEMPERATURE RECEIVED: %d\r\n",dBR_BOILER_SET);
	}
	if (strcmp(topicBuf,BR_TEMP_ROOM_SET)==0) {
		dBR_TEMP_ROOM_SET=atoi(dataBuf);
		INFO("BATHROOM SET TEMPERATURE RECEIVED: %d\r\n",dBR_TEMP_ROOM_SET);
	}
	if (strcmp(topicBuf,BR_HUMIDITY_SET)==0) {
		dBR_HUMIDITY_SET=atoi(dataBuf);
		INFO("HUMIDITY SET RECEIVED: %d\r\n",dBR_HUMIDITY_SET);
	}
	if (strcmp(topicBuf,BR_MODE)==0) {
		dBR_MODE=atoi(dataBuf);
		INFO("BATHROOM MODE RECEIVED: %d\r\n",dBR_MODE);
	}

	// HERE ENDS THE BASIC LOGIC BY KONSTANTIN
//	INFO("GPIO2 State: %d",GPIO_INPUT_GET(PIN_GPIO));
	INFO("MQTT topic: %s, data: %s \r\n", topicBuf, dataBuf);
//	os_free(topicBuf);
//	os_free(dataBuf);
}
Пример #11
0
void ICACHE_FLASH_ATTR publish_roomba_status(MQTT_Client* client, char * message){
	MQTT_Publish(client, MQTT_ROOMBA_STATUS_TOPIC, message, strlen(message), 0, 0);
}