Arduino MQTT convert


#1

Hi,
after 14 january my device stopped. Then I try to convert my sketch to MQTT.

I have a problem with library Timer.h it seems to be in conflict.

error: redefinition of 'class Timer’
error: ‘Timer’ has no member named 'update’
error: ‘Timer’ has no member named ‘pulse’

In my original sketch I have a defined a

// Timer
Timer t;

and then I use

void loop()
{
//Cayenne.run();
Cayenne.loop();
// Update timer t
t.update();
//
}

and in a function

t.pulse(RELAY_DIGITAL_PIN5, 1500, 0); //pin, time(milliseconds), initial state

Thanks to those who will help me.

Fabio


#2

it looks like the initializer “Timer” or “t” is being called more than once in some way. Did you recently change the instance “t” from something else? Could we see the whole code to help more?

Thanks


#3

Here is your error, you did not define them in your code.


#4

update and pulse is defined in library timer.h. With old sketch ( not MQTT ) it was six months that it worked without any problem.


#5

#include <OneWire.h>
#include <DallasTemperature.h>
// If you’re not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
//#include <CayenneEthernet.h>
#include <CayenneMQTTEthernet.h>
//https://github.com/JChristensen/Timer
#include <Timer.h>

// Virtual Pin of the DS18B20 widget.
const int VIRTUAL_PIN20 =20;
const int VIRTUAL_PIN21 =21;
// Virtual Pin of the Relay widget.
const int VIRTUAL_PIN5 =5;
const int VIRTUAL_PIN7 =7;
const int VIRTUAL_PIN8 =8;
#define RELAY_DIGITAL_PIN5 5

// Virtual PIN input
const int VIRTUAL_PIN3 =3; // VirtualPin Stato Climatizzazione (Fotoresistenza)
#define DIGITAL_INPUT3 3 //Stato Climatizzazione (Fotoresistenza)
#define DIGITAL_INPUT9 9 //Stato centrale ( OC - inserito/disinserito )
// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define tmpPin 2 // PIN onewire
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);
// Indirizzi sonde
DeviceAddress sondaext = {0x28, 0xFF, 0x96, 0xB2, 0x85, 0x16, 0x03, 0x06};//sonda esterna
DeviceAddress sondaint = {0x28, 0xF5, 0xD6, 0xAD, 0x08, 0x00, 0x00, 0x54};//sonda interna
// contatore
unsigned long counter = 0;// contatore per il tempo
// Variabili temperatura
float temp_ext = 0; // Temperatura esterna
float temp_int = 0; // Temperatura interna
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.

