SAGAR M
Published

Optical_Encoder using Bolt_IoT with ML

Optical encoder is a device that is used to measure the speed of a slotted disc in RPM.

BeginnerFull instructions provided1 hour206
Optical_Encoder using Bolt_IoT with ML

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
fc 03
×1
DC motor (generic)
×1
coded disc
×1

Story

Read more

Schematics

optical_encoder_bEAMzXcV89.fzz

Code

Encoder

Python
import tach
from boltiot import Bolt, Sms
import math, statistics, time
import requests
import json

mybolt = Bolt(tach.api, tach.ID)

def compute_bounds(cumulative_data, MF, FS):       #polynomial regression
  if len(cumulative_data) < FS:
    return None

  if len(cumulative_data) > FS:
    del cumulative_data[0:len(cumulative_data - FS)]

  mn = statistics.mean(cumulative_data)    #mean
  var = 0            #variance

  for i in cumulative_data:
    var += math.pow((i-mn), 2)

  zs = MF * math.sqrt(var/FS)       #z-score
  UB = cumulative_data[FS - 1] + zs     #upper bound
  LB = cumulative_data[FS - 1] - zs     #lower bound
  return [LB, UB]

def tele_msg(msg):           #code to recieve msg on telegram
 try:
  URL = "https://api.telegram.org/" + tach.BOT + "/sendMessage"
  data = {
  "chat_id" : tach.chat_id,
  "text" : msg
  }
  response = requests.request(
  "POST",
  URL,
  params = data
  )
  resp = json.loads(response.text)
  return resp["ok"]
 except Exception as e:
  print(e)
  return False

cumulative_data = []

while 1:
 try:
  sv = mybolt.analogRead("A0")
  data = json.loads(sv)
  Pulse = int(data["value"])         #pulse = pulse per second
 if data["success"]!=1:
    print("couldn't fetch the data")
    print("this is the response, " +str(data["success"]))
  elif data["value"]!=3072:
    RPM = (Pulse * 60) / 20        # ppr = 20 
    print("measured rpm is: ", str(RPM))
 except Exception as e:
   print(e)

 bounds = compute_bounds(cumulative_data, tach.MF, tach.FS)
 if not bounds:
    data_required = tach.FS - len(cumulative_data)
    print(str(data_required) + " more data_points are required")
    cumulative_data.append(RPM)
    time.sleep(10)
    continue

 try:
  if RPM < bounds[0]:
   response = tele_msg("drop in speed sensed: " + str(RPM))
   print("this is the response from telegram: ", response)
  elif RPM > bounds[1]:

Credits

SAGAR M
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.