Ejemplo n.º 1
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/#");
}
// 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...");
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
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;
        }
    }
}