Exemplo n.º 1
0
void espMultipleProcess(short numTimes)
{
    for(short processCount = 0; processCount<numTimes; ++processCount)
    {
        esp.process();
        delay(20);
    }
}
CiaoData PassThroughRead(const char* connector, const char* hostname, String data, const char* method){


	//short mode = 0;

	if (!strcmp(connector, "rest")){
		//mode = 0;
		if (!strcmp(method, "GET")){
			return requestGET(hostname, data);
		}
		else if (!strcmp(method, "POST")){
			return requestPOST(hostname, data);
		}
		else{
			CiaoData ciao_data;
			ciao_data.msg_split[0]="";
			ciao_data.msg_split[1]="";
			ciao_data.msg_split[2]="Method Error";
			return ciao_data;
		}
	}
	else if (!strcmp(connector, "mqtt")){
		//mode = 1;
		CiaoData ciao_data;
		esp.process();
		check_topic(hostname);
		char topic[mqtt_topic.length()+1];
		mqtt_topic.toCharArray(topic,mqtt_topic.length()+1);
		if(mqtt_data_tmp != "" && !strcmp(hostname,topic)){
				mqtt_data = mqtt_data_tmp;
				ciao_data.msg_split[0]="mqtt";
				ciao_data.msg_split[1]= hostname;
				ciao_data.msg_split[2]= mqtt_data.c_str();
				mqtt_topic="";
				mqtt_data_tmp="";
		 }
		else{
				ciao_data.msg_split[0]="mqtt";
				ciao_data.msg_split[1]=hostname;
				ciao_data.msg_split[2]="";
		}
		return ciao_data;
	}
	else {
		CiaoData ciao_data;
		ciao_data.msg_split[0]="";
		ciao_data.msg_split[1]="";
		ciao_data.msg_split[2]="Protocol Error";
		return ciao_data;
	}
}
Exemplo n.º 3
0
void bringUpWifi()
{
    unsigned triesBeforeChipReset = 2000;
    unsigned count = 0;
    while(!wifiConnected)
    {
        // Start by putting chip in known state
        esp.enable();
        delay(500);
        hardResetEspChip();
        delay(1000);
        esp.reset();
        delay(500);
        while(!esp.ready())
        {
            digitalWrite(HEART_BEAT_LED, HIGH);
            delay(1000);
            digitalWrite(HEART_BEAT_LED, LOW);
            delay(1000);
            if(count >= 3)
            {
                debugSerial.println("ARDUINO: Resetting ESP chip");
                esp.enable();
                delay(500);
                hardResetEspChip();
                delay(1000);
                esp.reset();
                delay(500);
                count = 0;
            }

            count++;
        }


        /*setup wifi*/
        debugSerial.println("ARDUINO: Setting up wifi");
        esp.wifiConnect(SSID,PASSWORD);
        // We should chill here until we get a wifi connection
        while(!wifiConnected && (count < triesBeforeChipReset) )
        {
            esp.process();
            delay(100);
            ++count;
        }
        count = 0;
    }
}
Exemplo 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;
        }
    }
}