#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <DNSServer.h>
//#include <ESP8266mDNS.h>
#include <EEPROM.h>

#include <OneWire.h>
#include <DallasTemperature.h>
/*
 * This example serves a "hello world" on a WLAN and a SoftAP at the same time.
 * The SoftAP allow you to configure WLAN parameters at run time. They are not setup in the sketch but saved on EEPROM.
 * 
 * Connect your computer or cell phone to wifi network ESP_ap with password 12345678. A popup may appear and it allow you to go to WLAN config. If it does not then navigate to http://192.168.4.1/wifi and config it there.
 * Then wait for the module to connect to your wifi and take note of the WLAN IP it got. Then you can disconnect from ESP_ap and return to your regular WLAN.
 * 
 * Now the ESP8266 is in your network. You can reach it through http://192.168.x.x/ (the IP you took note of) or maybe at http://esp8266.local too.
 * 
 * This is a captive portal because through the softAP it will redirect any http request to http://192.168.4.1/
 */
 
/* Set these to your desired softAP credentials. They are not configurable at runtime */
const char *softAP_ssid = "AAAP";
const char *softAP_password = "12345678";

/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
const char *myHostname = "AllAuto";

/* Don't set this wifi credentials. They are configurated at runtime and stored on EEPROM */
// char ssid[32] = "";
// char password[32] = "";

int connRes = 0 ;

// added from a7
String pinnum = "0O4I0O0O5OEIFI" ;
int buttonpin = 0 ; 
//buttonpin = 0 ; // pins(1)
int pumppin1 = 4 ; // 4 ;
//pumppin1 = pins(3) ; // 4 ;
int buttpin = 0 ; // 0 ;
//buttpin = pins(5) ; // 0 ;
int heaterpin = 0 ; // 0 ;
//heaterpin = pins(7) ; // 0 ;
int acpin = 5 ; // 5 ;
//acpin = pins(9) ; // 5 ;
int pumppin2 = 0 ;
//pumppin2 = pins(11) ;
int pumppin3 = 0 ;
//pumppin3 = pins(13) ;
/*
int io = pins(1) ; //.toInt() ; // 0;
int pumppin1 = pins(3) ; // 4 ;
int buttpin = pins(5) ; // 0 ;
int heaterpin = pins(7) ; // 0 ;
int acpin = pins(9) ; // 5 ;
int pumppin2 = pins(11) ;
int pumppin3 = pins(13) ;
*/
/*
String pins = pinnum.substring(1,1) ;
pins = pinnum.substring(2,2) ;
int pumppin = pins.toInt() ; // 4 ;
pins = pinnum.substring(3,3) ;
int buttpin = pins.toInt() ; // 0 ;
pins = pinnum.substring(4,4) ;
int heaterpin = pins.toInt() ; // 0 ;
pins = pinnum.substring(5,5) ;
int acpin = pins.toInt() ; // 5 ;
*/
#define BUTTON         buttonpin
#define PUMP1          pumppin1
#define BUTT           buttpin
#define HEATP          heaterpin
#define ACP            acpin
#define PUMP2          pumppin2
#define PUMP3          pumppin3
//endo of add a7


// DNS server
const byte DNS_PORT = 53;

/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);
DNSServer dnsServer;
// Web server
ESP8266WebServer server(80);

/** Should I connect to WLAN asap? */
boolean connect;

/** Last time I tried to connect to WLAN */
long lastConnectTry = 0;

/** Current WLAN status */
int status = WL_IDLE_STATUS;

String connectnet = "" ;
// added code from A7

String wheretosend = "GET http://boatautomation.com/jojo/sendtoAPI-1.php" ;
const char http_site[] = "75.103.94.221";
const int http_port = 80;
int timetotestwifi = 300 ; // 300 ; // wait 300 seconds in ap mode 
String ipadd = "";
String ipStr = "";
uint8_t MAC_array[6];
char MAC_char[22];
//String who = "single temp server with switch - apserverA3";
//String devicetype = "DB18s20 Single Temp";
String who = "ap-esp12";
String devicetype = "" ; // "DB18s20 Single Temp";
String firsteeprom = "9" ;
int contowifi = 0 ;

String clearchar = " ";
int numtemp = 0; 
int delaytime = 1 ; // 1 second
int precision = 100 ; // .01
String tempreading = "C" ;
float temptemp; // temp   read from the thermometer
float temptemp2; // temp   read from the thermometer
float voltvolt; // volt read from the adc

