int main()
{
	// check that we are running on Galileo or Edison
	mraa::Platform platform = mraa::getPlatformType();
	if ((platform != mraa::INTEL_GALILEO_GEN1) &&
			(platform != mraa::INTEL_GALILEO_GEN2) &&
			(platform != mraa::INTEL_EDISON_FAB_C)) {
		std::cerr << "Unsupported platform, exiting" << std::endl;
		return mraa::ERROR_INVALID_PLATFORM;
	}

	// create the MQTT client
	int rc = 0;
	rc = MQTTClient_create(&client, const_cast<char *>(host),
			const_cast<char *>(clientID), MQTTCLIENT_PERSISTENCE_NONE, NULL);
	if (rc != MQTTCLIENT_SUCCESS) {
		std::cerr << "Failed to create MQTT client, exiting" << std::endl;
		exit(rc);
	}

	// setup call backs before connecting the client to the server
	MQTTClient_setCallbacks(client, NULL,
			&connection_lost, NULL, &delivery_complete);

	MQTTClient_connectOptions data = MQTTClient_connectOptions_initializer;
	data.username = const_cast<char *>(username);
	data.password = const_cast<char *>(password);
	// connect the client to the server
	rc = MQTTClient_connect(client, &data);
	if (rc != MQTTCLIENT_SUCCESS) {
		std::cerr << "Failed to connect MQTT client, exiting" << std::endl;
		exit(rc);
	}

	// led connected to D3 (digital out)
	upm::GroveLed* led = new upm::GroveLed(3);

	// temperature sensor connected to A0 (analog in)
	upm::GroveTemp* temp_sensor = new upm::GroveTemp(0);

	// simple error checking
	if ((led == NULL) || (temp_sensor == NULL)) {
		std::cerr << "Can't create all objects, exiting" << std::endl;
		return mraa::ERROR_UNSPECIFIED;
	}

	// loop forever updating the temperature values every second
	for (;;) {
		temperature_update(temp_sensor, led);
		sleep(1);
	}

	printf("Stopping\n");

	int timeout = 100;
	MQTTClient_disconnect(client, timeout);
	MQTTClient_destroy(&client);
	return mraa::SUCCESS;
}
Esempio n. 2
0
File: tdm.c Progetto: gfornax/SiK
/// blink the radio LED if we have not received any packets
///
static void
link_update(void)
{
	static uint8_t unlock_count, temperature_count;
	if (received_packet) {
		unlock_count = 0;
		received_packet = false;
	} else {
		unlock_count++;
	}
	if (unlock_count < 6) {
		LED_RADIO = LED_ON;
	} else {
		LED_RADIO = blink_state;
		blink_state = !blink_state;
	}
	if (unlock_count > 40) {
		// if we have been unlocked for 20 seconds
		// then start frequency scanning again

		unlock_count = 5;
		// randomise the next transmit window using some
		// entropy from the radio if we have waited
		// for a full set of hops with this time base
		if (timer_entropy() & 1) {
			register uint16_t old_remaining = tdm_state_remaining;
			if (tdm_state_remaining > silence_period) {
				tdm_state_remaining -= packet_latency;
			} else {
				tdm_state_remaining = 1;
			}
			if (at_testmode & AT_TEST_TDM) {
				printf("TDM: change timing %u/%u\n",
				       (unsigned)old_remaining,
				       (unsigned)tdm_state_remaining);
			}
		}
		if (at_testmode & AT_TEST_TDM) {
			printf("TDM: scanning\n");
		}
		fhop_set_locked(false);
	}

	if (unlock_count != 0) {
		statistics.average_rssi = (radio_last_rssi() + 3*(uint16_t)statistics.average_rssi)/4;

		// reset statistics when unlocked
		statistics.receive_count = 0;
	}
	if (unlock_count > 5) {
		memset(&remote_statistics, 0, sizeof(remote_statistics));
	}

	test_display = at_testmode;
	send_statistics = 1;

	temperature_count++;
	if (temperature_count == 4) {
		// check every 2 seconds
		temperature_update();
		temperature_count = 0;
	}
}