Guest User

Untitled

a guest
Feb 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. TypeError: float() argument must be a string or a number
  2.  
  3. from flask import Flask, request, render_template
  4. from datetime import datetime
  5. from ubidots import ApiClient
  6. import matplotlib.pyplot as plt
  7. import matplotlib.dates as mdates
  8. import matplotlib.animation as animation
  9. from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
  10. from matplotlib.figure import Figure
  11. import io
  12.  
  13. app = Flask(__name__)
  14.  
  15. //get the latest value of the ldr sensor from Ubidots
  16. def get_records():
  17. api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXX')
  18. my_variable = api.get_variable('XXXXXXXXXXXXXXXXXXXX')
  19. ldr = my_variable.get_values(1)
  20. return ldr
  21.  
  22. //get the latest timestamp from Ubidots
  23. def get_times():
  24. api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXXX')
  25. variable = api.get_variable('XXXXXXXXXXXXXXXXXXXXXX')
  26. value = variable.get_values(1)[0]
  27. timestamp = value.get('timestamp')
  28. global t
  29. t= mdates.epoch2num(timestamp)
  30. return t
  31.  
  32. //render the graph with x and y values of time and sensor values respectively and send to Flask
  33. @app.route('/', methods=['POST'])
  34. def my_form_post():
  35. ldr= get_records()
  36. time= get_time()
  37. templateData = {
  38. 'ldr' : ldr,
  39. 'time' : t
  40. }
  41. return render_template('main.html', **templateData)
  42.  
  43. //Plotting the graph using Matplotlib
  44. @app.route('/')
  45. def plot_temp():
  46. ldr= get_records()
  47. ys = ldr
  48. fig = Figure()
  49. axis = fig.add_subplot(1,1,1)
  50. axis.set_title("Light Intensity")
  51. axis.set_xlabel("Time")
  52. axis.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
  53. axis.grid(True)
  54. time= get_times()
  55. xs = time
  56. axis.plot(xs,ys)
  57. canvas = FigureCanvas(fig)
  58. output = io.BytesIO()
  59. canvas.print_png(output)
  60. response = make_response(output.getvalue())
  61. response.mimetype = 'image/png'
  62. ani= animation.FuncAnimation(fig, animate, interval=3000)
  63. return response
  64.  
  65. if __name__ == "__main__":
  66. app.run(host='0.0.0.0', port=80, debug=True)
  67.  
  68. <!DOCTYPE html>
  69. <head>
  70. <title>{{ title }}</title>
  71. </head>
  72.  
  73. <body>
  74. <h1>Hello, World!</h1>
  75. <h2>The date and time on the server is: {{ time }}</h2>
  76. <img src="/" alt="Image Placeholder" width="49%">
  77. </body>
  78. </html>
Add Comment
Please, Sign In to add comment