//MQTT receive
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  int svalue1, loc, StepperTarget;
  String sub, devicename;

  StaticJsonDocument<512> doc;

  sendCommand("p0.pic=55");
  sendCommand("tm1.en=1");

#ifdef DEBUG
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
#endif

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, payload);


  // Test if parsing succeeds.
  if (error) {
#ifdef DEBUG
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
#endif
    return;
  }

  //Show Status WIFI icon
  sendCommand("p0.pic=55");

  // Get the root object in the document
  JsonObject root = doc.as<JsonObject>();

  int idx = root["idx"];
  const char* idx_name = root["name"];
  const char* idx_svalue1 = root["svalue1"];
  const char* idx_svalue2 = root["svalue2"];
  const char* idx_svalue4 = root["svalue4"];

  int idx_nvalue = root["nvalue"];

#ifdef DEBUG
  Serial.println(idx);
  Serial.println(idx_name);
  Serial.println(idx_svalue1);
  Serial.println(idx_nvalue);
#endif


  //Get IDX IR config
  if (idx == -1)
  {
      idx_staandelamp = root["svalue1"];
      idx_haardverlichting = root["svalue2"];
      idx_tuinverlichting = root["svalue3"];
      idx_rooster = root["svalue4"];
      idx_hanglamp = root["svalue5"];
      idx_ir06 = root["svalue6"];
      idx_ir07 = root["svalue7"];
      idx_ir08 = root["svalue8"];
      idx_ir09 = root["svalue9"];
      idx_ir10 = root["svalue10"];
      idx_ir11 = root["svalue11"];
      idx_ir12 = root["svalue12"];
      idx_ir13 = root["svalue13"];
      idx_ir14 = root["svalue14"];
      idx_ir15 = root["svalue15"];
      idx_ir16 = root["svalue16"];
  }


  //Dim display
  if (idx == idx_display_dim)
  {
     if(root["nvalue"] >=1)
        sendCommand("dim=10");
     else
        sendCommand("dim=100");      
  }


  if (idx == idx_staandelamp)
  {
    if (root["nvalue"] >= 1)
      main_btn_staandelamp = 1;
    else
      main_btn_staandelamp = 0;

    btn_2.setValue(main_btn_staandelamp);
  }


  if (idx == idx_tuinverlichting)
  {
    if (root["nvalue"] >= 1)
      main_btn_tuinverlichting = 1;
    else
      main_btn_tuinverlichting = 0;

    btn_4.setValue(main_btn_tuinverlichting);
  }

  if (idx == idx_hanglamp)
  {
    if (root["nvalue"] >= 1)
      main_btn_hanglamp = 1;
    else
      main_btn_hanglamp = 0;

    btn_1.setValue(main_btn_hanglamp);
  }

  if (idx == idx_haardverlichting)
  {
    if (root["nvalue"] >= 1)
      main_btn_haardverlichting = 1;
    else
      main_btn_haardverlichting = 0;

    btn_3.setValue(main_btn_haardverlichting);
  }

  if (idx == idx_rooster)
  {
    if (root["nvalue"] == 1)
      main_btn_rooster = 1;
    else
      main_btn_rooster = 0;

 //   btn_x.setValue(main_btn_rooster);
  }

  if (idx == idx_ir06)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir06 = 1;
    else
      main_btn_ir06 = 0;

