Relay oil tank counter to cayenne code conversion help


#1

Hello we successfully connected our fuel counter to arduino and it counts the litres and works on serial monitor. I have converted the code to send to cayenne but having trouble can someone please review to make sure all is right in the cayenne loop?

 #define CAYENNE_PRINT Serial  // Comment this out to disable prints 
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. 
char username[] = "";
char password[] = "";
char clientID[] = "";

int state=LOW;
int lastState=LOW;
int count=0;
int state2=LOW;
int lastState2=LOW;
int count2=0;
int state3=LOW;
int lastState3=LOW;
int count3=0;
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3


void setup()
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  pinMode(8, INPUT);
  state=digitalRead(8);
  pinMode(9, INPUT);
  state2=digitalRead(9);
  pinMode(10, INPUT);
  state3=digitalRead(10);
}

void loop(){
{
  Cayenne.loop();
}
  if (state==HIGH && lastState==LOW){
  count++;
  }
  if (state2==HIGH && lastState2==LOW){
  count2++;
  }
  if (state3==HIGH && lastState3==LOW){
  count3++;
  }
  lastState=state;
  state=digitalRead(8);
  lastState2=state2;
  state2=digitalRead(9);
  lastState3=state3;
  state3=digitalRead(10);
  delay (500);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL1)
{
  Cayenne.virtualWrite(1, count, "counter", "null");
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL2)
{
  Cayenne.virtualWrite(2, count2, "counter", "null");
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL3)
{
  Cayenne.virtualWrite(3, count3, "counter", "null");
}

#2

all is fine with your code. what trouble are you facing?


#3

well the first time it didn’t connect to the dashboard but that was because of the w5100 ethernet shield
" widespread issue impacting cheap chinese clones of the arduino ethernet modules which incorrectly include a 510 Ohm resistor network (labelled 511) on the network cable connection instead of the specified 49.9 Ohm resistor network."
we solved that by adding 100 ohm resistors will try again and let you know

thx


#4

After adding 100 ohm resistor did it connect?


#5

yes we have a big network with 3+ switches the chinese shield works fine with short cable but once u use many switches and long cable dhcp fails…

I saw after reading this thread, that i had the 511 resistor on my board. So i did as k1ggi did. Soldered 2x 100ohm resistors to pins 1-2 and 3-6. Works perfect with the DHCP address printer example ethernet sketch. I am connected to a Tp-link router, and i am using a arduino Mega, also chinese clone.

from

https://forum.arduino.cc/index.php?topic=351477.30


#6

pins also note it says 511 smb resistor it should say 510


#7

hello i have tried the below code i converted to cayenne and the arduino disconects after 3 min and doesnt come back online.

  #define CAYENNE_PRINT Serial  // Comment this out to disable prints 
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. 
char username[] = "";
char password[] = "";
char clientID[] = "";

int state=LOW;
int lastState=LOW;
int count=0;
int state2=LOW;
int lastState2=LOW;
int count2=0;
int state3=LOW;
int lastState3=LOW;
int count3=0;
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3


    void setup()
    {
      Serial.begin(9600);
      Cayenne.begin(username, password, clientID);
      pinMode(8, INPUT);
      state=digitalRead(8);
      pinMode(9, INPUT);
      state2=digitalRead(9);
      pinMode(10, INPUT);
      state3=digitalRead(10);
    }

    void loop(){
    {
      Cayenne.loop();
    }
      if (state==HIGH && lastState==LOW){
      count++;
      }
      if (state2==HIGH && lastState2==LOW){
      count2++;
      }
      if (state3==HIGH && lastState3==LOW){
      count3++;
      }
      lastState=state;
      state=digitalRead(8);
      lastState2=state2;
      state2=digitalRead(9);
      lastState3=state3;
      state3=digitalRead(10);
      delay (500);
    }

    // This function is called at intervals to send sensor data to Cayenne.
    CAYENNE_OUT(VIRTUAL_CHANNEL1)
    {
      Cayenne.virtualWrite(1, count, "counter", "null");
    }

    // This function is called at intervals to send sensor data to Cayenne.
    CAYENNE_OUT(VIRTUAL_CHANNEL2)
    {
      Cayenne.virtualWrite(2, count2, "counter", "null");
    }

    // This function is called at intervals to send sensor data to Cayenne.
    CAYENNE_OUT(VIRTUAL_CHANNEL3)
    {
      Cayenne.virtualWrite(3, count3, "counter", "null");
    }

