#include <Arduino.h>
#include "SoftwareSerial.h"
#include "Wire.h"
#include <Adafruit_MLX90614.h>
//#define DEBUG_SERIAL Serial
#include <SPI.h>
#include "SparkFun_Ublox_Arduino_Library.h"
SoftwareSerial mySerial(2,12);
SFE_UBLOX_GPS myGPS;
//== CHANGE THIS ============
double new_emissivity =0.95;
//===========================
#include <Wire.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
u_char ret[17];
#define MYPORT_TX 14
#define MYPORT_RX 13
SoftwareSerial myPort(MYPORT_RX,MYPORT_TX, false);
SoftwareSerial GPSSeiral(2, 12, false);
//HardwareSerial myPort(1);
//swSer(14, 12, false, 256);
u_char a[]={0x01,0x04,0x00,0x00,0x00 ,0x06,0x70,0x08};
u_char yuliang[]={0x02, 0x03, 0x00, 0x00 ,0x00, 0x01, 0x84, 0x39};
u_char ClearYL[]={0x02,0x06,0x00, 0x00 ,0x00 ,0x5A ,0X09 ,0Xc2};
u_char yuliangfuliangdu[]={0x02,0x03,0x00,0x02,0x00,0x02,0x65,0xf8};

u_char openGNRMC[]={0xF1,0xD9,0x06,0x01,0x03,0x00,0xF0,0x05,0x01,0x00,0x1A};
u_char gpsSaveConfig[]={0xF1,0xD9,0x06,0x09,0x08,0x00,0x00,0x00,0x00,0x00,0x2F,0x00,0x00,0x00,0x46,0xB7};
void getMLX()
{
    double a =mlx.readObjectTempC();
    Serial.write((char*) &a,8);
}
void getWehter()
{
    while (myPort.read()!=-1);
    myPort.write(a,8);



    delay(90);

    for (int j = 0; j <17 ; ++j) {
        ret[j]=myPort.read();
    }
    float VV=(ret[3]*256+ret[4])*1.0/100;
    float DD=(ret[5]*256+ret[6])*1.0/10;
    float TT=(ret[7]*256+ret[8])*1.0/100;
    float HH=(ret[9]*256+ret[10])*1.0/100;
    long PP=(ret[13]*256+ret[14])*256*256+(ret[11]*256+ret[12]);
#ifdef DEBG
    Serial.println("VV:"+String(VV)+" DD:"+String(DD)+" TT:"+String(TT)+" HH:"+String(HH)+" PP:"+String(PP));
#endif
    Serial.write(ret,17);

    //delay(2000);
}
void getYuliang()
{

  





    while (myPort.read()!=-1);
    myPort.write(yuliang,8);
    delay(90);

    for (int j = 0; j <7; ++j) {
        ret[j]=myPort.read();
    }
    float Value=ret[3]*256+ret[4];
    Value=Value/10;
   // Serial.write(ret,7);
    Serial.print(Value);
    Serial.print("#");

}

void getYuliangfuliang()
{
    while (myPort.read()!=-1);
    myPort.write(yuliangfuliangdu,8);
    delay(90);

    for (int j = 0; j <7; ++j) {
        ret[j]=myPort.read();
    }
    int32_t  Value=(ret[3]*256+ret[4])*256*256+ret[5]*256+ret[6];
   // Value=Value/10;
   // Serial.write(ret,7);
    Serial.print(Value);
     Serial.print("#");

}


void claeryuliang(){
        while (myPort.read()!=-1);
    myPort.write(ClearYL,8);
    delay(90);
        for (int j = 0; j <8; ++j) {
        ret[j]=myPort.read();
    }
    Serial.print("OK");
}
TwoWire wireme;
void onRequest() {
  //Wire.print(i++)
  Serial.println("onRequest");;
  Wire.print(" Packets.");
  
}

void onReceive(int len) {
  //Serial.printf("onReceive[%d]: ", len);
  Serial.println("onReceive:["+String(len)+"] ");
  while (Wire.available()) {
    Serial.write(Wire.read());
  }
  Serial.println();
}


