Advertisement
Guest User

Ilmajama

a guest
Jul 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.67 KB | None | 0 0
  1. from time import sleep
  2. import requests
  3. from requests.auth import HTTPBasicAuth
  4. import json
  5. from dateutil.tz import tzlocal
  6. import time
  7. from datetime import datetime
  8. import io
  9. import math
  10. import smbus
  11. import subprocess
  12. import threading
  13. import random
  14. # Import SPI library (for hardware SPI) and MCP3008 library.
  15. import Adafruit_GPIO.SPI as SPI
  16. import Adafruit_MCP3008
  17. import RPi.GPIO as GPIO
  18.  
  19. GPIO.setmode(GPIO.BCM)
  20. GPIO.setup(18, GPIO.IN)
  21. GPIO.setup(17, GPIO.OUT)
  22.  
  23. global revcount
  24. revcount = 0
  25. url = 'https://tarktartu.iot.cs.ut.ee/'
  26. tenant = "tarktartu"
  27.  
  28. deviceID = "10220"
  29. serialNr = "0000000017b769d5"
  30. username = "testing"
  31. password = "garage48"
  32. auth = HTTPBasicAuth(tenant+"/"+username, password)
  33.  
  34. SPI_PORT   = 0
  35. SPI_DEVICE = 0
  36. mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
  37.  
  38.  
  39. def SendCustomTempMeasurement(targetID = 10000, tempValue = 25):
  40.     requestUrl = url+'measurement/measurements'
  41.     headers = {
  42.         "Content-Type": "application/json",
  43.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  44.         "Connection": "close"
  45.     }
  46.  
  47.     data =  {
  48.         "com_stagnationlab_c8y_driver_measurements_TemperatureMeasurement": {
  49.             "temperature": {
  50.                 "value": tempValue,
  51.                 "unit": "C" }
  52.             },
  53.         #"time": str(datetime.now(tzlocal()).isoformat()),
  54.         "time": str(datetime.utcnow().isoformat()),
  55.         "source": { "id": targetID },
  56.         "type": "com_stagnationlab_c8y_driver_measurements_TemperatureMeasurement"
  57.     }
  58.  
  59.  
  60.     output =  io.StringIO()
  61.     json.dump(data, output)
  62.     jsonString = output.getvalue()
  63.     #print(jsonString)
  64.     response = s.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  65.  
  66.     print(response)
  67.     print(response.content)
  68.  
  69. def SendCustomSoundMeasurement(targetID = 10000, soundValue = 40):
  70.     requestUrl = url+'measurement/measurements'
  71.     headers = {
  72.         "Content-Type": "application/json",
  73.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  74.     }
  75.  
  76.     data =  {
  77.         "com_stagnationlab_c8y_driver_measurements_SoundMeasurement": {
  78.             "soundLevel": {
  79.                 "value": soundValue,
  80.                 "unit": "dB" }
  81.             },
  82.         #"time": str(datetime.now(tzlocal()).isoformat()),
  83.         "time": str(datetime.utcnow().isoformat()),
  84.         "source": { "id": targetID },
  85.         "type": "com_stagnationlab_c8y_driver_measurements_SoundMeasurement"
  86.     }
  87.  
  88.  
  89.     output =  io.StringIO()
  90.     json.dump(data, output)
  91.     jsonString = output.getvalue()
  92.     #print(jsonString)
  93.     response = s.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  94.  
  95.     print(response)
  96.     print(response.content)
  97.  
  98. def SendCustomPressureMeasurement(targetID = 10000, pressureValue = 40):
  99.     requestUrl = url+'measurement/measurements'
  100.     headers = {
  101.         "Content-Type": "application/json",
  102.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  103.         "Connection": "close"
  104.  
  105.     }
  106.  
  107.     data =  {
  108.         "com_stagnationlab_c8y_driver_measurements_PressureMeasurement": {
  109.             "pressure": {
  110.                 "value": pressureValue,
  111.                 "unit": "kPa" }
  112.             },
  113.         "time": str(datetime.now(tzlocal()).isoformat()),
  114.         "source": { "id": targetID },
  115.         "type": "com_stagnationlab_c8y_driver_measurements_PressureMeasurement"
  116.     }
  117.  
  118.  
  119.     output =  io.StringIO()
  120.     json.dump(data, output)
  121.     jsonString = output.getvalue()
  122.     #print(jsonString)
  123.     response = s.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  124.  
  125.     print(response)
  126.     print(response.content)
  127.  
  128.  
  129. def SendCustomHumidityMeasurement(targetID = 10000, humValue = 40):
  130.     requestUrl = url+'measurement/measurements'
  131.     headers = {
  132.         "Content-Type": "application/json",
  133.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  134.         "Connection": "close"
  135.     }
  136.  
  137.     data =  {
  138.         "com_stagnationlab_c8y_driver_measurements_HumidityMeasurement": {
  139.             "humidity": {
  140.                 "value": humValue,
  141.                 "unit": "%" }
  142.             },
  143.         "time": str(datetime.now(tzlocal()).isoformat()),
  144.         "source": { "id": targetID },
  145.         "type": "com_stagnationlab_c8y_driver_measurements_HumidityMeasurement"
  146.     }
  147.  
  148.  
  149.     output =  io.StringIO()
  150.     json.dump(data, output)
  151.     jsonString = output.getvalue()
  152.     #print(jsonString)
  153.     response = requests.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  154.  
  155.     print(response)
  156.     print(response.content)
  157. def SendCustomLightMeasurement(targetID = 10000, lightValue = 40):
  158.     requestUrl = url+'measurement/measurements'
  159.     headers = {
  160.         "Content-Type": "application/json",
  161.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  162.         "Connection": "close"
  163.     }
  164.  
  165.     data =  {
  166.         "com_stagnationlab_c8y_driver_measurements_LightMeasurement": {
  167.             "lightLevel": {
  168.                 "value": lightValue,
  169.                 "unit": "lux" }
  170.             },
  171.         "time": str(datetime.now(tzlocal()).isoformat()),
  172.         "source": { "id": targetID },
  173.         "type": "com_stagnationlab_c8y_driver_measurements_LightMeasurement"
  174.     }
  175.  
  176.  
  177.     output =  io.StringIO()
  178.     json.dump(data, output)
  179.     jsonString = output.getvalue()
  180.     #print(jsonString)
  181.     response = s.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  182.  
  183.     print(response)
  184.     print(response.content)
  185.  
  186. def SendCustomAnemoMeasurement(targetID = 10000, anemoValue = 40):
  187.     requestUrl = url+'measurement/measurements'
  188.     headers = {
  189.         "Content-Type": "application/json",
  190.         "Accept": "application/vnd.com.nsn.cumulocity.measurement+json",
  191.         "Connection": "close"
  192.     }
  193.  
  194.     data =  {
  195.         "com_stagnationlab_c8y_driver_measurements_AnemoMeasurement": {
  196.             "anemo": {
  197.                 "value": anemoValue,
  198.                 "unit": "m/s" }
  199.             },
  200.         "time": str(datetime.now(tzlocal()).isoformat()),
  201.         "source": { "id": targetID },
  202.         "type": "com_stagnationlab_c8y_driver_measurements_AnemoMeasurement"
  203.     }
  204.  
  205.  
  206.     output =  io.StringIO()
  207.     json.dump(data, output)
  208.     jsonString = output.getvalue()
  209.     #print(jsonString)
  210.     response = s.post(requestUrl, auth=auth, data=jsonString, headers=headers)
  211.  
  212.     print(response)
  213.     print(response.content)
  214.  
  215.  
  216.  
  217. def increaserev(channel):
  218.     global revcount
  219.     revcount += 1
  220.  
  221. GPIO.add_event_detect(18, GPIO.RISING, callback=increaserev)
  222.  
  223. if __name__ == '__main__':
  224.     s= requests.session()
  225.     while True:
  226.         global revcount
  227.         termo_file = open("/sys/bus/w1/devices/28-0516863925ff/w1_slave")
  228.         lines = termo_file.readlines()
  229.         t_pos = lines[1].find('t=')
  230.         temp_string = lines[1][t_pos+2:]
  231.         temp_c = float(temp_string) / 1000.0
  232.         SendCustomTempMeasurement(deviceID, temp_c)
  233.         termo_file.close()
  234.  
  235.         dB = round(20.0 * math.log10(mcp.read_adc(1)+1) + 10)
  236.         SendCustomSoundMeasurement(deviceID, dB)
  237.  
  238.         #Valgus
  239.         lux = round(mcp.read_adc(0)*1123/691)
  240.         print(lux)
  241.         if lux < 50:
  242.             now = datetime.now()
  243.             if now.hour > 10 or now.hour < 20:
  244.                 GPIO.output(17, True)
  245.             else:
  246.                 GPIO.output(17, False)
  247.         else:
  248.             GPIO.output(17, False)
  249.         SendCustomLightMeasurement(deviceID, lux)
  250.         time.sleep(1)
  251.         rc = revcount * 60
  252.         print ("RPM is " + str(rc))
  253.         print ("M/S is " + str(rc/100))
  254.         revcount = 0
  255.         if rc/100 is not 0.0:
  256.             SendCustomAnemoMeasurement(deviceID, rc/100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement