Skip to content

Commit

Permalink
Merge pull request mqtt-tools#155 from marcelrv/patch-2
Browse files Browse the repository at this point in the history
thingspeak API addition
  • Loading branch information
jpmens committed Nov 23, 2015
2 parents 5db3a02 + 1ba5776 commit bd3c791
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ _mqttwarn_ supports a number of services (listed alphabetically below):
* [sqlite](#sqlite)
* [smtp](#smtp)
* [syslog](#syslog)
* [thingspeak](#thingspeak)
* [twilio](#twilio)
* [twitter](#twitter)
* [xbmc](#xbmc)
Expand Down Expand Up @@ -1517,6 +1518,22 @@ Where `priority` can be between -2 and 5 and maps to `syslog` levels by;
Apr 22 12:42:42 mqttest019 mqttwarn[9484]: Disk utilization: 94%
```
### `thingspeak`
The `thingspeak` service publishes data to thingspeak.com using the thingspeak API.
```ini
[config:thingspeak]
targets = {
#API WRITE KEY field optional builddata=true/false
'field1' : [ 'XXYYZZXXYYZZXXYY' ,'field1' , 'true' ],
'field2' : [ 'XXYYZZXXYYZZXXYY' ,'field2' ]
}
```
Using builddata=true you can build an update with multiple fields in 1 update. Using this function no direct update. Only with the next update without builddata=true all entries are send (e.g. when multiple sensors are updating diferent topics, then you can do the build the data and submit when the last sensor is sending the data)
note: use the field as per the example, (lower case, `'field1'` with the last digit being the field number )
### `twilio`
```ini
Expand Down
65 changes: 65 additions & 0 deletions services/thingspeak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The code for thingspeak plugin for mqttwarn is based on other plugins

from urllib import urlencode
from httplib import HTTPSConnection, HTTPException
from ssl import SSLError

__author__ = 'Marcel Verpaalen'
__copyright__ = 'Copyright 2015 Marcel Verpaalen'
__license__ = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)"""

builddata = {}

def plugin(srv, item):
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)

build = ""
title = item.get('title', srv.SCRIPTNAME)
message = item.message

try:
if len(item.addrs) == 3:
apikey, field_id , build = item.addrs
elif len(item.addrs) == 2:
apikey, field_id = item.addrs
else:
srv.logging.warn("thingspeak target is incorrectly configured. use 2 or 3 arguments")
return False
except:
srv.logging.warn("thingspeak target is incorrectly configured")
return False

if build == "true":
builddata.update ({field_id: message.encode('utf-8')})
srv.logging.debug("thingspeak content building. Update %s to '%s' stored for later submission." , field_id, message.encode('utf-8'))
return True

http_handler = HTTPSConnection("api.thingspeak.com")

data = {'api_key': apikey,
field_id: message.encode('utf-8')
}
if len (builddata) > 0:
data.update (builddata)
builddata.clear()

try:
http_handler.request("POST", "/update",
headers={'Content-type': "application/x-www-form-urlencoded"},
body=urlencode(data)
)
except (SSLError, HTTPException), e:
srv.logging.warn("Thingspeak update failed: %s" % str(e))
return False

response = http_handler.getresponse()
body = response.read()

if body == '0':
srv.logging.warn("Thingspeak channel '%s' field '%s' update failed. Reponse: %s, %s, %s" % (item.target, field_id, response.status, response.reason, body))
else:
srv.logging.debug("Reponse: %s, %s, update: %s" % (response.status, response.reason, body))

return True

0 comments on commit bd3c791

Please sign in to comment.