Tag Archives: python

Emoncms & Python

Below is a small code example that will post updates to EmonCMS using Python –

#!bin/python
import requests

def postEmoncms(parameter_list):					# parameter_list is a dictionary
	
	host					=	'your.emoncms.host'			# Enter you host name or IP here
	emoncmsApi				=	'yourEmonCmsRwApiKey'		# This must be your EmonCMS RW API Key
	nodeName 				=	'nodeNameYouWantToUse'		# The name of the node you want to update with these values
	
	url 					= 	'http://' + host + '/emoncms/input/post'

	fullList				=	dict()
	fullList['node']		=	nodeName
	fullList['apikey']		=	emoncmsApi
	fullList['json']		=	str(parameter_list)
	
	http 					= 	requests.Session()
		
	r 						= 	http.post	(		url,
													params=fullList
											)
	return

def main():
	valuesToPost					=	dict()		# Create an empty dictionary to contain the values to post

	valuesToPost['youKeyName1']		=	1			# First Key Name and value
	valuesToPost['youKeyName2']		=	2			# Second Key Name and value
	valuesToPost['youKeyName3']		=	3			# Third Key Name and value

	postEmoncms(valuesToPost)						# pass the keys to the post function

if __name__ == "__main__":

	main()