Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Python code for Stepper Gauge
import socket
import sys
import json
import RPi.GPIO as GPIO
import socket
import time
step_pin = 12
direction_pin = 16
current_position = 0
def Main():
host = '192.168.1.104'
port = 50008
GPIO.setmode(GPIO.BCM)
GPIO.setup(step_pin, GPIO.OUT)
GPIO.setup(direction_pin, GPIO.OUT)
while(1):
try:
mySocket = socket.socket()
mySocket.connect((host,port))
message = "get_powers"
mySocket.send(message.encode())
data = mySocket.recv(1024).decode()
print ('Received from server: ' + data)
mySocket.close()
except:
raise
try:
thepowers = json.loads(data)
new_position = int(thepowers['totalwatts'])
MoveMotorAbs(new_position, 1)
except:
raise
time.sleep(2)
def MoveMotorAbs(new_position, speed=100):
global current_position
moveto = new_position - current_position
current_position = new_position
print("moving to {}".format(moveto))
if(moveto > 0):
GPIO.output(direction_pin, GPIO.LOW)
print("forward")
else:
GPIO.output(direction_pin, GPIO.HIGH)
print("reverse")
for i in range(0, abs(moveto)):
GPIO.output(step_pin, GPIO.HIGH)
time.sleep(speed*0.001)
GPIO.output(step_pin, GPIO.LOW)
time.sleep(speed*0.001)
current_position = new_position
if __name__ == "__main__":
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment