Help writing serial plugin

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
fluppie
Normal user
Posts: 81
Joined: 09 Oct 2015, 13:23

Help writing serial plugin

#1 Post by fluppie » 16 Jan 2019, 08:32

I use Plugin 165 from the playground, SerSwitch as a base:

Code: Select all

void getmcustate() {
  // 0x02, 0x30, 0x32, 0x36, 0x32, 0x03 / STX 0262 ETX
  Serial.write(0x02); // Startbit
  Serial.write(0x30); // 0
  Serial.write(0x32); // 2
  Serial.write(0x36); // 6
  Serial.write(0x32); // 2
  Serial.write(0x03); // Stopbit
  Serial.flush();
}

void sendmcucommand(byte btnnum, byte state, byte swtype, byte btnnum_mode) // btnnum=0,1,2, state=0/1
{
  byte sstate;

  switch (swtype)
  {
    case SER_SWITCH_YEWE:
      {
        Plugin_165_switchstate[btnnum] = state;
        if ((btnnum == 0) && (state == 1))
        {
        // {0x02,0x30,0x33,0x30,0x32,0x38,0x30,0x32,0x44,0x03};
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33);
        Serial.write(0x30);
        Serial.write(0x32);
        Serial.write(0x38);
        Serial.write(0x30);
        Serial.write(0x32);
        Serial.write(0x44);
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 1) && (state == 1))
        {
        // 0x02, 0x30, 0x33, 0x30, 0x32, 0x31, 0x30, 0x32, 0x36, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x32);
        Serial.write(0x31); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x36); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 2) && (state == 1))
        {
        // 0x02, 0x30, 0x37, 0x30, 0x30, 0x30, 0x38, 0x32, 0x46, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x37); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x30);
        Serial.write(0x30); // following data length 0x05
        Serial.write(0x38); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x46); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 3) && (state == 1))
        {
        // 0x02, 0x30, 0x37, 0x30, 0x30, 0x32, 0x30, 0x32, 0x39, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x37); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x30);
        Serial.write(0x32); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x39); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 0) && (state == 0))
        {
        // 0x02, 0x30, 0x33, 0x30, 0x32, 0x30, 0x30, 0x32, 0x35, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x32);
        Serial.write(0x30); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x35); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 1) && (state == 0))
        {
        // 0x02, 0x30, 0x33, 0x30, 0x32, 0x30, 0x30, 0x32, 0x35, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x32);
        Serial.write(0x30); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x35); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 2) && (state == 0))
        {
        // 0x02, 0x30, 0x33, 0x30, 0x32, 0x30, 0x30, 0x32, 0x35, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x32);
        Serial.write(0x30); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x35); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        }
        if ((btnnum == 3) && (state == 0))
        {
        // 0x02, 0x30, 0x33, 0x30, 0x32, 0x30, 0x30, 0x32, 0x35, 0x03
        Serial.write(0x02); // Startbit
        Serial.write(0x30);
        Serial.write(0x33); // version 00
        Serial.write(0x30); // Tuya command 06 - send order
        Serial.write(0x32);
        Serial.write(0x30); // following data length 0x05
        Serial.write(0x30); // relay number 1,2,3
        Serial.write(0x32); // ?
        Serial.write(0x35); // ?
        Serial.write(0x03); // Stopbit
        Serial.flush();
        break;
        }
      }
Launching the outputs works but I don't get feedback in the webinterface, stays 0

I found a python script who can process the answers from the other mcu over serial:

Code: Select all

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])
Anyone here understands the protocoll. I starts with 0x02 data end with 0x03 and 0x06 for ack on receiving data from the mcu.

fluppie
Normal user
Posts: 81
Joined: 09 Oct 2015, 13:23

Re: Help writing serial plugin

#2 Post by fluppie » 20 May 2019, 12:14

Who would like to help on this? I got a little bit further in understanding the serial protocoll and decoding it with regular Arduino code on a ESP8266.
Now I want to send/get results through an ESP Easy plugin. Who want's to offer help? If one of the main dev's want to help out I could donate to the project as a thank you?

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests