bool MQTTWifiConnection::checkDataAvalible(void){

	// Reconnect MQTT if OFFLINE and not have Client (TcpServer)
	if (!mqtt.connected() && mqttTimeout.expired() &&  !WifiConnection::client.connected()) {
		if(WiFi.status() == WL_CONNECTED){
			mqttConnect();
		}
	}


	if (mqtt.connected()){
		Config.keepAlive = false; // on MQTT is not required

		checkOTA(); // check for OTA updates (because WifiConnection  is not called)

		mqtt.loop();
		mqttTimeout.disable();
		setStream(mqttClient);
		return DeviceConnection::checkDataAvalible();
	}else{ // TCP SERVER...
		if(!mqttTimeout.isEnabled()) mqttTimeout.enable();
		Config.keepAlive = true; // on raw TCP is  required
		return WifiConnection::checkDataAvalible();
	}

}
void MQTTWifiConnection::begin(){
	WifiConnection::begin();
	Logger.debug("MQTT", "BEGIN");
	mqtt.setServer(Config.server, MQTT_PORT);
	mqtt.setCallback(mqttCallback);
	mqttConnect();
}
void publish(const __FlashStringHelper * resource, float value) {

    String data;
    data += value;

    // Publish data and try to reconnect when publishing data fails
    if (!mqttClient.publish(resource, data.c_str())) {
        DebugSerial.println(F("Failed to publish, trying reconnect..."));
        mqttConnect();
    }
}
void loop() {
    // Check the serial port to see if there is a new packet available
    xbee.loop();
    Ethernet.maintain();

    if (millis() - last_send_time > 10000) {
        if (mqtt_response != 0) { // connect will return 0 for connected
            mqttClient.disconnect();
            mqttConnect();
        }
        last_send_time = millis();
    }
}
void setup() {
    // Setup debug serial output
    DebugSerial.begin(115200);
    DebugSerial.println(F("Coordinator Starting..."));

    // Setup XBee serial communication
    XBeeSerial.begin(9600);
    xbee.begin(XBeeSerial);
    delay(1);
    internetConnect();
    delay(500);
    mqttConnect();

    // Setup callbacks
    xbee.onPacketError(printErrorCb, (uintptr_t)(Print * ) & DebugSerial);
    xbee.onResponse(printErrorCb, (uintptr_t)(Print * ) & DebugSerial);
    xbee.onZBRxResponse(processRxPacket);
}
Exemple #6
0
bool BlindCloud::connected( const bool wait )
{
  // if network client has been disabled return..
  if (  !CLOUD_IS(STATE_ON) )
    return false;

  // if network client not initilized do it now!
  if ( !CLOUD_IS(STATE_INIT) ) {
    WiFi.mode(WIFI_STA);    // WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3
    
    WiFi.begin( m_ssid, m_password );
    Serial.printf("WiFi Mode is : %d\n", WiFi.getMode() );
    m_state |= STATE_INIT;
  }

  //if ( !checkWiFiConnection() )
    //m_state &= ~STATE_CONNECTED;

  bool ok = checkWiFiConnection();
  while ( !ok && wait ) {
    delay( 500 );
    Serial.print(".");
    ok = checkWiFiConnection();
  }
  
  if ( CLOUD_IS(STATE_CONNECTED) ) {
    if ( !CLOUD_IS(STATE_UDP_CONNECTED) ) {
      m_state |= STATE_UDP_CONNECTED;
      // connect to NTP time server
      m_udp.begin( NTP_SERVER_PORT );
    }

    mqttConnect();
    
    return true;
  }

  m_state &= ~STATE_UDP_CONNECTED;
  return false;
}
Exemple #7
0
void mqttAppConnect()
{
   mqttConnect(&mqtt);
}
Exemple #8
0
/**
 * 
 * @param e
 */