the original code works fine and it counts relay here is the original without the cayenne conversion

int state=LOW;
int lastState=LOW;
int count=0;
int state2=LOW;
int lastState2=LOW;
int count2=0;
int state3=LOW;
int lastState3=LOW;
int count3=0;

void setup(){
Serial.begin(9600);
pinMode(8, INPUT);
state=digitalRead(8);
pinMode(9, INPUT);
state2=digitalRead(9);
pinMode(10, INPUT);
state3=digitalRead(10);
}

void loop(){
if (state==HIGH && lastState==LOW){
count++;
Serial.print("Counter1=");
Serial.println(count);
}
if (state2==HIGH && lastState2==LOW){
count2++;
Serial.print("Counter2=");
Serial.println(count2);
}
if (state3==HIGH && lastState3==LOW){
count3++;
Serial.print("Counter3=");
Serial.println(count3);
}
lastState=state;
state=digitalRead(8);
lastState2=state2;
state2=digitalRead(9);
lastState3=state3;
state3=digitalRead(10);
delay (500);
}

can someone please help since its obviouse the problem is from the cayenne loop since it disconects i have also tried adding the cayenne.loop() at the end of the void loop but still disconnects after a few minutes


#8

Add this line #define CAYENNE_DEBUG in your code and check your serial monitor for cause of dissconnect. Post the error from the serial monitor here.


#9

Dont use pin 10 as input, change it to something else beside 10 11 12 13, they are reserved for communication.


#10

ok thx will add debug and change pin 10
and will post the error from serial


#11

ok I changed the pins to 7 8 9 and it still disconnect after a couple of minutes we also have on the same switch 3 other arduinos with w5100 ethernet shield from china I noticed from the debug that they all have the exact same mac address as soon as I switch them off it stopped disconnecting

uno

