     }
 }
 
-void sendDataToSql(Sensor& sensor)
+void sendDataToServer(Sensor& sensor)
 {
     if (sensor.isValid())
     {
-        sendDataToSql(sensor.getTemperature(),
-                      roundf(sensor.getHumidity()),
-                      roundf(sensor.getPressureMmHg()),
-                      sensor.getId());
-    }
-}
-
-void sendDataToSql(float temp, int humidity, int pressure, int sensorId)
+        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);
-
-    char buf[128];
+    
+    if (!timeIsSet)
+        return;
+
+    char payload[128];
     char tempStr[7];
+    char tokenStr[10];
     dtostrf(temp, sizeof(tempStr)-1, 2, tempStr);
 
-    int res = snprintf(buf, sizeof(buf),
-        "INSERT INTO Weather.sensor_smile (temp, hum, press, sensorId) VALUES (%s, %d, %d, %d)",
-        tempStr, humidity, pressure, sensorId);
-
-    if (res < 0 || res >= sizeof(buf))
-    {
-        DebugPrint(debug, "The SQL buffer is too small.\r\n");
+    // 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 (sqlCursor.execute(buf))
-    {
-        DebugPrint(debug, "SQL sent: %s\r\n", buf);
-    }
+    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);
 }


        ArduinoOTA.begin();
    }

    if (!sqlConn.connected())
    {
        DebugPrint(debug, "Connecting to SQL... ");
        if (sqlConn.connect(sqlAddr, 3306, SQL_USER, SQL_PASS))
        {
            DebugPrint(debug, "OK\r\n");
        }
        else
        {
            DebugPrint(debug, "FAILED\r\n");
        }
    }

    // Time setting
    if (!timeIsSet)
    {
        const int daySavingOffset = 3600;
        const int timezoneSec = 2 * 3600 + daySavingOffset;

        configTime(timezoneSec, 0, "pool.ntp.org", "time.nist.gov");
        DebugPrint(debug, "Waiting for time");

#include "Sensor.h"
#include "DebugStream.h"

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>

#include <time.h>

