#include <math.h>

//--- made by SKA ---
//--- test EtherEncLib 
// adapted by Renato Aloi
// May 2015
// removed SD Card part for future implementation

#include <SPI.h>
#include <EtherEncLib.h>
#if (ESP8266)
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif



static unsigned char ipaddr[] = { 192, 168, 1, 125 };
static unsigned char macaddr[] = { 0x00, 0x11, 0x22, 0x44, 0x00, 0x25 };

EtherEncLib eElib(80);

const PROGMEM char resp200Txt[] = {"HTTP/1.0 200 OK\n\rContent-Type: text/html\n\rPragma: no-cache\n\r\n\r"};

//sensor start

int DustSensePin = 13;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sample 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;


void setup() 
{
#if (ESP8266)
    Serial.begin(115200);
    pinMode(5,OUTPUT);	//--- ? -- SS pin must be output # by Renato Aloi
#else
    Serial.begin(9600);
    pinMode(8,OUTPUT);	//--- ? -- SS pin must be output # by Renato Aloi
#endif

  eElib.begin(ipaddr,macaddr);
  Serial.println(F("------ program start -----------"));
  //Serial.println(F("NO SDCARD version")); // by Renato Aloi
  
  pinMode(DustSensePin,INPUT);
    starttime = millis();//get the current time;
}

void loop() {
  
  duration = pulseIn(DustSensePin, LOW);
    lowpulseoccupancy = lowpulseoccupancy+duration;
    
    if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
    {
        ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
        
        //float exponential power - DOESN"T BUILD
        //concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
        
        //leaner exponential power 
        concentration = 1.1*ipow(ratio,3)-3.8*ipow(ratio,2)+520*ratio+0.62; // using spec sheet curve
        //Serial.print(lowpulseoccupancy);
        //Serial.print(",");
        //Serial.print(ratio);
        //Serial.print(",");
        //Serial.println(concentration);
        //lowpulseoccupancy = 0;
        starttime = millis();
        Serial.println("DataGet");
    }
    
  if ( eElib.available() ) 
  {
    Serial.println(eElib.getParams());
    eElib.print((char *)&resp200Txt[0],strlen_P(&resp200Txt[0]));
    if (eElib.isIndexHtml)
    {
      eElib.print("<HTML><body><H1>Hello World!</H1>");
      //eElib.print("<form method=POST>");
      eElib.print("<br>");
      eElib.print((int)lowpulseoccupancy);
      eElib.print("<br>");
      eElib.print((int)ratio);
      eElib.print("<br>");
      eElib.print((int)concentration);
      eElib.print("<br>");
      //eElib.print("</form></body>");
      eElib.print("</body></HTML>");
      
        //Serial.print(lowpulseoccupancy);
        //Serial.print(",");
        //Serial.print(ratio);
        //Serial.print(",");
        //Serial.println(concentration);
    }
    /*
    else if (eElib.isPost)
    {
      eElib.print("<HTML><body><H1>POST Params: ");
      eElib.print(eElib.getParams());
      eElib.print("</H1></body>");
      eElib.print("</HTML>");
    }
    else if (eElib.isGet)
    {
      eElib.print("<HTML><body><H1>GET Params: ");
      eElib.print(eElib.getParams());
      eElib.print("</H1></body>");
      eElib.print("</HTML>");
    }*/
    eElib.close();
  }
}

int ipow(int base, int exp)
{
    int result = 1;
    for (;;)
    {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        if (!exp)
            break;
        base *= base;
    }

    return result;
}
