In this tutorial we will connect Influx DB with raspberry PLC using python and upload a sensor data at regular intervals.
InfluxDB is an open-source time series database (TSDB) developed by the company InfluxData. It is written in the Go programming language for storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet of Things sensor data, and real-time analytics. It also has support for processing data from Graphite. Source: wiki
As mentioned, InfluxDB stores time series data. It stores them in "points". A point is a single data record. Similar to a row in SQL database.
Each point has 4 component:
A Measurement :- This is basically the string that is being stored. For eg. Temperature, Current, Voltage,Humidity,butterflies etc. It is a strings
A Tagset :- This is a dictionary of key-value pair and can be more than one. This is to store the metadata of the point and fetch or group points with this tag in the future.
A Fieldset: Again a key-value pair, but this time it stores the data. For eg. if Measurement is butterflies, fields key can be "Monarch" and "Ringlet" and field values can be "2cm" and "1cm" respectively.
Industrial shield's Raspberry PLC needs no introduction. It is one of the best selling PLCs from our vast catalogue. It is selected for great flexibility, versetality and efficiency.
It is a non invasive DC,AC and pulse hall split core current sensor that measures the current and gives an analog output.
from rpiplc_lib import rpiplc
from influxdb_client.client.write_api import SYNCHRONOUS token ='qYtbuLIcxrjSuiUI_HOws_EBuXL_LQsCgolHVyhuGOLLnAxpRMEenXJGkQxKO3ANMf6EsUefmpO5FaGDkGCYRw==' org = '[email protected]' bucket = 'current_sensor' def sensor_data(): rpiplc.init('RPIPLC_57R') while True: sensoutput=rpiplc.analog_read("I0.5") print(sensoutput) print('....') #Calculating the voltage v_in_volt = (10/2048)*sensoutput print(v_in_volt) print('....') #Calculating the current using the formula of the datasheet current = ((v_in_volt -2.5)/0.625)*30 print(current)
#Connect with influxDB and insert data. client = influxdb_client.InfluxDBClient(url="https://europe-west1-1.gcp.cloud2.influxdata.com/",token=token, org=org) write_api = client.write_api(write_options=SYNCHRONOUS) p=influxdb_client.Point("current_readings").tag("sensor","HSTS016L").field("Current",current) write_api.write(bucket=bucket, org=org, record=p) time.sleep(0.2) def main(): print('starting...') sensor_data() if __name__ == "__main__": """ This is executed when run from the command line """ main()