Go Down

Topic: Python to Arduino (Read 295 times) previous topic - next topic

fluppie

Hi, I found a Python script that reads data over a serial connection. This works with a RaspberryPi and Python 2.7.
But I want to run this code on a ESP8266/ESP32, so I/someone need to convert the script to Arduino code. Only I don't understand Python and have only basic knowledge of Arduino.
Are there people who can convert such scripts, if so, where is the right place to look for them? I would also agree to a small fee for help with this.

Code: [Select]


#!/usr/bin/python
# coding=utf-8
# 22.12.2011
# Dieses Programm kommuniziert mit Gira RWM

import serial
import signal
import sys
import binascii
import types
import time

# Seriellen Port eintragen
# ser = serial.Serial('/dev/ttyUSB0', 9600,)
ser = serial.Serial('COM7', 9600,)

# Steuerzeichen
null = binascii.unhexlify("00")
stx  = binascii.unhexlify("02")
etx  = binascii.unhexlify("03")
ack  = binascii.unhexlify("06")

# Daten im Format [Req, Res]
req_res = [
[stx+"0464"+etx,""],
[stx+"0868"+etx,""],
[stx+"0969"+etx,""],
[stx+"0B72"+etx,""],
[stx+"0C73"+etx,""],
[stx+"0D74"+etx,""],
[stx+"0E75"+etx,""]]

result = []

# Sauber Beenden mit Strg+C
def process_sigint(signum, frame):
print ("Ende")
ser.close()
sys.exit(0)

# Daten lesen
def get_data(p_req_res):
state = 0
data = ""
for rr in p_req_res:
# print "Sent Data ",rr[0]
ser.flushInput()
ser.write(rr[0])
while 1:
read = ser.read()
if read == null or read == ack:
# print "Got 1Data ", read, "->", binascii.b2a_hex(read)
continue
if read == stx:
# print "Begin"
state = 1
data = ""
if read == etx:
state = 0
data = data + read
# print "Got Data ", data, "->", binascii.b2a_hex(data)
rr[1] = data
ser.write(ack)
time.sleep(0.1)
break
if state == 1:
data = data + read

# Daten auswerten
def process_data(p_req_res):
n = 0
for rr in p_req_res:
item = ["",""]
n=n+1
if n == 1:
item = ["Serial",int(rr[1][3:11],16)]
elif n == 2:
item = ["?",int(rr[1][3:11],16)]
elif n == 3:
item = ["Betriebszeit",int(rr[1][3:11],16)/4/60/60]
elif n == 4:
item = ["Rauchkammer",int(rr[1][3:7],16)*0.003223]
result.append(item)
item = ["Anzahl Rauchalarm",int(rr[1][7:9],16)]
result.append(item)
item = ["Verschmutzungsgrad",int(rr[1][9:11],16)]
elif n == 5:
item = ["Spannung Batterie",int(rr[1][3:7],16)*0.018369]
result.append(item)
item = ["Temp 1",(int(rr[1][7:9],16)/2)-20]
result.append(item)
item = ["Temp 2",(int(rr[1][9:11],16)/2)-20]
elif n == 6:
item = ["Anzahl Thermoalarm",int(rr[1][3:5],16)]
result.append(item)
item = ["Anzahl Testalarm",int(rr[1][5:7],16)]
result.append(item)
item = ["Anzahl Alarm Draht",int(rr[1][7:9],16)]
result.append(item)
item = ["Anzahl Alarm Funk",int(rr[1][9:11],16)]
elif n == 7:
item = ["Anzahl Testalarm Draht",int(rr[1][3:5],16)]
result.append(item)
item = ["Anzahl Testalarm Funk",int(rr[1][5:7],16)]
result.append(item)




signal.signal(signal.SIGINT, process_sigint)


get_data(req_res)

#for rr in req_res:
# print rr[0], "-->",rr[1], "-->",binascii.b2a_hex(rr[1])

process_data(req_res)
for res in result:
print (res[0], "-->",res[1])




Robin2

Your description and Title seem to be in conflict.

Your title gives the impression that you want a Python program so send data to an Arduino.

Your description suggests that you want to do in C++ what you are currently doing in Python.

Depending on which it is these links may help get you started if you want to learn how to do it yourself

Python - Arduino demo
Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

fluppie

It is indeed what is in the description. Someone made a Python script for what I want to do in C++. As I'm not familiar with writing/reading Python script, I was looking for a place where people can help me to do this. I understand that no-one will do this for free, so more looking for a "marketplace" for someone with a project where developers can help out for a certain fee.

So I think my post should be moved to Gigs & Collaboration?

Go Up