   Serial.println();
 
   // Only proceed if incoming message's topic matches
-  if (String(topic) == topicLed) {
-    ledStatus = !ledStatus;
-    digitalWrite(LED_PIN, ledStatus);
-    mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
-  }
-}
+  if (String(topic) == topicHeater) {
+    HeaterStatus = payload[0] == '0' ? LOW : HIGH; 
+    digitalWrite(HEATER_PIN, HeaterStatus);
+    mqtt.publish(topicHeaterStatus, HeaterStatus ? "1" : "0");
+  }
+}
+
+void senddht22(int pinDHT22, char* topic) {
+  float temperature = 0;
+  float humidity = 0;
+  int err = SimpleDHTErrSuccess;
+  size_t len = strlen(topic);
+
+  if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
+    delay(2000);
+    Serial.println("DHT init failed");
+    return;
+  }
+
+  strcpy(topicbuffer,topic);
+  
+  strcpy(topicbuffer + len, tempprefix);
+  dtostrf(temperature, 0, 1, valbuffer);
+  mqtt.publish(topicbuffer, valbuffer);
+  
+  strcpy(topicbuffer + len, humprefix);
+  dtostrf(humidity, 0, 1, valbuffer);
+  mqtt.publish(topicbuffer, valbuffer);
+}
+  
+void sendbme(char* topic) {
+  float temperature = 0;
+  float humidity = 0;
+  size_t len = strlen(topic);
+
+  if (bme_status) {
+    strcpy(topicbuffer,topic);
+  
+    strcpy(topicbuffer + len, tempprefix);
+    dtostrf(bme.readTemperature(), 0, 1, valbuffer);
+    mqtt.publish(topicbuffer, valbuffer);
+
+    strcpy(topicbuffer + len, humprefix);
+    dtostrf(bme.readHumidity(), 0, 1, valbuffer);
+    mqtt.publish(topicbuffer, valbuffer);
+
+    strcpy(topicbuffer + len, pressprefix);
+    dtostrf(bme.readPressure(), 0, 2, valbuffer);
+    mqtt.publish(topicbuffer, valbuffer);
+  }
+}
+// foo
+
+  //  mqtt.publish(topic, xxx);

      }
    }
  }

}

void mqttCallback(char* topic, byte* payload, unsigned int len) {

 **************************************************************/

// Select your modem:
#define TINY_GSM_MODEM_M590

#include <TinyGsmClient.h>
#include <PubSubClient.h>

#include <SimpleDHT.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>

// Edit BME280_ADDRESS (0x76)
#include <Adafruit_BME280.h>

#include <Adafruit_SleepyDog.h>

// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[]  = "internet";
const char user[] = "";
const char pass[] = "";
const char mqtt_user[] = "valpuri";
const char mqtt_pass[] = "ammu1001";

// Use Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1

// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);

SimpleDHT22 dht22;

Adafruit_BME280 bme;

const char broker[] = "kw.dy.fi";

const char topicHeater[] = "Karttula/Lammitin/";
const char topicHeaterStatus[] = "Karttula/LammitinStatus/";
const char topicInit[] = "Karttula/init/";
const char topicOlohuone[] = "Karttula/Olohuone/";
const char topicMakuuhuone[] = "Karttula/Makuuhuone/";
const char topicVierashuone[] = "Karttula/Vierashuone/";
const char topicWC[] = "Karttula/WC/";

const char tempprefix[] = "T/";
const char humprefix[] = "RH/";
const char pressprefix[] = "P/";

#define Makuuhuone 4
#define WC 5
#define Vierashuone 6

#define HEATER_PIN 7
#define GSM_RESET_PIN 8

int HeaterStatus = LOW;

long lastReconnectAttempt = 0;
unsigned long lastrun = millis();
int state = 0;
//#define PERIOD 5*60*1000
#define PERIOD 3000

bool bme_status;

#define TOPICBUFLEN 36
#define VALBUFLEN 10

char topicbuffer[TOPICBUFLEN];
char valbuffer[VALBUFLEN];

void setup() {

  int countdownMS = Watchdog.enable(20000);
  
  pinMode(HEATER_PIN, OUTPUT);
  pinMode(GSM_RESET_PIN, OUTPUT);

  // Turn heater off by default
  digitalWrite(HEATER_PIN, LOW);

  // Reset GSM module
  digitalWrite(GSM_RESET_PIN, LOW);
  delay(100);
  digitalWrite(GSM_RESET_PIN, HIGH);
  delay(500);
  digitalWrite(GSM_RESET_PIN, LOW);
  
  // Set console baud rate
  Serial.begin(115200);
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);

  // Restart takes quite some time