void onReceive111(size_t len) {
 // Serial.printf("onReceive[%d]: ");
    Serial.println("onReceive111:["+String(len)+"] ");
  while (Wire.available()) {
    Serial.write(Wire.read());
  }
  Serial.println();
}




void setup() {
  // put your setup code here, to run once:
  Wire.begin(4,5);
  
    Serial.begin(115200);
    Serial.println("init ok");

      //Wire.pins(D1,D2);
      Wire.setClock(100000);
    Wire.begin(4,5,0x42);
  Wire.onReceive(onReceive111);
  Wire.onRequest(onRequest);
  return;





    myPort.begin(9600, SWSERIAL_8N1, MYPORT_RX, MYPORT_TX, false);
    pinMode(16,OUTPUT);
    digitalWrite(16,LOW);
    delay(2000);
    digitalWrite(16,HIGH);
    GPSSeiral.begin(115200, SWSERIAL_8N1, 2, 12, false);
    
    
    if (!mlx.begin(MLX90614_I2CADDR)) {
        Serial.println("Error connecting to MLX sensor. Check wiring.");
        //while (1);
    };
    mlx.writeEmissivity(new_emissivity); // this does the 0x0000 erase write
    Serial.println(sizeof(openGNRMC));
  int ii=10;  
do {
    Serial.println("GPS: trying 38400 baud");
    mySerial.begin(38400);
    if (myGPS.begin(mySerial) == true) break;

    delay(100);
    Serial.println("GPS: trying 9600 baud");
    mySerial.begin(9600);
    if (myGPS.begin(mySerial) == true) {
        Serial.println("GPS: connected at 9600 baud, switching to 38400");
        myGPS.setSerialRate(38400);
        delay(100);
    } else {
        //myGPS.factoryReset();
        delay(2000); //Wait a bit before trying again to limit the Serial output
    }
  } while(ii--);
  Serial.println("GPS serial connected");

  myGPS.setUART1Output(COM_TYPE_UBX); //Set the UART port to output UBX only
  myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
  myGPS.saveConfiguration(); //Save the current settings to flash and BBR
  ///myGPS.enableDebugging();


   /// for (size_t i = 0; i < 100; i++)
   // {
   //         GPSSeiral.write(openGNRMC,sizeof(openGNRMC));
    //            GPSSeiral.write(gpsSaveConfig,sizeof(gpsSaveConfig));
   //         delay(5);
        /* code */
   // }
    







}

String gettodvalue(int a)
{
    if (a<10)
    {
        return "0"+String(a);
        /* code */
    }else
    {
        return String(a);
    }
    
}
void loop() {
    Serial.println("loop");
    delay(200);
    return;
    if(Serial.available())
    {
        String str=Serial.readStringUntil('#');
        //Serial.println("data come");
        //Serial.println(str);
        if (str=="WindSensor")
        {
            getWehter();
        }
        if(str=="MLX")
        {
            getMLX();

        }
        if(str=="YULIANG")
        {
            
           getYuliang();
        }
        if(str=="YLFZ")
        {
            
           getYuliangfuliang();
        }
        if(str=="ClearYL")
        {
            
           claeryuliang();
        }
        if (str=="GETGPS")
        {
            
             Serial.println("Time_"+String(myGPS.getYear())
             +"-"+gettodvalue(myGPS.getMonth())
             +"-"+gettodvalue(myGPS.getDay())
             +" "+gettodvalue(myGPS.getHour())
             +":"+gettodvalue(myGPS.getMinute())
             +":"+gettodvalue(myGPS.getSecond())
             +"#Number_"+String(myGPS.getSIV())+"#Latitude_"+String(myGPS.getLatitude()/10000000.0,7)+"#Longtitude_"+String(myGPS.getLongitude()/10000000.0,7)+"\n");
            /* code */
        }
    }
   // myPort.println("adfsdafsdf");
    //getYuliang();
   // delay(1000);
   
   
    //GPSSeiral.print(Serial.readString());
   /// getWehter();
   // delay(1000);
}


