0

Pushing data from Arduino to Adafruit IO feeds.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pushing data from Arduino to Adafruit IO feeds.

by schmidt_be on Tue Jun 12, 2018 11:39 pm

Edit - Code now added.

Hi there,

Any help much appreciated!

I have created an RFID scanning/logging system to mark attendance (using an Arduino Yun). Everything works perfectly except one part and I'm wondering if it is related to Adafruit IO. When I scan the first card it pushes the value to my Adafruit IO feed and it all works fine (IFTTT then triggers the card ID to be logged to a Dropbox text file) . When I swipe the second card it states "error ping failed". If I wait exactly 5 minutes between card scans it works perfectly on the second card. Once again I need to wait 5 minutes to successfully scan the third card. I don't think the problem is in my arduino code.

I have timed it and each time it is exactly 5 minutes before my arduino is "connected" and ready to scan again.

Is this related to me having a free Adafruit IO account? Should I be able to push data faster/more regularly than every 5 minutes?

Thanks for your help.

Ben.

Code:

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "" // TODO - insert username
#define AIO_KEY "" // TODO - insert app key

#include <Bridge.h>
#include <Console.h>
#include <BridgeClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define SS_PIN 10
#define RST_PIN 5

// values to send to different Adafruit IO Feeds
// initialise as many as you need (same number as Feeds below)
uint32_t valueToSend1;
uint32_t valueToSend2;
uint32_t valueToSend3;

// Note - The Update encourages the user to insert the Card's UIDs directly in the loop
// please locate the TODO in the loop

BridgeClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Initialise Feeds - You can initialise as many feeds as you want depending on amount of employees
// make sure to create these feeds on Adafruit IO - only 1 subscription required
Adafruit_MQTT_Publish sendToDropbox1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox1");
Adafruit_MQTT_Publish sendToDropbox2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox2");
Adafruit_MQTT_Publish sendToDropbox3 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox3");
Adafruit_MQTT_Subscribe subscribeA = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/subscribeA");

// now we will create loops that publish to a certain feed depending on card
// create as many loops as the amount of employees you want to monitor
void sendEmployee1()
{
Serial.print("Publishing ");
Serial.println(valueToSend1);
Console.println(valueToSend1);

if(!sendToDropbox1.publish(valueToSend1))
{
Serial.println(F(" Error - Failed to Send Data"));
}
else
{
Serial.println(F(" Success - Data Sent"));
}
}

void sendEmployee2()
{
Serial.print("Publishing ");
Serial.println(valueToSend2);
Console.println(valueToSend2);

if(!sendToDropbox2.publish(valueToSend2))
{
Serial.println(F(" Error - Failed to Send Data"));
}
else
{
Serial.println(F(" Success - Data Sent"));
}
}

void sendEmployee3()
{
Serial.print("Publishing ");
Serial.println(valueToSend3);
Console.println(valueToSend3);

if(!sendToDropbox3.publish(valueToSend3))
{
Serial.println(F(" Error - Failed to Send Data"));
}
else
{
Serial.println(F(" Success - Data Sent"));
}
}



void MQTTConnect()
{
int8_t ret;

if(mqtt.connected()) // if already connected
{
return;
}

Serial.println("Connecting to Server");

while((ret = mqtt.connect()) != 0) // attempt to connect
{
Serial.print(" Error - ");
Serial.println(mqtt.connectErrorString(ret));
Serial.println(" Attempting Reconnection in 5 seconds");

mqtt.disconnect();
delay(5000);
}

Serial.println(" Success - Connection Established");
Serial.println("Scan Card");
Serial.println("");
}

void setup()
{
Bridge.begin();
Console.begin();
SPI.begin();

Serial.begin(9600);

mfrc522.PCD_Init();
delay(500);
mqtt.subscribe(&subscribeA); // start subscription
}