const char* APssid = "AAAP"; // Name of access point

String st;
String rsid = ""; // "Jojo" ; //"UNITE-52ED";
String rpass = "" ; // "jojowifi" ; //"22329280";
long rssi ;
String wifiname = "" ;

boolean newSSID = false;
int ss = 0 ;
int x = 0;
int y = 0 ;
int v = 0 ;
int t = 0 ;
int t2 = 0 ;
int pb = 0 ;
int pbstatus = 0 ;
int pbtime = 0 ;
int freq = 3600 ; //  transmit the data every 1 hour
int frequency = freq ;
int details = 0 ;
//int details = 1 ;
 
String clientcode = "" ;// "d2ViaG9va3M6cGFzc3dvcmQ=" ;
float adjtemp = 0 ;
float adjtemp2 = 0 ;
float adjvolt = 29.6 ;

int thermostat =1 ;
int heattostart = 25 ;
int actostart = 22 ;
int aircond = 0 ;
int heater = 0 ;
int buttonupdt = 1 ;

int resolution = 9 ;

float oldtemp = 0;
float newtemp = 0;
float oldtemptest = 200;
float oldtemp2 = 0;
float newtemp2 = 0;
float oldtemptest2 = 200;
float oldvolt = 0;
float newvolt = 0;
float oldvolttest = 200;

float tempdiff = .5 ; // degrees diffenece to report
float voltdiff = .5 ; // volts diffenece to report

float vprecision = 10;
int  rebootfreq = 999; // reboot every 24 hours by default
String strfreq = "" ;
String strrequest = "" ;
String strreqanswer = "" ;
String webString = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
String devicetodo = "" ;

String whatis = "" ; 
String tempv="";

float valuedev1 = 0 ;
float valuedev2 = 0 ;
float valuedev3 = 0 ;
int changey = 0;

int rsid_from = 1 ;
int rsid_to = rsid_from + 32 ;
int rpass_from = rsid_to ;
int rpass_to = rpass_from + 64 ; // 97
int freq_from = rpass_to ;
int freq_to = freq_from + 6 ; // 103
int rebootfreq_from = freq_to ;
int rebootfreq_to = rebootfreq_from + 3; // 106
int clientcode_from = rebootfreq_to ;
int clientcode_to = clientcode_from + 30 ; // 136
int adjtemp_from = clientcode_to ;
int adjtemp_to = adjtemp_from + 4; // 140
int adjtemp2_from = adjtemp_to ;
int adjtemp2_to = adjtemp2_from + 4; // 144
int resolution_from = adjtemp2_to ;
int resolution_to = resolution_from + 1 ; // 145
int button_from = resolution_to ;
int button_to = button_from + 1 ; // 146
int tempreading_from = button_to ;
int tempreading_to = tempreading_from + 7 ;
int precision_from = tempreading_to ;
int precision_to = precision_from + 3 ;
int tempdiff_from = precision_to ; 
int tempdiff_to = tempdiff_from + 3 ;
int delaytime_from = tempdiff_to ;
int delaytime_to = delaytime_from + 4 ;
int whatis_from = delaytime_to;
int whatis_to = whatis_from + 1 ;
int pinnum_from = whatis_to ;
int pinnum_to = pinnum_from + 5 ;
int adjvolt_from = pinnum_to;
int adjvolt_to = adjvolt_from +5;
int voltdiff_from = adjvolt_to ;
int voltdiff_to = voltdiff_from + 3;
int vprecision_from = voltdiff_to ;
int vprecision_to = vprecision_from + 3 ;
int actostart_from = vprecision_to ;
int actostart_to = actostart_from + 5 ; 
int heattostart_from = actostart_to ;
int heattostart_to = heattostart_from + 5 ; 
int eeprom_from = 0 ;
int eeprom_to = heattostart_to ; // button_to ;


String s = "";
// relay pin
//// #define BUTTON          0
// button state
int current = 0;
int currentold = 0 ;
int curr = 0;
int currold = 0 ;
int pushbutttime = 0 ;
// pump state
int Pcurrent = 0;
int Pcurrentold = 0 ;

int last = -1;

