Code pour contrôler Aqua Rite
Portion on C voir plus bas pour python
Il faut envoyer le message toutes les secondes
#define COM_BAUDRATE 9600 //baudrate, e.g. 9600, 19200, 57600, 115200
#define COM_PARITY_NONE 0 //no parity
#define COM_PARITY_ODD 1 //odd parity
#define COM_PARITY_EVEN 2 //even parity
#define BUFFER_SIZE 20 //maximum number of bytes to send
//assigning inputs to variables, these variables are READ-ONLY
long input(0) signal0; //pourcentage chlorinator
long input(1) signal1; //flag error
long input(2) signal4; //super chlorinate
long output(0) ppm; //salt ppm
long output(1) errorflag; //Error flag
long output(2) pct; //Chlorinator pct actual
long output(3) writeerror; // 2021-05-03 write return code (number of bytes=8 when ok, minus is an error)
long output(4) recverror; // 2021-05-03 recv return code (number of bytes=9 when ok, minus is an error)
bool output(5) checkcell;
long output(14) numcomreset; // 2021-05-03 number of errors requiring restart of uart
//assigning variables to outputs, these variables are WRITE-ONLY
long output(15) handle; //handle of the serial device
//declaration of variables
long hCom1; //communication handle
long buffer[BUFFER_SIZE]; //buffer for incoming data
long rbuffer[BUFFER_SIZE]; //buffer for incoming data
long dataCnt; //number of bytes sent
long convData[2]; //array for data conversions
long errors;
bool read;
/* Initialization of the REXLANG algorithm */
// the init procedure is executed once when the REXLANG function block initializes
long init(void)
{
hCom1 = -1;
writeerror=0;
errors=0;
numcomreset=0;
read=false;
checkcell=false;
return 0;
}
/* The body of the REXLANG algorithm */
// the main procedure is executed once in each sampling period
long main(void)
{
if (hCom1<0)
{
//Close(hCom1); // added 2021-05-01 when error trying to close and re-open port.
hCom1 = OpenCom("/dev/ttySC0",COM_BAUDRATE,COM_PARITY_NONE); //opening serial device ttyUSB0 ttyS0
}
else
{
if (!read) {
buffer[0] = 0x10;
buffer[1] = 0x02;
buffer[2] = 0x50;
buffer[3] = 0x11;
if (signal1 == 0)
{
buffer[4] = signal0 & 0xFF; // 0x03;
if(signal4 == 1 )
{
buffer[4] = 0x65; // At 100% we consider superchlorinate so we set to 65 instead of 64
}
}
else{
buffer[4] = 0xff;
}
pct=buffer[4];
buffer[5] = buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4]; // 0x78; // Checksum sum of bytes %256
buffer[6] = 0x10;
buffer[7] = 0x03;
//now all the data can be sent
writeerror = Write(hCom1,buffer,8); //send data, number of bytes = 21
buffer[4]=0x00;
buffer[5]=0xff;
// 10 02 00 12 42 00 66 10 03 Normal receive 42 is the hex for salt / 50
// 10 02 00 12 40 20 84 10 03 ERROR unplug 20 is an error
// 10 02 00 12 3E 10 00 72 10 00 03 When check cell is flashing
read=true;
}
else {
dataCnt = Read(hCom1,rbuffer,BUFFER_SIZE); //receive data, max number of bytes = BUFFER_SIZE
recverror = dataCnt; // 2021-05-03
// Ignore 3 first bytes always 10 02 00
if (dataCnt>=9)
{
if (dataCnt==9)
{
ppm=rbuffer[4] * 50;
errorflag=rbuffer[5];
checkcell=false;
}
if (dataCnt==10)
{
ppm=rbuffer[4] * 50;
checkcell=true;
errorflag=signal0 & 0xFF; // if check cell is flashing we are not getting the pct
}
}
else {
ppm=0;
pct=0;
errorflag=1;
errors++;
if (errors > 2 )
{
Close(hCom1); // added 2021-05-01 when error trying to close and re-open port.
hCom1 = OpenCom("/dev/ttySC0",COM_BAUDRATE,COM_PARITY_NONE); // added 2021-05-01 when error trying to close and re-open port.
numcomreset++;
errors=0;
}
}
read=false;
}
}
//publishing the serial communication handle through output signal (for debugging)
handle = hCom1;
return 0;
}
/* Closing the REXLANG algorithm */
//the exit procedure is executed once when the task is correctly terminated
// (system shutdown, downloading new control algorithm, etc.)
long exit(void)
{
if(hCom1>=0) Close(hCom1);
return 0;
}
!/usr/bin/python3
import serial
import paho.mqtt.client as mqtt
import time
global pct
pct=0
def on_message(client, userdata, message):
global pct
#print(“message received ” ,str(message.payload.decode(“utf-8”)))
#pct=int(message.payload.decode(“utf-8”))
#print(“PCTMQTT: ” + str(pct))
ser = serial.Serial('/dev/ttySC0', 9600, timeout=1)
client =mqtt.Client("raspSpaAquaRite")
client.connect("xxxx", port=1883, keepalive=60 )
client.subscribe("raspSpa/AquaRitePCT")
client.on_message = on_message
global pct
pct=101
client.loop_start()
while (1==1):
#print("PCT: " + str(pct))
chksum = ((0x10 + 0x02 + 0x50 + 0x11 + pct) %256 )
#print(chksum)
values = bytearray([0x10, 0x02, 0x50, 0x11, pct, chksum, 0x10, 0x03])
ser.write(values)
datareturn=ser.read(size=10)
#print(datareturn)
ppm=datareturn[4]*50
#print(ppm)
client.publish("raspSpa/AquaRitePPM",ppm)
client.publish("raspSpa/AquaRitePCTAct",pct)
time.sleep(2)
ser.close()