示例#1
0
void bringUpMqtt()
{
    unsigned brokerConnectionTryLimit = 10;
    unsigned count = 0;
    // if we lost wifiConnectivity, we need to break out and
    // go back to bringing up wifi
    debugSerial.println("ARDUINO: Inside bringUpMqtt()");


    if( !mqttClient.init() )
    {
        debugSerial.println("ARDUINO: Bad news, mqtt client failed to initialize correctly");
        return;
    }

    while(!MqttClient::connected && (count < brokerConnectionTryLimit) )
    {
        debugSerial.println("ARDUINO: Connecting mqttClient");
        mqttClient.connect(SERVER_NAME, SERVER_PORT, false);
        // process 10 times
        espMultipleProcess(10);
        delay(100);
        espMultipleProcess(10);

        ++count;
    }
    debugSerial.println("ARDUINO: Leaving bringUpMqtt()");

    return;
}
// MQTT : Publish our message
void publishMessage()
{
	if (mqtt.getConnectionState() != eTCS_Connected) {
		startMqttClient(); // Auto reconnect
		return;
	}

	// Read DHT22
	TempAndHumidity th;
	if(!dht.readTempAndHumidity(th)) {
		return;
	}
	// Make JSON data
	String message = "{\"temp\":";
	message += th.temp;
	message += ", \"humi\":";
	message += th.humid;
	message += "}";

	// publish message
	Serial.println("Let's publish message now!");
	mqtt.publish("home/thirdroom/temp_humi", message, true); // retained message

	displayTemp(message);
}
示例#3
0
// Run MQTT client
void startMqttClient()
{
	if(!mqtt.setWill("last/will","The connection from this device is lost:(", 1, true)) {
		debugf("Unable to set the last will and testament. Most probably there is not enough memory on the device.");
	}
	mqtt.connect("esp8266");
	mqtt.subscribe("main/status/#");
}
示例#4
0
// Publish our message
void publishMessage()
{
	if (mqtt.getConnectionState() != eTCS_Connected)
		startMqttClient(); // Auto reconnect

	Serial.println("Let's publish message now!");
	mqtt.publish("main/frameworks/sming", "Hello friends, from Internet of things :)"); // or publishWithQoS
}
// Run MQTT client
void startMqttClient()
{
	if(!mqtt.setWill("last/will","The connection from this device is lost:(", 1, true)) {
		debugf("Unable to set the last will and testament. Most probably there is not enough memory on the device.");
	}
	mqtt.connect("ESP8266");
	mqtt.subscribe("messagebox");
	displayText("Start MQTT client...");
}
示例#6
0
// Will be called when WiFi station was connected to AP
void connectOk()
{
	Serial.println("I'm CONNECTED");

	// Run MQTT client
	mqtt.connect("esp8266");
	mqtt.subscribe("main/status/#");

	// Start publishing loop
	procTimer.initializeMs(20 * 1000, publishMessage).start(); // every 20 seconds
}
示例#7
0
bool sendCmd(Cmd cmd)
{
    static const char* itemCurtains = "curtains";
    static const char* itemLights = "overHeadLights";
    static const char* itemPixels = "pixelWall";
    static const char* itemFan = "fan";

    // figure out which itemName we are talking to
    const char* itemName = 0;
    switch(cmd)
    {
    case e_cmdButton1:
        itemName = itemCurtains;
        break;
    case e_cmdButton2:
        itemName = itemLights;
        break;
    case e_cmdButton3:
        itemName = itemPixels;
        break;
    case e_cmdButton4:
        itemName = itemFan;
        break;
    default:
        debugSerial.println("ARDUINO: Bad cmd passed to sendCmd()\n");
        itemName = "";
        return false;
    }
    char topic[32] = {0};
    sprintf(topic, "/%s/%s", "bedroom1", itemName);

    char mesg[64] = {0};
    sprintf(mesg, "{\"command\":\"toggle\"}");
    debugSerial.println("");
    debugSerial.println(topic);
    debugSerial.println(mesg);

    // Send out command over mqtt protocol
    mqttClient.publish(topic, mesg);

    return true;
}
示例#8
0
// Publish our message
void publishMessage()
{
	Serial.println("Let's publish message now!");
	mqtt.publish("main/frameworks/sming", "Hello friends, from Internet of things :)"); // or publishWithQoS
}
示例#9
0
int MqttClient::staticSendPacket(void* userInfo, const void* buf, unsigned int count)
{
	MqttClient* client = (MqttClient*)userInfo;
	bool sent = client->send((const char*)buf, count);
	return sent ? count : 0;
}
示例#10
0
void loop(void)
{

    if(!wifiConnected)
    {
        debugSerial.println("ARDUINO: Wifi down, bringing it up");
        bringUpWifi();

        espMultipleProcess(10);
        delay(1000);

    }

    if(!MqttClient::connected && wifiConnected)
    {
        debugSerial.println("ARDUINO: Mqtt down, bringing it up");
        bringUpMqtt();
        espMultipleProcess(10);
        delay(1000);
    }

    if(MqttClient::connected && !wifiConnected)
    {
        debugSerial.println("ARDUINO: Bad state. mqtt somehow connected and wifi isn't");
        debugSerial.println("ARDUINO: restarting state machine");
        wifiConnected = false;
        delay(1000);
    }

    // subscriptions!!!!
    if(wifiConnected && MqttClient::connected)
    {
        debugSerial.println("ARDUINO: All systems online!");
        // set subscriptions
        mqttClient.subscribe();
        espMultipleProcess(10);
        delay(1000);
    }

    while(wifiConnected && MqttClient::connected)
    {
        // Throw some clock cycles at the esp
        esp.process();

        buttonPressed = (PINC & 0x0F);

        if( buttonPressed )
        {
            // lastButtonPressed will be cleared after a time
            if( buttonPressed!=lastButtonPressed )
            {
                if( buttonPressed == BTN_1)
                {
                    ledsOff = !ledsOff;
                    forceLedUpdate = true;
                    debugSerial.println("Turning Button Leds");
                    debugSerial.println(ledsOff?"OFF":"ON");

                    //if( !sendCmd(e_cmdButton1) )
                    //{
                    //	debugSerial.println("Error sending e_cmdButton1");
                    //}
                }
                else if( buttonPressed == BTN_2 )
                {
                    if( !sendCmd(e_cmdButton2) )
                    {
                        debugSerial.println("ARDUINO: Error sending e_cmdButton2");
                    }
                }
                else if( buttonPressed == BTN_3 )
                {
                    if( !sendCmd(e_cmdButton3) )
                    {
                        debugSerial.println("ARDUINO: Error sending e_cmdButton3");
                    }
                }
                else if( buttonPressed == BTN_4 )
                {
                    if( !sendCmd(e_cmdButton4) )
                    {
                        debugSerial.println("ARDUINO: Error sending e_cmdButton4");
                    }
                }

                espMultipleProcess(5);
                delay(debounceDelay); //debounce delay
                // save lastButtonPressed to check against next time
                lastButtonPressed = buttonPressed;
                buttonPressed = 0x00;
            }
            else
            {
                // They pressed the same button again, restart the counter
                lastButtonPressExpireTimer = 0;
            }
        }
        if(lastButtonPressed)
        {
            ++lastButtonPressExpireTimer;
        }
        // After a while, we clear the lastButtonPressed
        // The point is to prevent a user holding a button
        if(lastButtonPressExpireTimer > lastButtonPressTimeout)
        {
            lastButtonPressed = 0x00;
            lastButtonPressExpireTimer = 0;
        }

        ++cyclesCount;

        if( (cyclesCount >= ledUpdateWaitCycles) || forceLedUpdate )
        {
            updateButtonLeds(ledsOff);
            cyclesCount = 0;
            forceLedUpdate = false;
        }
    }
}