//    btn_1.setValue(main_btn_kaarsen);
  }

  if (idx == idx_ir07)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir07 = 1;
    else
      main_btn_ir07 = 0;
  }

  if (idx == idx_ir08)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir08 = 1;
    else
      main_btn_ir08 = 0;
  }

  if (idx == idx_ir09)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir09 = 1;
    else
      main_btn_ir09 = 0;
  }
  
  if (idx == idx_ir10)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir10 = 1;
    else
      main_btn_ir10 = 0;
  }

  if (idx == idx_ir11)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir11 = 1;
    else
      main_btn_ir11 = 0;
  }

  if (idx == idx_ir12)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir12 = 1;
    else
      main_btn_ir12 = 0;
  }

  if (idx == idx_ir13)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir13 = 1;
    else
      main_btn_ir13 = 0;
  }
  
  if (idx == idx_ir14)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir14 = 1;
    else
      main_btn_ir14 = 0;
  }
  
  if (idx == idx_ir15)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir15 = 1;
    else
      main_btn_ir15 = 0;
  }

  if (idx == idx_ir16)
  {
    if (root["nvalue"] >= 1)
      main_btn_ir16 = 1;
    else
      main_btn_ir16 = 0;
  }

  //Tijd
  if (idx == idx_datumtijd)
  {
#ifdef DEBUG
    //  Serial.println(root["svalue1"]);
#endif

    //  memcpy(datumtijd,root["svalue1"])
    datumtijd = root["svalue1"];
    //datumtijd[10] = '\0';

    //    txt_header.setText(datumtijd);
    nextion_send(nex_datumtijd, datumtijd);
    //  txt_1_header.setText(idx_svalue1);
  }

  //Weer
  if (idx == idx_weer_text)
  {
    weer_text = root["svalue1"];
    nextion_send(nex_weer_text, weer_text);
  }

  if (idx == idx_netatmo_woonkamer)
  {

#ifdef DEBUG
  Serial.print("!!!! idx 459 !!! ");
#endif
    

    main_temp_in = atof(root["svalue1"]);
    main_hum_in = atof(root["svalue2"]);
    main_baro = atof(root["svalue4"]);
    nex_send_float(nex_temp_in, main_temp_in, 4, 1);
    nex_send_float(nex_hum_in, main_hum_in, 2, 0);
    nex_send_float(nex_baro, main_baro, 4, 0);
  }

  //Min temperatuur
  if (idx == idx_temp_min)
  {
    main_temp_min = atof(root["svalue1"]);
    nex_send_float(nex_temp_min, main_temp_min, 4, 1);
  }

  //Max Temperatuur
  if (idx == idx_temp_max)
  {
    main_temp_max = atof(root["svalue1"]);
    nex_send_float(nex_temp_max, main_temp_max, 4, 1);
  }

  //Rain
  if (idx == idx_netatmo_regen)
  {
    main_rain = atof(root["svalue1"]);
    main_rain_hour = atof(root["svalue2"]);
    nex_send_float(nex_rain, main_rain, 3, 1);
    nex_send_float(nex_rain_hour, main_rain_hour, 3, 1);
  }

  if (idx == idx_netatmo_buiten)
  {
    main_temp_out = atof(root["svalue1"]);
    main_hum_out = atof(root["svalue2"]);
    nex_send_float(nex_temp_out, main_temp_out, 4, 1);
    nex_send_float(nex_hum_out, main_hum_out, 2, 0);
  }

  //Set weer icoon
  if (idx == idx_weer_icon)
  {
    main_pic_weer = root["nvalue"];
    pic_weer.setPic(main_pic_weer);
  }

  if (idx == idx_deurbel)
  {
    if (idx_nvalue == 1)
    {
#ifdef DEBUG
      Serial.println("Show doorbell");
#endif
      sendCommand("pic_doorbell.pic=52");
      sendCommand("vis pic_doorbell,1");
      //delay(200);
      for (int i = 0; i < 10; i++)
      {
//        tone(BUZZER_PIN, 1000, 200);
        delay(500);
      }
      sendCommand("vis pic_doorbell,0");
    }
  }

  //Show Deurbel stil icon
  if (idx == idx_deurbelstil)
  {
    if (idx_nvalue == 1)
      sendCommand("p1.pic=56");
    else
      sendCommand("p1.pic=53");
  }
  
  //Show Alarm
  if(idx == idx_alarm)
  {
    if (idx_nvalue == 1)
    {
#ifdef DEBUG
      Serial.println("Show Alarm");
#endif
      sendCommand("pic_doorbell.pic=58");
      sendCommand("vis pic_doorbell,1");
      alarm_sound = 1;
    }
    else
    {
#ifdef DEBUG
      Serial.println("Hide Alarm");
#endif
      sendCommand("vis pic_doorbell,0");
      sendCommand("pic_doorbell.pic=52");
      alarm_sound = 0;
    }
  }
}
