buenos días este es el código
#include<ModbusMaster.h>
#include<CayenneMQTTEthernet.h>
char username = “aa3b8080-a99c-11ea-93bf-d33a96695544”;
char password = “071871dc4273146c6038f8f147db4ab76122c73e”;
char clientID = “606c2fc0-b970-11ea-a67f-15e30d90bbf4”;
//Definicion de pines virtuales y fisico para cayenne
#define VIRTUAL_CHANNEL_13 13
#define VIRTUAL_CHANNEL_14 14
#define VIRTUAL_CHANNEL_15 15
#define ACTUATOR_PINN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
//permite habilitar y deshabilitar la comunicacion del MAX RS485
#define MAX485_DE 7 //3
#define MAX485_RE_NEG 6 //2
uint8_t j;
uint16_t data[8],mw1,result;
word Estado_PS,Estado_Auma;
float PIT01_C1,PIT02_C1,FIT01_C1,PV01_C1;
bool falla_UPS,falla_suministro,falla_valvula;
//******************************************
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{//----------------------configuracion for cayenne-------------
pinMode(4,OUTPUT);
pinMode(2,INPUT_PULLUP);
// attachInterrupt(0, blink, CHANGE);
//------------------------------------------
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
pinMode(8,OUTPUT);
// inicia en modo recepecion
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
//velocidad de comunicacion serial modbus
Serial.begin(9600);
// esclavo modbus 4
node.begin(4, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
pinMode(ACTUATOR_PINN, OUTPUT);
Cayenne.begin(username, password, clientID);
}
bool state = true;
bool M3;
void loop() {
// Toggle the coil at address 0x0002 (Manual Load Control)
// result = node.writeSingleCoil(0x0002, state);
// state = !state;
result = node.writeSingleCoil(0x0003, M3);
//*******************************************************
node.setTransmitBuffer(6,mw1);
result = node.writeMultipleRegisters(0,8);
// Read 16 registers starting at 0x3100)
// result = node.readInputRegisters(0x3100, 16);
result = node.readHoldingRegisters(420,8);
// result = node.readCoils(0,8);
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 8; j++)
{
data[j] = node.getResponseBuffer(j);
if(data[1]<100){
digitalWrite(8,HIGH);
}
else{digitalWrite(8,LOW); }
}
}
delay(10);
PIT01_C1=data[0]/100.0;
PIT02_C1=data[1]/100.0;
FIT01_C1=data[2]/10.0;
PV01_C1=data[3]/100.0;
Estado_PS=data[4];
Estado_Auma=data[5];
if(bitRead(Estado_PS,0)==1){
falla_suministro=true;}
else{falla_suministro=false;}
if(bitRead(Estado_PS,1)==1){
falla_UPS=true;}
else
{falla_UPS=false;}
if(bitRead(Estado_Auma ,15)==1){
falla_valvula=true;}
else
{falla_valvula=false;}
// PV03_A=data[6]/100.0;
// PV04_B=data[7]/100.0;
Cayenne.loop();
}
CAYENNE_IN(VIRTUAL_CHANNEL_13)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL2, ACTUATOR_PINN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PINN, value);
}
//CAYENNE_IN(VIRTUAL_CHANNEL_14)
//{
//
// CAYENNE_LOG(“Got a value: %s”, getValue.asInt());
// mw1=getValue.asInt();
//}
CAYENNE_IN(VIRTUAL_CHANNEL_15)
{
CAYENNE_LOG(“Got a value: %s”, getValue.asInt());
M3=getValue.asInt();
}
//Salidas de modulo arduino to cayenne
CAYENNE_OUT(9)
{
CAYENNE_LOG(“Send data for Virtual Channel 9”);
// This command writes the device’s uptime in seconds to the Virtual Channel.
Cayenne.virtualWrite(9, millis());
}
CAYENNE_OUT(0)
{
CAYENNE_LOG(“Send data for Virtual Channel 0”);
Cayenne.virtualWrite(0, PIT01_C1);
}
CAYENNE_OUT(1)
{
CAYENNE_LOG(“Send data for Virtual Channel 1”);
Cayenne.virtualWrite(1,PIT02_C1);
}
CAYENNE_OUT(2)
{
CAYENNE_LOG(“Send data for Virtual Channel 2”);
Cayenne.virtualWrite(2,FIT01_C1);
}
CAYENNE_OUT(3)
{
CAYENNE_LOG(“Send data for Virtual Channel 3”);
Cayenne.virtualWrite(3,PV01_C1);
}
CAYENNE_OUT(4)
{
CAYENNE_LOG(“Send data for Virtual Channel 2”);
Cayenne.virtualWrite(4,falla_suministro);
}
CAYENNE_OUT(5)
{
CAYENNE_LOG(“Send data for Virtual Channel 2”);
Cayenne.virtualWrite(5,falla_UPS);
}
//CAYENNE_OUT(VIRTUAL_CHANNEL)
//{
// // Read data from the sensor and send it to the virtual channel here.
// // For example, to send a digital value you can use the following:
// int value = digitalRead(SENSOR_PIN);
// Cayenne.virtualWrite(VIRTUAL_CHANNEL, value, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
CAYENNE_OUT(6)
{
CAYENNE_LOG(“Send data for Virtual Channel 2”);
Cayenne.virtualWrite(6,falla_valvula);
}