void ICACHE_FLASH_ATTR dispatcher_t::runTask(os_event_t* e)
{
    if(m_FirstRun == true) {
        m_FirstRun = false;

        os_printf("{\"jsonrpc\":\"2.0\",\"method\":\"system\",\"params\":{\"code\":1,\"sys\":\"system\",\"msg\":\"start\",\"p1\":null,\"p2\":null,\"n1\":0,\"n2\":0}}\n");
    }
    
    m_MqttClient.run(m_Seconds);
    
    int c = uart0_rx_one_char();

    //
    // TODO: handle too-long interval between indivdual characters - reset parser if interval too long
    //

    if(c != -1) {
        int ret = os()->jsonRpc()->parse(c);

        if(ret == EJSON_METHOD) {
            DTXT("%lu dispatcher_t::runTask(): parse() == EJSON_METHOD\n", millis());
            
            if(os_strcmp("mqtt_connect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_connect\n", millis());                
                mqttConnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("mqtt_disconnect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_disconnect\n", millis());                
                mqttDisconnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_publish", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_publish\n", millis());                
                mqttPublish(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_subscribe", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_subscribe\n", millis());                
                mqttSubscribe(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("mqtt_unsubscribe", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; mqtt_unsubscribe\n", millis());                
                mqttUnsubscribe(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());                
            }
            else if(os_strcmp("wifi_connect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_connect\n", millis());                
                wifiConnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("wifi_disconnect", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_disconnect\n", millis());                
                wifiDisconnect(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("wifi_scan", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; wifi_scan\n", millis());                
                wifiScan(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("sys_nop", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; sys_nop\n", millis());                
                sysNop(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else if(os_strcmp("sys_restart", os()->jsonRpc()->getMethod()) == 0) {
                DTXT("%lu dispatcher_t::runTask(): parse; sys_restart\n", millis());                
                sysRestart(os()->jsonRpc()->getParams(), os()->jsonRpc()->getMsgId());
            }
            else {
                DTXT("%lu dispatcher_t::runTask(): parse; unknown\n", millis());                
            }
            
            os()->jsonRpc()->parseReset();
        }
        else if(ret == EJSON_ERROR) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret == EJSON_ERROR\n", millis());
        }
        else if(ret == EJSON_RESULT) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret == EJSON_RESULT\n", millis());
        }
        else if(ret != EJSON_PARSE_INCOMPLETE && ret != EJSON_PARSE_ERR) {
            DTXT("%lu dispatcher_t::runTask(): parse; ret != EJSON_PARSE_INCOMPLETE && ret != EJSON_PARSE_ERR (%d)\n", millis(), ret);
        }
    }

#if defined(WITH_TESTCODE)
    int qos = 0;

    if(millis() - m_LastRun >= getRunInterval()) {
        DTXT("%lu dispatcher_t::runTask(): time to publish; freeHeap = %lu\n", millis(), os()->freeHeap());

        static char txt[60];
        
        os_sprintf(txt, "{\"seconds\":%lu,\"contact\":\"[email protected]\"}", m_Seconds);
        
        for(int i = 0; i < m_MqttClient.getMaxConnections() - 1; ++i) {            
            DTXT("%lu dispatcher_t::runTask(): m_Command = %d, m_Status = %d\n", millis(), m_MqttClient.getCommand(i), m_MqttClient.getStatus(i));

            if(m_MqttClient.isReady(i) == 1) {
                DTXT("%lu dispatcher_t::runTask(): slot %d is connected\n", millis(), i);

                if(m_MqttClient.publish(i, "fabric/esp8266/seconds", txt, os_strlen(txt), qos, MQTT_RETAIN) != 0) {
                    DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
                }
            }
        }
        
        m_LastRun = millis();
        ++m_Seconds;
    }

    static int x0 = 0;
    static int x1 = 0;
    static int x2 = 0;
    static int x3 = 0;

    if(x0 == 0 && m_MqttClient.isReady(0) == 1) {
        if(m_MqttClient.publish(0, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x0;
        }
    }
    
    if(x1 == 0 && m_MqttClient.isReady(1) == 1) {
        if(m_MqttClient.publish(1, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x1;
        }
    }

    if(x2 == 0 && m_MqttClient.isReady(2) == 1) {
        if(m_MqttClient.publish(2, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x2;
        }
    }
    
    if(x3 == 0 && m_MqttClient.isReady(3) == 1) {
        if(m_MqttClient.publish(3, "fabric/esp8266/status", "online", 6, qos, MQTT_RETAIN) != 0) {
            DTXT("%lu dispatcher_t::runTask(): could not publish\n", millis());                
        }
        else {
            ++x3;
        }
    }
#else
    if(millis() - m_LastRun >= getRunInterval()) {
        m_LastRun = millis();
        ++m_Seconds;
    }    
#endif
}