Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from time import gmtime, strftime
  2. import paho.mqtt.client as mqtt
  3. import sqlite3
  4.  
  5. temperature_topic = "temperature"
  6. humidity_topic = "humidity"
  7. dbFile = "esp8266.db"
  8.  
  9. # The callback for when the client receives a CONNACK response from the server.
  10. def on_connect(client, userdata, flags, rc):
  11.     print("Connected with result code "+str(rc))
  12.  
  13.     # Subscribing in on_connect() means that if we lose the connection and
  14.     # reconnect then subscriptions will be renewed.
  15.     client.subscribe("temperature")
  16.     client.subscribe("humidity")
  17.  
  18. # The callback for when a PUBLISH message is received from the server.
  19. def on_message(client, userdata, msg):
  20.     theTime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
  21.     # theTime = strftime("%H:%M:%S", gmtime())
  22.     #print(msg.topic+" "+str(msg.payload))
  23.     result = (theTime + "\t" + str(msg.payload))
  24.     print(msg.topic + "\t" + result)
  25.     if (msg.topic == temperature_topic):
  26.         writeToDb(theTime, str(msg.payload))
  27.         return
  28.     if (msg.topic == humidity_topic):
  29.         return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement