1

Topic: reconnect cloud server

Connection solutions cloud server


/*
   -- 0000 --
   
   This source code of graphical user interface
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 2.4.3 or later version
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.5.1 or later version;
     - for iOS 1.4.1 or later version;
   
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.   
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP8266WIFI_LIB_CLOUD
#include <ESP8266WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "YOUR_SSID"
#define REMOTEXY_WIFI_PASSWORD "YOUR_PASS"
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "YOUR_TOKEN"
#define REMOTEXY_ACCESS_PASSWORD "1234"

// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,1,0,3,0,24,0,10,24,1,
  65,23,10,36,39,39,1,4,11,37,
  37,37,25,31,79,78,45,79,70,70,
  0 };
 
// this structure defines all the variables and events of your control interface
struct {

    // input variables
  uint8_t button_1; // =1 if button pressed, else =0

    // output variables
  uint8_t led_1_r; // =0..255 LED Red brightness
  uint8_t led_1_g; // =0..255 LED Green brightness
  uint8_t led_1_b; // =0..255 LED Blue brightness

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_BUTTON_1 D4
bool wfconnect;
bool Switch1 = false;
WiFiClient CloudRemoteXY;
unsigned long timesevercheck = 0;

void cloudchecker(){
  int retxycloudserver = CloudRemoteXY.connect(REMOTEXY_CLOUD_SERVER,REMOTEXY_CLOUD_PORT);
  if ( retxycloudserver == 1 ) {
     Serial.print("cloud Connected.");
     }else{
     Serial.print("cloud NOT Connect.");
     remotexy = new CRemoteXY (RemoteXY_CONF_PROGMEM, &RemoteXY, REMOTEXY_ACCESS_PASSWORD, REMOTEXY_WIFI_SSID, REMOTEXY_WIFI_PASSWORD, REMOTEXY_CLOUD_SERVER, REMOTEXY_CLOUD_PORT, REMOTEXY_CLOUD_TOKEN);
     wfconnect = false;
     Serial.print("Reconnect cloud server");
     }
}


void setup(){
    Serial.begin(115200);
    //RemoteXY_Init ();
 
     pinMode (PIN_BUTTON_1, OUTPUT);
     remotexy = new CRemoteXY (RemoteXY_CONF_PROGMEM, &RemoteXY, REMOTEXY_ACCESS_PASSWORD, REMOTEXY_WIFI_SSID, REMOTEXY_WIFI_PASSWORD, REMOTEXY_CLOUD_SERVER, REMOTEXY_CLOUD_PORT, REMOTEXY_CLOUD_TOKEN);
     
  // TODO you setup code
    RemoteXY.led_1_b = 255;
     if ( WiFi.status() == WL_CONNECTED ) {
     wfconnect = true;
     Serial.print("Wifi Connected.");
     }
  timesevercheck = millis(); 
}

void loop()
{
  RemoteXY_Handler ();
//wifi checker & reconnect to remotexy
if(wfconnect == true){
      if ( WiFi.status() != WL_CONNECTED ) {
      remotexy = new CRemoteXY (RemoteXY_CONF_PROGMEM, &RemoteXY, REMOTEXY_ACCESS_PASSWORD, REMOTEXY_WIFI_SSID, REMOTEXY_WIFI_PASSWORD, REMOTEXY_CLOUD_SERVER, REMOTEXY_CLOUD_PORT, REMOTEXY_CLOUD_TOKEN);
      wfconnect = false;
      Serial.println("Wifi DisConnected.");
      Serial.println("Wifi Reonnect");
      }
  }else if(wfconnect == false){   
    if ( WiFi.status() == WL_CONNECTED ) {
     wfconnect = true;
     Serial.println("Wifi ReConnected.");
     Serial.println( F("My IP address: "));
     Serial.println(WiFi.localIP());
     }
  }
///server cloudchecker
if ( (millis() - timesevercheck >= 60000 )) {
     Serial.println("sever checker"); 
     cloudchecker();
     timesevercheck = millis();
     
      }


  digitalWrite(PIN_BUTTON_1, (RemoteXY.button_1==0)?LOW:HIGH);

  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay()


}