[0] MAC: FE-64-78-5E-6B-B2
[513] IP: 192.168.1.106
[514] Connecting to mqtt.mydevices.com:1883
[1143] Connected
[1328] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
[1339] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
[1416] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[1489] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[1560] Publish: topic 8, channel 65534, value W5100, subkey , key 
[4638] Publish: topic 1, channel 1, value 0, subkey null, key counter
[4646] Publish: topic 1, channel 2, value 0, subkey null, key counter
[4721] Publish: topic 1, channel 3, value 0, subkey null, key counter
[13825] Connection ok
[19845] Publish: topic 1, channel 1, value 0, subkey null, key counter
[19854] Publish: topic 1, channel 2, value 0, subkey null, key counter
[19929] Publish: topic 1, channel 3, value 1, subkey null, key counter
[24518] Connection ok
[35053] Publish: topic 1, channel 1, value 0, subkey null, key counter
[35063] Publish: topic 1, channel 2, value 0, subkey null, key counter
[35138] Publish: topic 1, channel 3, value 1, subkey null, key counter
[35213] Connection ok
[45768] Connection ok
[50284] Publish: topic 1, channel 1, value 0, subkey null, key counter
[50293] Publish: topic 1, channel 2, value 0, subkey null, key counter
[50368] Publish: topic 1, channel 3, value 1, subkey null, key counter
[56463] Connection ok
[63484] Disconnected
[63485] Connecting to mqtt.mydevices.com:1883
[63955] Connected
[64141] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
[64153] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
[64230] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[64305] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[64376] Publish: topic 8, channel 65534, value W5100, subkey , key 
[65951] Publish: topic 1, channel 1, value 0, subkey null, key counter
[65960] Publish: topic 1, channel 2, value 0, subkey null, key counter
[66036] Publish: topic 1, channel 3, value 2, subkey null, key counter
[67615] Connection ok
[78150] Connection ok
[81161] Publish: topic 1, channel 1, value 0, subkey null, key counter
[81170] Publish: topic 1, channel 2, value 0, subkey null, key counter
[81246] Publish: topic 1, channel 3, value 2, subkey null, key counter
[88845] Connection ok
[96370] Publish: topic 1, channel 1, value 0, subkey null, key counter
[96379] Publish: topic 1, channel 2, value 0, subkey null, key counter
[96454] Publish: topic 1, channel 3, value 2, subkey null, key counter
[99539] Connection ok
[110074] Connection ok
[111580] Publish: topic 1, channel 1, value 0, subkey null, key counter
[111590] Publish: topic 1, channel 2, value 0, subkey null, key counter
[111666] Publish: topic 1, channel 3, value 3, subkey null, key counter
[120771] Connection ok
[126792] Publish: topic 1, channel 1, value 0, subkey null, key counter
[126802] Publish: topic 1, channel 2, value 0, subkey null, key counter
[126878] Publish: topic 1, channel 3, value 3, subkey null, key counter
[131469] Connection ok
[142004] Publish: topic 1, channel 1, value 0, subkey null, key counter
[142015] Publish: topic 1, channel 2, value 0, subkey null, key counter
[142090] Publish: topic 1, channel 3, value 3, subkey null, key counter
[143166] Disconnected
[143166] Connecting to mqtt.mydevices.com:1883
[143639] Connected
[143834] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
[143848] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
[143926] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[144002] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[144074] Publish: topic 8, channel 65534, value W5100, subkey , key 
[145650] Connection ok
[156185] Connection ok
[157690] Publish: topic 1, channel 1, value 0, subkey null, key counter
[157701] Publish: topic 1, channel 2, value 0, subkey null, key counter
[157776] Publish: topic 1, channel 3, value 4, subkey null, key counter
[166882] Connection ok
[172902] Publish: topic 1, channel 1, value 0, subkey null, key counter
[172913] Publish: topic 1, channel 2, value 0, subkey null, key counter
[172989] Publish: topic 1, channel 3, value 4, subkey null, key counter
[177580] Connection ok
[188114] Publish: topic 1, channel 1, value 0, subkey null, key counter
[188125] Publish: topic 1, channel 2, value 0, subkey null, key counter
[188200] Publish: topic 1, channel 3, value 4, subkey null, key counter
[188277] Connection ok
[198834] Connection ok
[203350] Publish: topic 1, channel 1, value 0, subkey null, key counter
[203360] Publish: topic 1, channel 2, value 0, subkey null, key counter
[203436] Publish: topic 1, channel 3, value 5, subkey null, key counter
[209531] Connection ok
[218562] Publish: topic 1, channel 1, value 0, subkey null, key counter
[218572] Publish: topic 1, channel 2, value 0, subkey null, key counter
[218647] Publish: topic 1, channel 3, value 5, subkey null, key counter
[219723] Disconnected
[219724] Connecting to mqtt.mydevices.com:1883
[220258] Connected
[220442] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
[220456] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
[220534] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[220610] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[220683] Publish: topic 8, channel 65534, value W5100, subkey , key 
[222259] Connection ok
[232794] Connection ok
[234299] Publish: topic 1, channel 1, value 0, subkey null, key counter
[234309] Publish: topic 1, channel 2, value 0, subkey null, key counter
[234386] Publish: topic 1, channel 3, value 5, subkey null, key counter
[243491] Connection ok
[249511] Publish: topic 1, channel 1, value 0, subkey null, key counter
[249522] Publish: topic 1, channel 2, value 0, subkey null, key counter
[249597] Publish: topic 1, channel 3, value 6, subkey null, key counter
[254188] Connection ok


mega 

