 
 ESP8266WebServer httpServer(80);
 
-WiFiClient sqlClient;
-MySQL_Connection sqlConn(&sqlClient);
-MySQL_Cursor sqlCursor(&sqlConn);
-IPAddress sqlAddr;
+WiFiClient mqttClient;
+PubSubClient mqtt(DATA_SERVER_ADDR, DATA_SERVER_PORT, mqttClient);
 
 WiFiServer telnetServer(23);
 WiFiClient telnetClients[MAX_SRV_CLIENTS];
+
+void sendDataViaMqtt(float temp, int humidity, int pressure, int sensorId);
 
 void handleRoot()
 {

    digitalWrite(led, 1);
    Serial.begin(115200);

    sqlAddr.fromString(SQL_SERVER_ADDR);

    telnetServer.begin();
    telnetServer.setNoDelay(true);

    }
}

void sendDataToServer(Sensor& sensor)
{
    if (sensor.isValid())
    {
        sendDataViaMqtt(sensor.getTemperature(),
                        roundf(sensor.getHumidity()),
                        roundf(sensor.getPressureMmHg()),
                        sensor.getId());
    }
}

/*uint32_t getParamsHash(curTime, temp, humidity, pressure, sensorId)
{
    return;
}*/

void mqttCallback(char* topic, byte* payload, unsigned int length)
{
    DebugPrint(debug, "Message arrived [%s]\n", topic);

    for (unsigned int i = 0; i < length; ++i)
    {
        DebugPrint(debug, "%c", (char)payload[i]);
    }
    DebugPrint(debug, "\n");

    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '1')
    {
        digitalWrite(BUILTIN_LED, LOW);
        // Turn the LED on (Note that LOW is the voltage level
        // but actually the LED is on; this is because
        // it is acive low on the ESP-01)
    }
    else
    {
        digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
    }
}

void sendDataViaMqtt(float temp, int humidity, int pressure, int sensorId)
{
    digitalWrite(led, 0);
    
    if (!timeIsSet)
        return;

    char payload[128];
    char tempStr[7];
    char tokenStr[10];
    dtostrf(temp, sizeof(tempStr)-1, 2, tempStr);

    // remove trailing space
    char* tempStrP = tempStr;
    while (*tempStrP == ' ')
        ++tempStrP;

    const uint32_t curTime = time(nullptr);
    const uint32_t token = 0; //getParamsHash(curTime, temp, humidity, pressure, sensorId);
    snprintf(tokenStr, sizeof(tokenStr), "%x", token);

    // {"d":1495917000, "t":25.7, "h":51, "p":750, "sid":1003, "tok":"806be8"}
    int res = snprintf(payload, sizeof(payload),
        "{\"d\":%u,\"t\":%s,\"h\":%d,\"p\":%d,\"sid\":%d,\"tok\":\"%s\"}",
        curTime, tempStrP, humidity, pressure, sensorId, tokenStr);

    // WITHOUT TIME
    //int res = snprintf(payload, sizeof(payload),
    //    "{\"t\":%s,\"h\":%d,\"p\":%d,\"sid\":%d,\"tok\":\"%s\"}",
    //    tempStrP, humidity, pressure, sensorId, tokenStr);

    if (res < 0 || res >= sizeof(payload))
    {
        DebugPrint(debug, "Payload buffer is too small.\r\n");
        return;
    }

    if (mqtt.connect(WiFi.macAddress().c_str()))
    {
        DebugPrint(debug, "Sending to mqtt: %s\r\n", payload);

        if (!mqtt.publish(MQTT_TOPIC, payload))
        {
            DebugPrint(debug, "Cannot send to mqtt server: %s:%d\r\n", DATA_SERVER_ADDR, DATA_SERVER_PORT);
        }
    }
    else
    {
        DebugPrint(debug, "Server unreachable: %s:%d\r\n", DATA_SERVER_ADDR, DATA_SERVER_PORT);
    }

    mqtt.disconnect();

    digitalWrite(led, 1);
}
