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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int _SUETFLPATemp = 0;
byte _nextionSaveDataVAL_P0_E2;
long _nextionSaveDataPCO_P0_E3;
String _nextionSaveDataTXT_P0_E3;
byte _nextionSaveDataPICC_P0_E4;
int _nextionSaveDataVAL_P1_E2;
long _nextionSaveDataVAL_P1_E4;
String _nextionCommandTempString;
struct _nextionLissenStruct {
  char code;
  String result;
  long number;
};
_nextionLissenStruct _nextionTempLissen;
int nextionGetPageIdTempVar = 0;
int NextionPanel_47525121_PageIdOut = 0;
int _hasNumberChangeInTemp = 0;
int _gtv1;
int _gtv2;
int _gtv3 = 290;
bool _gtv4 = 0;
bool _gtv5 = 0;
int _gtv6;
unsigned long _dht1Tti = 0UL;
float _dht1t = 0.00;
float _dht1h = 0.00;
bool _changeNumber1_Out = 0;
int _changeNumber1_OLV;
bool _nextionSetAttr1_isNU = 0;
bool _nextionSetAttr1_oldState = 0;
bool _nextionSetAttr2_isNU = 0;
bool _nextionSetAttr2_oldState = 0;
bool _trgrt1 = 0;
bool _trgrt1I = 0;
bool _trgrt2 = 0;
bool _trgrt2I = 0;
bool _nextionSetAttr3_isNU = 0;
bool _nextionSetAttr3_oldState = 0;
long _swi1;
bool _nextionSetAttr4_isNU = 0;
bool _nextionSetAttr4_oldState = 0;
byte _swi2;
bool _nextionSetAttr5_isNU = 0;
bool _nextionSetAttr5_oldState = 0;
bool _changeNumber2_Out = 0;
int _changeNumber2_OLV;
int _swi3;
bool _nextionSetAttr6_isNU = 0;
bool _nextionSetAttr6_oldState = 0;
bool _trgrt3 = 0;
bool _trgrt3I = 0;
bool _gen1I = 0;
bool _gen1O = 0;
unsigned long _gen1P = 0UL;
bool _trgrt5 = 0;
bool _trgrt5I = 0;
bool _nextionAddPointToWave1_OldState = 0;
bool _nextionAddPointToWave2_OldState = 0;
bool _SFLPAS1;
bool _SFLPAS2;
bool _nextionSendArraytToWave1_OldState = 0;
bool _nextionSendArraytToWave2_OldState = 0;
bool _nextionSetLighting1_OldStae = 0;
bool _changeNumber3_Out = 0;
int _changeNumber3_OLV;
void setup()
{
  Serial100.begin(9600);
  _dht1.begin();
  nextionSendCommand("", 100);
  nextionAck(100);
  nextionSendCommand("page 0", 100);
  delay(100);
  nextionAck(100);
}
void loop()
{ nextionGetPageIdTempVar = nextionAskPageNamper(100);
  if ( ! (nextionGetPageIdTempVar < 0) ) {
    NextionPanel_47525121_PageIdOut = nextionGetPageIdTempVar;
  };
  if (_isTimer(_dht1Tti, 5000)) {
    _dht1Tti = millis();
    float tempDht4;
    tempDht4 = _dht1.readTemperature();
    if (!(isnan(tempDht4))) {
      _dht1t = tempDht4;
    }
    tempDht4 = _dht1.readHumidity();
    if (!(isnan(tempDht4))) {
      _dht1h = tempDht4;
    }
  }



  _gtv1 = (int((_dht1t) * (10)));
  _gtv2 = (int(_dht1h));
  if ((_gtv1) > (_gtv3)) {
    if (_trgrt1I) {
      _trgrt1 = 0;
    } else {
      _trgrt1 = 1;
      _trgrt1I = 1;
    }
  } else {
    _trgrt1 = 0;
    _trgrt1I = 0;
  };
  if ((_gtv1) > (_gtv3))
  {
    _swi1 = 63488;
  }
  else
  {
    _swi1 = 2016;
  }
  if (!((_gtv1) > (_gtv3))) {
    if (_trgrt2I) {
      _trgrt2 = 0;
    } else {
      _trgrt2 = 1;
      _trgrt2I = 1;
    }
  } else {
    _trgrt2 = 0;
    _trgrt2I = 0;
  };
  if ((_gtv1) > (_gtv3))
  {
    _swi2 = 2;
  }
  else
  {
    _swi2 = 0;
  }
  if (_changeNumber1_Out) {
    _changeNumber1_Out = 0;
  } else {
    _hasNumberChangeInTemp = _gtv1;
    if (_hasNumberChangeInTemp != _changeNumber1_OLV) {
      _changeNumber1_OLV = _hasNumberChangeInTemp;
      _changeNumber1_Out = 1;
    }
  }
  if (_changeNumber1_Out) {
    if (! _nextionSetAttr1_oldState ) {
      _nextionSetAttr1_oldState = 1;
      _nextionSetAttr1_isNU = 1;
      _nextionSaveDataTXT_P0_E3 =  ((( _floatToStringWitRaz((_gtv1) / (10.00), 1))) + (String(" C"))) ;
    }
  } else {
    _nextionSetAttr1_oldState = 0;
  } if (_nextionSetAttr1_isNU) {
    _nextionCommandTempString = String("page0.tnt.txt=\"") + _nextionSaveDataTXT_P0_E3 + String("\"");
    nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
    _nextionSetAttr1_isNU = 0;
  } if (_changeNumber1_Out) {
    if (! _nextionSetAttr2_oldState ) {
      _nextionSetAttr2_oldState = 1;
      _nextionSetAttr2_isNU = 1;
      _nextionSaveDataVAL_P0_E2 =  (map((_gtv1), (-400), (500), (10), (91))) ;
    }
  } else {
    _nextionSetAttr2_oldState = 0;
  } if (_nextionSetAttr2_isNU) {
    _nextionCommandTempString = String("page0.tpb.val=") + String(_nextionSaveDataVAL_P0_E2);
    nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
    _nextionSetAttr2_isNU = 0;
  } if (( (_trgrt1) || (_trgrt2) )) {
    if (! _nextionSetAttr3_oldState ) {
      _nextionSetAttr3_oldState = 1;
      _nextionSetAttr3_isNU = 1;
      _nextionSaveDataPCO_P0_E3 =  _swi1 ;
    }
  } else {
    _nextionSetAttr3_oldState = 0;
  }
  if (nextionAskPageNamper(100) == 0) {
    if (_nextionSetAttr3_isNU) {
      _nextionCommandTempString = String("page0.tnt.pco=") + String(_nextionSaveDataPCO_P0_E3);
      nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
      _nextionCommandTempString = "ref tnt";
      nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
      _nextionSetAttr3_isNU = 0;
    }
  } else {
    _nextionSetAttr3_isNU = 1;
  }
  if (( (_trgrt1) || (_trgrt2) )) {
    if (! _nextionSetAttr4_oldState ) {
      _nextionSetAttr4_oldState = 1;
      _nextionSetAttr4_isNU = 1;
      _nextionSaveDataPICC_P0_E4 =  _swi2 ;
    }
  } else {
    _nextionSetAttr4_oldState = 0;
  }
  if (nextionAskPageNamper(100) == 0) {
    if (_nextionSetAttr4_isNU) {
      _nextionCommandTempString = String("page0.tci.picc=") + String(_nextionSaveDataPICC_P0_E4);
      nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
      _nextionCommandTempString = "ref tci";
      nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
      _nextionSetAttr4_isNU = 0;
    }
  } else {
    _nextionSetAttr4_isNU = 1;
  }
  if (((map((_gtv2), (0), (100), (-46), (226)))) < (0))
  {
    _swi3 = ((map((_gtv2), (0), (100), (-46), (226)))) + (360);
  }
  else
  {
    _swi3 = (map((_gtv2), (0), (100), (-46), (226)));
  }
  if (_changeNumber2_Out) {
    _changeNumber2_Out = 0;
  } else {
    _hasNumberChangeInTemp = _gtv2;
    if (_hasNumberChangeInTemp != _changeNumber2_OLV) {
      _changeNumber2_OLV = _hasNumberChangeInTemp;
      _changeNumber2_Out = 1;
    }
  }
  if (_changeNumber2_Out) {
    if (! _nextionSetAttr5_oldState ) {
      _nextionSetAttr5_oldState = 1;
      _nextionSetAttr5_isNU = 1;
      _nextionSaveDataVAL_P1_E4 =  _gtv2 ;
    }
  } else {
    _nextionSetAttr5_oldState = 0;
  } if (_nextionSetAttr5_isNU) {
    _nextionCommandTempString = String("page1.humN.val=") + String(_nextionSaveDataVAL_P1_E4);
    nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
    _nextionSetAttr5_isNU = 0;
  } if (_changeNumber2_Out) {
    if (! _nextionSetAttr6_oldState ) {
      _nextionSetAttr6_oldState = 1;
      _nextionSetAttr6_isNU = 1;
      _nextionSaveDataVAL_P1_E2 =  _swi3 ;
    }
  } else {
    _nextionSetAttr6_oldState = 0;
  } if (_nextionSetAttr6_isNU) {
    _nextionCommandTempString = String("page1.humG.val=") + String(_nextionSaveDataVAL_P1_E2);
    nextionSendCommand(_nextionCommandTempString.c_str(), 100);  nextionAck(100);
    _nextionSetAttr6_isNU = 0;
  } if (1) {
    if (! _gen1I) {
      _gen1I = 1;
      _gen1O = 1;
      _gen1P = millis();
    }
  } else {
    _gen1I = 0 ;
    _gen1O = 0;
  }
  if (_gen1I) {
    if ( _isTimer ( _gen1P , 5000 )) {
      _gen1P = millis();
      _gen1O = ! _gen1O;
    }
  }
  if (_gen1O) {
    if (_trgrt5I) {
      _trgrt5 = 0;
    } else {
      _trgrt5 = 1;
      _trgrt5I = 1;
    }
  } else {
    _trgrt5 = 0;
    _trgrt5I = 0;
  };
  if (_gtv4) {
    if (_trgrt3I) {
      _trgrt3 = 0;
    } else {
      _trgrt3 = 1;
      _trgrt3I = 1;
    }
  } else {
    _trgrt3 = 0;
    _trgrt3I = 0;
  };
  _gtv4 = (NextionPanel_47525121_PageIdOut) == (2);
  _gtv5 = _trgrt5;
  if (_trgrt3) {
    if ( ! _nextionAddPointToWave1_OldState) {
      _nextionAddPointToWave1_OldState = 1;
      if (nextionAskPageNamper(100) == 2) {
        for (int nextionSATWTemp = 0;  nextionSATWTemp < 330;  nextionSATWTemp ++) {
          _nextionCommandTempString = String ("add 1,1,") + String (_FLPArray142003126[nextionSATWTemp]);
          nextionSendCommand(_nextionCommandTempString.c_str(), 100);
        } delay (50);
      }
    }
  } else {
    _nextionAddPointToWave1_OldState = 0;
  }

  if (_trgrt3) {
    if ( ! _nextionAddPointToWave2_OldState) {
      _nextionAddPointToWave2_OldState = 1;
      if (nextionAskPageNamper(100) == 2) {
        for (int nextionSATWTemp = 0;  nextionSATWTemp < 330;  nextionSATWTemp ++) {
          _nextionCommandTempString = String ("add 1,0,") + String (_FLPArray239384258[nextionSATWTemp]);
          nextionSendCommand(_nextionCommandTempString.c_str(), 100);
        } delay (50);
      }
    }
  } else {
    _nextionAddPointToWave2_OldState = 0;
  }

  if (_gtv5) {
    if (!_SFLPAS1) {
      _SFLPAS1 =  1;
      for (_SUETFLPATemp = 0; _SUETFLPATemp < 329; _SUETFLPATemp = _SUETFLPATemp + 1 ) {
        _FLPArray239384258[_SUETFLPATemp ] = _FLPArray239384258[_SUETFLPATemp + 1];
      }  _FLPArray239384258[329] = _gtv2;
    }
  }  else {
    _SFLPAS1 = 0;
  }
  _gtv6 = ((_gtv1) / (5)) + (160);
  if (_gtv5) {
    if (!_SFLPAS2) {
      _SFLPAS2 =  1;
      for (_SUETFLPATemp = 0; _SUETFLPATemp < 329; _SUETFLPATemp = _SUETFLPATemp + 1 ) {
        _FLPArray142003126[_SUETFLPATemp ] = _FLPArray142003126[_SUETFLPATemp + 1];
      }  _FLPArray142003126[329] = _gtv6;
    }
  }  else {
    _SFLPAS2 = 0;
  }
  if (( (_gtv5) && (!(_trgrt3)) && (_gtv4) )) {
    if ( ! _nextionSendArraytToWave1_OldState) {
      _nextionSendArraytToWave1_OldState = 1;
      if (nextionAskPageNamper(100) == 2) {
        _nextionCommandTempString = String ("add 1,0,") + String (_gtv2);
        nextionSendCommand(_nextionCommandTempString.c_str(), 100); delay (50);
      }
    }
  } else {
    _nextionSendArraytToWave1_OldState = 0;
  }

  if (( (_gtv5) && (!(_trgrt3)) && (_gtv4) )) {
    if ( ! _nextionSendArraytToWave2_OldState) {
      _nextionSendArraytToWave2_OldState = 1;
      if (nextionAskPageNamper(100) == 2) {
        _nextionCommandTempString = String ("add 1,1,") + String (_gtv6);
        nextionSendCommand(_nextionCommandTempString.c_str(), 100); delay (50);
      }
    }
  } else {
    _nextionSendArraytToWave2_OldState = 0;
  }

  if (_changeNumber3_Out) {
    _changeNumber3_Out = 0;
  } else {
    _hasNumberChangeInTemp = (map(( (analogRead (0))), (0), (1023), (0), (100)));
    if (((_hasNumberChangeInTemp > _changeNumber3_OLV) && ((_hasNumberChangeInTemp - _changeNumber3_OLV ) > 5)) || ((_hasNumberChangeInTemp < _changeNumber3_OLV) && ((_changeNumber3_OLV - _hasNumberChangeInTemp ) > 5) ))
    {
      _changeNumber3_OLV = _hasNumberChangeInTemp;
      _changeNumber3_Out = 1;
    }
  }
  if (_changeNumber3_Out) {
    if (!(_nextionSetLighting1_OldStae)) {
      _nextionSetLighting1_OldStae = 1;
      _nextionCommandTempString = String("dim=") + String((map(( (analogRead (0))), (0), (1023), (0), (100))));
      nextionSendCommand(_nextionCommandTempString.c_str(), 100);
    }
  } else {
    _nextionSetLighting1_OldStae = 0;
  }




}
bool _isTimer(unsigned long startTime, unsigned long period )
{
  unsigned long currentTime;
  currentTime = millis();
  if (currentTime >= startTime) {
    return (currentTime >= (startTime + period));
  } else {
    return (currentTime >= (4294967295 - startTime + period));
  }
}
String  _floatToStringWitRaz(float value, int raz)
{
  float tv;
  int ti = int(value);
  String ts = String(ti);
  if (raz == 0) {
    return ts;
  }
  ts += ".";
  float tf = abs(value - ti);
  for (int i = 1; i <= raz; i++ )
  {
    tv = tf * 10;
    ti = int(tv);
    ts += String(ti);
    tf = (tv - ti);
  }
  return ts;
}
void nextionSendCommand(const char* cmd, byte port )
{
  while (Serial100.available()) {
    Serial100.read();
  }
  Serial100.print(cmd);
  Serial100.write(0xFF);
  Serial100.write(0xFF);
  Serial100.write(0xFF);
}
boolean nextionAck( byte port )
{
  uint8_t bytes[4] = {0};
  Serial100.setTimeout(20); if (sizeof(bytes) != Serial100.readBytes((char *)bytes, sizeof(bytes))) {
    return 0;
  }

  if ((bytes[1] == 0xFF) && (bytes[2] == 0xFF) && (bytes[3] == 0xFF)) {
    switch (bytes[0]) {
      case 0x00:
        return false; break;
      case 0x01:
        return true; break;
      default:
        return false;
    }
  }
}
struct _nextionLissenStruct nextionListen( byte port )
{
  char _bite;
  char _end = 0xff;
  String cmd;
  _nextionLissenStruct temp;
  int countEnd = 0;
  delay(10);
  while (Serial100.available() > 0) {
    if (Serial100.available() > 0) {
      _bite = Serial100.read();
      cmd += _bite;
      if (_bite == _end) {
        countEnd++;
      }
      if (countEnd == 3) {
        break;
      }
    }
  }
  temp.result = "";
  temp.code = 'z';
  temp.number = 0;
  switch (cmd[0]) {
    case 'e':
      temp.code = 'e';
      countEnd = 0;
      for (uint8_t i = 0; i < cmd.length(); i++) {
        if (cmd[i] == _end) {
          countEnd++;
        }
        temp.result += String(cmd[i], HEX);
        if (countEnd == 3) {
          return temp;
        }
        temp.result += " ";
      }
      break;
    case 'f':
      temp.code = 'f';
      temp.result = String(cmd[1], DEC);
      return temp;
      break;
    case 'g':
      temp.code = 'g';
      temp.result = String(cmd[2], DEC) + "," + String(cmd[4], DEC) + "," + String(cmd[5], DEC);
      return temp;
      break;
    case 'h':
      temp.code = 'h';
      temp.result = String(cmd[2], DEC) + "," + String(cmd[4], DEC) + "," + String(cmd[5], DEC);
      temp.result = "68 " + temp.result;
      return temp;
      break;
    case 'p':
      temp.code = 'p';
      temp.result = cmd.substring(1, cmd.length() - 3);
      return temp;
      break;
    case 'q':
      temp.code = 'q';
      temp.number = (cmd[4] << 24) | (cmd[3] << 16) | (cmd[2] << 8) | (cmd[1]);
      return temp;
      break;
    default:
      return temp;
      break;
  }
  return temp;
}
int nextionAskPageNamper(byte port)
{
  int result;
  _nextionLissenStruct temp;
  nextionSendCommand("sendme", port);
  temp = nextionListen(port);
  if ((temp.code == 'f') && (temp.result != "")) {
    result = temp.result.toInt();
  } else {
    result = -1;
  }
  return result;
}

© Geektimes