// Cayenne MQTTauthentication token.
char username[] = “xxxxxxxxxxxxxxxx”;
char password[] = “xxxxxxxxxxxxxxxx”;
char clientID[] = “xxxxxxxxxxxxxxxx”;

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { xxxxxxxxxxxxxxxx};
IPAddress arduino_ip(192, 168, 0, 210);
IPAddress dns_ip(8, 8, 8, 8);
IPAddress gateway_ip(192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
// Timer
Timer t;
//
byte pw=0;
byte pw1=0;

void setup()
{
// set digital pin to output
pinMode(RELAY_DIGITAL_PIN5, OUTPUT);
// set relay to low —> OFF
digitalWrite(RELAY_DIGITAL_PIN5, LOW);
//
counter=millis();
//serial
Serial.begin(9600);
Cayenne.begin(username, password, clientID, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
// Dallas sensor begin
sensors.begin();
}

void loop()
{

Cayenne.loop();
// Update timer t
t.update();
//
if((millis()-counter)>1000){
syncData();
Temperatura();
counter=millis();
}
// delay
delay(1);
}
// FUNZIONE RICHIESTA TEMPERATURA A SENSORE DS18B20
void Temperatura(){
sensors.requestTemperatures(); //Richiesta temperatura
temp_ext = sensors.getTempC(sondaext); // sonda esterna
temp_int = sensors.getTempC(sondaint); // sonda interna
temp_int-=0.6; // Calibro la sonda interna … differenza misurata 0.4-0.7
}
// This function syncs the Virtual Pin 5 data.
void syncData()
{
//CAYENNE_LOG(“Sync data”);
// Sync the Virtual Pin. This sends a request to the server to resend data for the pin.
// This will cause the CAYENNE_IN function to be called.
Cayenne.syncVirtual(VIRTUAL_PIN5);
Cayenne.syncVirtual(VIRTUAL_PIN7);
Cayenne.syncVirtual(VIRTUAL_PIN8);
}
//
// This function is called when the Cayenne widget requests data for the Virtual Pin.
void function_temperatura_Interna(int VIRTUAL_PIN20)
{
//This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN20, temp_int);
}
void function_temperatura_Esterna(int VIRTUAL_PIN21)
{
//This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN21, temp_ext);
}
void function_climatizzazione(int VIRTUAL_PIN3)
{
//IF DIGITAL PIN IS LOW THEN VIRTUAL PIN IS HIGH
bool stato=digitalRead (DIGITAL_INPUT3);
if (stato == HIGH) {
Cayenne.virtualWrite (VIRTUAL_PIN3, 0);
}
else{
Cayenne.virtualWrite (VIRTUAL_PIN3, 1);
}

}
CAYENNE_IN(VIRTUAL_PIN7)
{
pw = getValue.asInt(); // 0 to 1
}
CAYENNE_IN(VIRTUAL_PIN8)
{
pw1 = getValue.asInt(); // 0 to 1
}
CAYENNE_IN(VIRTUAL_PIN5)
{

if (pw == 1 && pw1 == 1) {
//get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue != digitalRead(DIGITAL_INPUT9) ) {
t.pulse(RELAY_DIGITAL_PIN5, 1500, 0); //pin, time(milliseconds), initial state
Cayenne.virtualWrite(VIRTUAL_PIN5, currentValue);
Cayenne.virtualWrite(VIRTUAL_PIN7, 0);
Cayenne.virtualWrite(VIRTUAL_PIN8, 0);
}
else{
Cayenne.virtualWrite(VIRTUAL_PIN5, digitalRead(DIGITAL_INPUT9) );
}
}
else {
Cayenne.virtualWrite(VIRTUAL_PIN5, digitalRead(DIGITAL_INPUT9) );
}

}


#6

Try to change Timer t; to Timer timer_something; also in rest of the code t to timer_something.


#7

I agree with @Agroelektronik . Change your “t”. It is prob conflicting with a variable in one of your libraries.


#8

No way. The error is always the same.

I think the problem is the class Timer that is in MQTTArduino.h

MQTTArduino.h:28:17: error: previous definition of ‘class Timer’ typedef struct Timer
error: redefinition of 'class Timer’
error: ‘Timer’ has no member named 'update’
error: ‘Timer’ has no member named ‘pulse’


#9

I tryed the code and i get collisions with MQTT lib in few lines of library code.
Editing MQTT lib would not be smart because of the future projects.
If its is not mandatory to use Timer lib, use this one

https://github.com/jfturcot/SimpleTimer

Works with old and new libs in my projects without any issues


#10

Thanks fo reply.

Try to solve the problem at the source.
In my project I need a button (not a switch), something that emulates a button press for one second.

t.pulse(RELAY_DIGITAL_PIN5, 1500, 0); //pin, time(milliseconds), initial state

The function t.pulse, does nothing more than bring state 1 to 1500ms and return to 0.
It’s connected to a solid state relay which activates and deactivates a load.

Is there any other function in the new MQTT library that does this?


#11

you can use this:

Cayenne.virtualWrite(Virtual_Pin, 0);


#12

Cayenne.virtualWrite(Virtual_Pin, 0);

has not a timer .


#13

You can also use millis() https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing