Skip to content

Instantly share code, notes, and snippets.

@ThomDietrich
Forked from otoolep/sine.py
Last active February 6, 2020 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ThomDietrich/ff836dbe0f0eaa2c5270a846a963893b to your computer and use it in GitHub Desktop.
Save ThomDietrich/ff836dbe0f0eaa2c5270a846a963893b to your computer and use it in GitHub Desktop.
Pump a sine wave signal as data points into a influxdb database
#!/usr/bin/python
# A little script to send test data to an influxdb installation
# Attention, the non-core library 'requests' is used. You'll need to install it first:
# http://docs.python-requests.org/en/master/user/install/
import json
import math
import requests
import sys
from time import sleep
IP = "192.168.0.2" # The IP of the machine hosting your influxdb instance
DB = "test" # The database to write to, has to exist
USER = "user" # The influxdb user to authenticate with
PASSWORD = "password123" # The password of that user
TIME = 1 # Delay in seconds between two consecutive updates
STATUS_MOD = 5 # The interval in which the updates count will be printed to your console
n = 0
while True:
for d in range(0, 360):
v = "sine_wave value={}".format(math.sin(math.radians(d)))
## without autentication
#r = requests.post("http://{}:8086/write?db={}".format(IP, DB), data=v)
## with autentication
r = requests.post("http://{}:8086/write?db={}".format(IP, DB), auth=(USER, PASSWORD), data=v)
if r.status_code != 204:
print("Failed to add point to influxdb ({}) - aborting.".format(r.status_code))
sys.exit(1)
n += 1
sleep(TIME)
if n % STATUS_MOD == 0:
print('{} points inserted.'.format(n))
@LeDumpier
Copy link

Hi Thom,

I downloaded this Py script but it looks incmplete. I'm no Python programmer but I can see the end to the for loop?

Have I missed something?

@JoKoT3
Copy link

JoKoT3 commented Apr 10, 2017

Hello,

the end of the loop is implicit, python syntax is based on indentation

@wthomson
Copy link

wthomson commented Oct 14, 2017

Hello,

Your title reads Pump a sine way into a influxdb database.
The word way should be wave.
Regards,

Bill Thomson

@webtop
Copy link

webtop commented Oct 26, 2017

Thank you, saved me having to write the same thing :)

@photon-schiesser
Copy link

photon-schiesser commented Jun 20, 2018

Why sleep for so long? Will we break something on the influx side if we try to insert too quickly or something? That seems like a huge problem if so...

@peterhoeg
Copy link

Why sleep for so long? Will we break something on the influx side if we try to insert too quickly or something?

It's time series data. You want it over time.

@ThomDietrich
Copy link
Author

Hehe thanks @peterhoeg

@Protheus23
Copy link

Hi ThomDietrich,

your script isn't starting on Debian 10.
I fixed it :)

There have to be braces in Line 29 and 34 for print

print('Failed to add point to influxdb (%d) - aborting') %r.status_code
...
print (('%d points inserted.') % n)

Now the script is working.

@ThomDietrich
Copy link
Author

@Protheus23 thanks. The script was in python 2 syntax before, now it is python2 and 3 compatible. Could you test again?

@Protheus23
Copy link

Its working!
Great Job!!

Thank you (also for your guide, its helping great)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment