/*
   -- voltmeter v2 --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.11 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.11.4 or later version;
     - for iOS 1.9.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__ESP32CORE_WIFI_CLOUD
#include <WiFi.h>

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "Keko30388_official"
#define REMOTEXY_WIFI_PASSWORD "Kevin54312"
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "a7af53c4bfd05f1590d591d7e710cdf2"


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =   // 108 bytes
  { 255,1,0,60,0,101,0,16,29,1,67,5,36,23,23,9,2,26,6,67,
  5,5,37,23,9,2,26,11,67,5,12,1,39,16,36,25,7,67,5,36,
  37,23,9,2,26,6,67,5,5,23,23,9,2,26,6,67,5,36,64,23,
  9,2,26,6,67,5,36,51,23,9,2,26,6,67,5,5,64,23,9,2,
  26,6,67,5,5,51,23,9,2,26,6,2,0,5,79,22,11,2,26,31,
  31,79,78,0,79,70,70,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t switch_1; // =1 if switch ON and =0 if OFF 

    // output variables
  char battery_2[6];  // string UTF8 end zero 
  char battery_3[11];  // string UTF8 end zero 
  char all[7];  // string UTF8 end zero 
  char battery_4[6];  // string UTF8 end zero 
  char battery_1[6];  // string UTF8 end zero 
  char battery_8[6];  // string UTF8 end zero 
  char battery_6[6];  // string UTF8 end zero 
  char battery_7[6];  // string UTF8 end zero 
  char battery_5[6];  // string UTF8 end zero 

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

} RemoteXY;
#pragma pack(pop)

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


//wifi connection led
const int wifi = 12;

//battery 1
const int batteryInput1 = 34;
int bat1value = 0;
double bat1V;
float R1 = 15000;
//battery 2
const int batteryInput2 = 34;
int bat2value = 0;
double bat2V;
//battery 3
const int batteryInput3 = 34;
int bat3value = 0;
double bat3V;
//battery 4
const int batteryInput4 = 34;
int bat4value = 0;
double bat4V;
//battery 5
const int batteryInput5 = 34;
int bat5value = 0;
double bat5V;
//battery 6
const int batteryInput6 = 34;
int bat6value = 0;
double bat6V;
//battery 7
const int batteryInput7 = 34;
int bat7value = 0;
double bat7V;
//all
double allbatts;

//switch
const int relay = 32;

//DEBUG
bool serial = true;

void setup() 
{
  RemoteXY_Init (); 
  
  // TODO you setup code
  Serial.begin(9600);
  pinMode (wifi, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() 
{ 
  RemoteXY_Handler ();
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay() 
 
 if(RemoteXY_isConnected()==1)
 {
  digitalWrite(wifi, HIGH);
 }
 else
 {
  digitalWrite(wifi, LOW);
 }

//battery 1
  bat1value = analogRead(batteryInput1);
  bat1V = bat1value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat1V, 0, 2, RemoteXY.battery_1);
//LOG
  //Serial.println(bat1value);
  //Serial.print (bat1V);
  //Serial.println ("battery 1 voltage");

//battery 2
  bat2value = analogRead(batteryInput2);
  bat2V = bat2value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat2V, 0, 2, RemoteXY.battery_2);
//battery 3
  bat3value = analogRead(batteryInput3);
  bat3V = bat3value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat3V, 0, 2, RemoteXY.battery_3);
//battery 4
  bat4value = analogRead(batteryInput4);
  bat4V = bat4value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat4V, 0, 2, RemoteXY.battery_4);
//battery 5
  bat5value = analogRead(batteryInput5);
  bat5V = bat5value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat5V, 0, 2, RemoteXY.battery_5);
//battery 6
  bat6value = analogRead(batteryInput6);
  bat6V = bat6value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat1V, 0, 2, RemoteXY.battery_6);
//battery 7
  bat7value = analogRead(batteryInput7);
  bat7V = bat7value * (3.3/4095) * 2 + 0.25;
  dtostrf(bat7V, 0, 2, RemoteXY.battery_7);
//battery 8
  strcpy  (RemoteXY.battery_8, "----");
//all
  allbatts = bat1V + bat2V + bat3V + bat4V + bat5V + bat6V + bat7V;
  dtostrf(allbatts, 0, 2, RemoteXY.all);

//switch
  digitalWrite(relay, (RemoteXY.switch_1==0)?LOW:HIGH);



//DEBUG
if(serial == true)
{
  Serial.print (bat1V);
  Serial.println ("battery 1 voltage");

  Serial.print (bat2V);
  Serial.println ("battery 2 voltage");

  Serial.print (bat3V);
  Serial.println ("battery 3 voltage");

  Serial.print (bat4V);
  Serial.println ("battery 4 voltage");

  Serial.print (bat5V);
  Serial.println ("battery 5 voltage");

  Serial.print (bat6V);
  Serial.println ("battery 6 voltage");

  Serial.print (bat7V);
  Serial.println ("battery 7 voltage");
}
  RemoteXY_delay(1000);
}