//initialize 1-wire bus
#define ONE_WIRE_BUS 2
//OneWire oneWire(ONE_WIRE_BUS);
//DallasTemperature sensors(&oneWire);
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
//endo of add a7

void setup() {
  delay(1000);
  Serial.begin(115200);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH);
  Serial.setDebugOutput(true);
  Serial.println("");
  Serial.print("Pulled IO 0 high");
  Serial.println();
  WiFi.macAddress(MAC_array);
  for (int i = 0; i < sizeof(MAC_array); ++i){
      sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
  }
  Serial.println("");
  Serial.print("Mac address = ");  
  Serial.println(MAC_char);


  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(softAP_ssid, softAP_password);
//  WiFi.softAP(softAP_ssid);
  delay(1000); // Without delay I've seen the IP address blank
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());

  /* Setup the DNS server redirecting all the domains to the apIP */  
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "*", apIP);

  /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
  server.on("/", handleRoot);
  server.on("/wifi", handleWifi);
  server.on("/wifisave", handleWifiSave);
  server.on("/generate_204", handleRoot);  //Android captive portal. Maybe not needed. Might be handled by notFound handler.
  server.on("/fwlink", handleRoot);  //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
  server.onNotFound ( handleNotFound );
  server.begin(); // Web server start
  Serial.println("HTTP server started");
  if (!ifCredentials() ){
    Serial.println ("in if eeprom empty");
//    Serial.print("1st character is ") ; Serial.println(char(EEPROM.read(0)));
  }
  else 
  {
    Serial.println ("in else eeprom empty");
//    Serial.print("1st character is ") ; Serial.println(char(EEPROM.read(0)));

    loadCredentials(); // Load WLAN credentials from network
//    connect = strlen(rsid) > 0; // Request WLAN connect if there is a SSID
    connectWifi();
  }

}

void connectWifi() {
  Serial.println("Connecting as wifi client...");
  WiFi.disconnect();

  //WiFi.begin ( ssid, password );
  rsid.trim();
  rpass.trim();

  WiFi.begin(rsid.c_str(), rpass.c_str());

  connRes = WiFi.waitForConnectResult();
  Serial.print ( "connRes: " );
  Serial.println ( connRes );
}

void loop() {

  x += 1 ;
//  if (connect && x == 100) {
  if (connect ) {
    Serial.println ( "Connect requested" );
    connect = false;
    connectWifi();
    x = 0 ;
    lastConnectTry = millis();
    Serial.print ( "in if connect - lastConnectTry = " );
    Serial.println ( lastConnectTry );
  }
  {
//    int s = WiFi.status();
    ss = WiFi.status();
//      Serial.print ( "Status 1: " );
//      Serial.println ( s );
    
    if (ss == 0 && millis() > (lastConnectTry + 60000) ) {
      //* If WLAN disconnected and idle try to connect 
      //* Don't set retry time too low as retry interfere the softAP operation 
      Serial.print ( "in if ss = 0 - lastConnectTry = " );
      Serial.println ( lastConnectTry );
      
      connect = true;
    }
    if (status != ss) { // WLAN status change
      Serial.print ( "Status 2: " );
      Serial.println ( ss );
      status = ss;
//      if (s == WL_CONNECTED) {
        //* Just connected to WLAN 
      if (ss == 3) {
        contowifi = 1;
        Serial.println ( "" );
        Serial.print ( "Connected to " );
        Serial.println ( rsid );
        Serial.print ( "IP address: " );
        Serial.println ( WiFi.localIP() );

        // Setup MDNS responder
 //       if (!MDNS.begin(myHostname)) {
 //         Serial.println("Error setting up MDNS responder!");
 //       } else {
 //         Serial.println("mDNS responder started");
 //         // Add service to MDNS-SD
 //         MDNS.addService("http", "tcp", 80);
 //       }
      } 
//      else { //  if (s == WL_NO_SSID_AVAIL) {
      else if (ss == WL_NO_SSID_AVAIL) {
        WiFi.disconnect();
        Serial.println("in else wl connected");
        if (rsid != "") {
          contowifi = 2;
          Serial.println("in if rsid, contowifi = 2");
        }
//        rsid = "";
//        rpass = "";

      }
    }
  }

  // Do work:  //DNS
  dnsServer.processNextRequest();
//  Serial.println("below dns");

  //HTTP
  server.handleClient();
//  Serial.println("below handle client");

}