[0] MAC: FE-64-78-5E-6B-B2 
[4526] IP: 192.168.1.106
[4527] Connecting to mqtt.mydevices.com:1883
[5156] Connected
[5342] Publish: topic 4, channel 65534, value Arduino Mega, subkey , key 
[5354] Publish: topic 6, channel 65534, value ATmega2560, subkey , key 
[5430] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[5504] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[5574] Publish: topic 8, channel 65534, value W5100, subkey , key 
[8653] Publish: topic 1, channel 1, value 0, subkey null, key counter
[8662] Publish: topic 1, channel 2, value 0, subkey null, key counter
[8735] Publish: topic 1, channel 3, value 1, subkey null, key counter
[17840] Connection ok
[23861] Publish: topic 1, channel 1, value 0, subkey null, key counter
[23870] Publish: topic 1, channel 2, value 0, subkey null, key counter
[23945] Publish: topic 1, channel 3, value 1, subkey null, key counter
[28535] Connection ok
[39071] Publish: topic 1, channel 1, value 0, subkey null, key counter
[39080] Publish: topic 1, channel 2, value 0, subkey null, key counter
[39155] Publish: topic 1, channel 3, value 1, subkey null, key counter
[39230] Connection ok
[49787] Connection ok
[54303] Publish: topic 1, channel 1, value 0, subkey null, key counter
[54312] Publish: topic 1, channel 2, value 1, subkey null, key counter
[54387] Publish: topic 1, channel 3, value 2, subkey null, key counter
[60483] Connection ok
[67504] Disconnected
[67505] Connecting to mqtt.mydevices.com:1883
[67990] Connected
[68175] Publish: topic 4, channel 65534, value Arduino Mega, subkey , key 
[68189] Publish: topic 6, channel 65534, value ATmega2560, subkey , key 
[68267] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[68341] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[68413] Publish: topic 8, channel 65534, value W5100, subkey , key 
[69988] Publish: topic 1, channel 1, value 0, subkey null, key counter
[69997] Publish: topic 1, channel 2, value 1, subkey null, key counter
[70072] Publish: topic 1, channel 3, value 2, subkey null, key counter
[71652] Connection ok
[82188] Connection ok
[85198] Publish: topic 1, channel 1, value 0, subkey null, key counter
[85208] Publish: topic 1, channel 2, value 1, subkey null, key counter
[85282] Publish: topic 1, channel 3, value 2, subkey null, key counter
[92883] Connection ok
[100409] Publish: topic 1, channel 1, value 0, subkey null, key counter
[100419] Publish: topic 1, channel 2, value 1, subkey null, key counter
[100495] Publish: topic 1, channel 3, value 3, subkey null, key counter
[103581] Connection ok
[114117] Connection ok
[115622] Publish: topic 1, channel 1, value 0, subkey null, key counter
[115634] Publish: topic 1, channel 2, value 1, subkey null, key counter
[115709] Publish: topic 1, channel 3, value 3, subkey null, key counter
[124816] Connection ok
[130837] Publish: topic 1, channel 1, value 0, subkey null, key counter
[130847] Publish: topic 1, channel 2, value 1, subkey null, key counter
[130923] Publish: topic 1, channel 3, value 3, subkey null, key counter
[135515] Connection ok
[146051] Publish: topic 1, channel 1, value 0, subkey null, key counter
[146061] Publish: topic 1, channel 2, value 1, subkey null, key counter
[146136] Publish: topic 1, channel 3, value 4, subkey null, key counter
[147212] Disconnected
[147213] Connecting to mqtt.mydevices.com:1883
[147687] Connected
[147872] Publish: topic 4, channel 65534, value Arduino Mega, subkey , key 
[147887] Publish: topic 6, channel 65534, value ATmega2560, subkey , key 
[147965] Publish: topic 7, channel 65534, value 16000000, subkey , key 
[148041] Publish: topic 5, channel 65534, value 1.2.0, subkey , key 
[148114] Publish: topic 8, channel 65534, value W5100, subkey , key 
[149689] Connection ok

the uno and the mega have different w5100 shields but with exact same MAC I will now set the MAC addresses manually.
Please beware of these chinese w5100 they gave us alote of trouble 1st wrong resistor now same mac.
the counters and code works fine though thanks for the help!


#12

try setting them manual and see what is output. you can find the manual connection code here https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ManualConnection/ManualConnection.ino .

had you kept the other devices ON while uploading the code to the device, so that a different MAC address is allotted ti the device in which code is being uploaded .