void loop()
{
MQTTConnect(); // connect to service

Adafruit_MQTT_Subscribe *subscription;

if(!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if(!mfrc522.PICC_ReadCardSerial())
{
return;
}

String content = ""; // string to store card id

// store card id
for(byte i = 0; i < mfrc522.uid.size; i++)
{
// store the card id number in a variable
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}

Serial.println(content.substring(1));

if(content.substring(1) == "fa 33 ba 2e")
{
valueToSend1++; // increase value to trigger write
sendEmployee1();
}

else if(content.substring(1) == "fa 75 be 2e")
{
valueToSend2++; // increase value to trigger write
sendEmployee2();
}

else if(content.substring(1) == "16 6c 3e 12")
{
valueToSend3++; // increase value to trigger write
sendEmployee3();
}

else
{
Serial.println("Error - The Card Does not Match UIDs Saved");
}

if(! mqtt.ping()) // ping to keep server connection alive
{
Serial.println("Error - Ping Failed");
}

delay(500);
}
Last edited by schmidt_be on Wed Jun 13, 2018 12:53 am, edited 1 time in total.

schmidt_be
 
Posts: 3
Joined: Tue Jun 12, 2018 11:30 pm

Re: Pushing data from Arduino to Adafruit IO feeds.

by franklin97355 on Wed Jun 13, 2018 12:27 am

Post your code so we can take a look.

franklin97355
 
Posts: 18801
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Pushing data from Arduino to Adafruit IO feeds.

by schmidt_be on Wed Jun 13, 2018 12:52 am

Code: Select all | TOGGLE FULL SIZE
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "" // TODO - insert username
#define AIO_KEY         ""  // TODO - insert app key

#include <Bridge.h>
#include <Console.h>
#include <BridgeClient.h>
#include <SPI.h>
#include <MFRC522.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define SS_PIN 10
#define RST_PIN 5

// values to send to different Adafruit IO Feeds
  // initialise as many as you need (same number as Feeds below)
uint32_t valueToSend1;
uint32_t valueToSend2;
uint32_t valueToSend3;

// Note - The Update encourages the user to insert the Card's UIDs directly in the loop
  // please locate the TODO in the loop
 
BridgeClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Initialise Feeds - You can initialise as many feeds as you want depending on amount of employees
  // make sure to create these feeds on Adafruit IO - only 1 subscription required
Adafruit_MQTT_Publish sendToDropbox1 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox1");
Adafruit_MQTT_Publish sendToDropbox2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox2");
Adafruit_MQTT_Publish sendToDropbox3 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/sendToDropbox3");
Adafruit_MQTT_Subscribe subscribeA = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/subscribeA");

// now we will create loops that publish to a certain feed depending on card
  // create as many loops as the amount of employees you want to monitor
void sendEmployee1()
{
  Serial.print("Publishing ");
  Serial.println(valueToSend1);
  Console.println(valueToSend1);
 
  if(!sendToDropbox1.publish(valueToSend1))
  {
    Serial.println(F("  Error - Failed to Send Data"));
  }
  else
  {
    Serial.println(F("  Success - Data Sent"));
  }
}

void sendEmployee2()
{
  Serial.print("Publishing ");
  Serial.println(valueToSend2);
  Console.println(valueToSend2);
 
  if(!sendToDropbox2.publish(valueToSend2))
  {
    Serial.println(F("  Error - Failed to Send Data"));
  }
  else
  {
    Serial.println(F("  Success - Data Sent"));
  }
}

void sendEmployee3()
{
  Serial.print("Publishing ");
  Serial.println(valueToSend3);
  Console.println(valueToSend3);
 
  if(!sendToDropbox3.publish(valueToSend3))
  {
    Serial.println(F("  Error - Failed to Send Data"));
  }
  else
  {
    Serial.println(F("  Success - Data Sent"));
  }
}



void MQTTConnect()
{
  int8_t ret;

  if(mqtt.connected()) // if already connected
  {
    return;
  }
 
  Serial.println("Connecting to Server");

  while((ret = mqtt.connect()) != 0) // attempt to connect
  {
    Serial.print("  Error - ");
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("  Attempting Reconnection in 5 seconds");
   
    mqtt.disconnect();
    delay(5000);
  }

  Serial.println("  Success - Connection Established");
  Serial.println("Scan Card");
  Serial.println("");
}

void setup()
{
  Bridge.begin();
  Console.begin();
  SPI.begin();
 
  Serial.begin(9600);
 
  mfrc522.PCD_Init();
  delay(500);
  mqtt.subscribe(&subscribeA); // start subscription
}

void loop()
{
  MQTTConnect(); // connect to service

  Adafruit_MQTT_Subscribe *subscription;

  if(!mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if(!mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  String content = ""; // string to store card id

  // store card id
  for(byte i = 0; i < mfrc522.uid.size; i++)
  {
    // store the card id number in a variable
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
 
  Serial.println(content.substring(1));
 
  if(content.substring(1) == "fa 33 ba 2e")
  {
    valueToSend1++; // increase value to trigger write
    sendEmployee1();
  }
 
  else if(content.substring(1) == "fa 75 be 2e")
  {
    valueToSend2++; // increase value to trigger write
    sendEmployee2();
  }
 
  else if(content.substring(1) == "16 6c 3e 12")
  {
    valueToSend3++; // increase value to trigger write
    sendEmployee3();
  }
 
  else
  {
    Serial.println("Error - The Card Does not Match UIDs Saved");
  }
 
  if(! mqtt.ping()) // ping to keep server connection alive
  {
    Serial.println("Error - Ping Failed");
  }

  delay(500);
}
Last edited by franklin97355 on Wed Jun 13, 2018 1:28 am, edited 1 time in total.
Reason: Please use code tags when posting code or logs to the forums. It preserves formatting and makes it easier for everyone to read the code. Click the code button above the reply box and past your code between the tags created.

schmidt_be
 
Posts: 3
Joined: Tue Jun 12, 2018 11:30 pm

Re: Pushing data from Arduino to Adafruit IO feeds.

by schmidt_be on Thu Jun 14, 2018 8:17 am

I think I found the problem. Connected my Yun to my personal WiFi (instead of work) and data is sending to Adafruit IO every time.

I now have the problem that my IFTTT widgets/triggers aren’t all checking the relevant Adafruit IO feeds. If I scan three cards, only one makes its way to the Dropbox text file. The other two IO feeds remain unchecked and don’t make it. Will keep working on it. Cheers.

schmidt_be
 
Posts: 3
Joined: Tue Jun 12, 2018 11:30 pm

Please be positive and constructive with your questions and comments.