/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

byte inByte[] = {0x00, 0x00};

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*****************************";

//inkomende data
String inData = "";

//LEDS
int ledGroen = 16;
int ledGeel = 14;
int ledRood = 12;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "********";
char pass[] = "*********";


//Zet de kachel aan of uit
BLYNK_WRITE(V1) {

}

//Lees de ingestelde kracht van de kachel
BLYNK_WRITE(V2) {

}

//Lees de current kracht van de kachel
BLYNK_WRITE(V3) {
byte message1B[] = {0x00, 0x37};
Serial.write(message1B, sizeof(message1B));
//Serial.println("Verzonden");
delay(200);

if (Serial.available() > 1) {
// get incoming byte:
for (int i = 0; i < 2 ; i++) {
Serial.print("Gelezen: ");
Serial.println(Serial.peek());
inByte[i] = Serial.read();
}
}

Blynk.virtualWrite(V30, inByte[0]);
Blynk.virtualWrite(V31, inByte[1]);

delay(1000);
}

BLYNK_WRITE(V30) {

}


//Stel de kracht van de kachel in
BLYNK_WRITE(V4) {
// receive the step value as param.asInt() and then use switch to run different code
switch(param.asInt()){
case 1:
{
//Zet kachel op stand 1
byte message1A[] = {0x80, 0x37, 0x19, 0xd0};
byte message1B[] = {0x00, 0x60, 0x19, 0x19};
Blynk.virtualWrite(V2, 1);
Serial.write(message1A, sizeof(message1A));
delay(200);
Serial.write(message1B, sizeof(message1B));
}
break;
case 2:
{
//Zet kachel op stand 2
byte message2A[] = {0x80, 0x37, 0x1a, 0xd1};
byte message2B[] = {0x00, 0x60, 0x1a, 0x1a};
Blynk.virtualWrite(V2, 2);
Serial.write(message2A, sizeof(message2A));
delay(200);
Serial.write(message2B, sizeof(message2B));
}
break;
case 3:
{
//Zet kachel op stand 3
byte message3A[] = {0x80, 0x37, 0x1b, 0xd2};
byte message3B[] = {0x00, 0x60, 0x1b, 0x1b};
Blynk.virtualWrite(V2, 3);
Serial.write(message3A, sizeof(message3A));
delay(200);
Serial.write(message3B, sizeof(message3B));
}
break;
case 4:
{
//Zet kachel op stand 4
byte message4A[] = {0x80, 0x37, 0x1c, 0xd3};
byte message4B[] = {0x00, 0x60, 0x1c, 0x1c};
Blynk.virtualWrite(V2, 4);
Serial.write(message4A, sizeof(message4A));
delay(200);
Serial.write(message4B, sizeof(message4B));
}
break;
}
}

void setup()
{
pinMode(ledGroen, OUTPUT);
pinMode(ledGeel, OUTPUT);
pinMode(ledRood, OUTPUT);

// Debug console
Serial.begin(1200,SERIAL_8N2);

Blynk.begin(auth, ssid, pass);
while(!Blynk.connected())
{
digitalWrite(ledGeel, HIGH);
delay(500);
digitalWrite(ledGeel, LOW);
delay(500);
}

if(Blynk.connected())
{
digitalWrite(ledGeel, LOW);
digitalWrite(ledRood, LOW);
digitalWrite(ledGroen, HIGH);
} else {
digitalWrite(ledRood, HIGH);
digitalWrite(ledGroen, LOW);
digitalWrite(ledGeel, LOW);
}

// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
Blynk.run();
}