WINC1500 & MQTT intermittent posting

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
baynesstua
 
Posts: 3
Joined: Thu Mar 22, 2018 7:01 pm

WINC1500 & MQTT intermittent posting

Post by baynesstua »

Hi there,

I have been trying to get a Mega2560 with a WINC1500 WiFi shield, PN532 NFC shield, and MPR121 capacitive breakout to post the readings from the MPR121 to my AIO feeds. I was hoping someone may be able to check the sketch and see what is going on.

Currently it will post rarely (maybe every 3rd run) and often just posts a group without the feed or posts a feed under the test group.

I am hoping to eventually achieve a system that will record the ID of the RFID card and create a group with this ID followed by a posting of several feeds under this group.

Any help will be greatly appreciated!


#include <SPI.h>
#include <WiFi101.h>
#include <Wire.h>
#include <Adafruit_PN532.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include "Adafruit_MPR121.h"

void(* resetFunc) (void) = 0;

/************************* WiFI Setup *****************************/
char ssid[] = "AndroidAP"; // your network SSID (name)
char pass[] = "nwhm4403"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "baynesstua"
#define AIO_KEY "923f225110ef422ebc79af0203121f41"


/************ Global State (you don't need to change this!) ******************/

//Set up the wifi client
WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }

/****************************Capacitive**************************************/
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
unsigned long time;
String left;
String right;
int finalise=0;

/******************************NFC/RFID**************************************/
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield

uint8_t success;
uint32_t cardid = 0; // cardId
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)


Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

/*************************** Sketch Code ************************************/

#define LEDPIN 13

void setup() {

while (!Serial);
Serial.begin(115200);

// Initialise the Client
Serial.print(F("\nInit the WiFi module..."));

// check for the presence of the breakout WINC1500
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WINC1500 not present");
resetFunc();
// don't continue:
while (true);
}
Serial.println("WINC1500 DETECTED and OK.");
pinMode(LEDPIN, OUTPUT);


/********************Check for PN532 chip*****************************************/
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (!versiondata){
Serial.print("Didn't find PN53 board");
while (1); //halt
}

Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print ("Firmware ver. "); Serial.println((versiondata>>16) & 0xFF, DEC);

// Configure board to read RFID tags
nfc.SAMConfig();

Serial.println("Waiting for an ISO14443A Card...");

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHexChar(uid, uidLength);
Serial.println("");

if (uidLength == 4)
{

Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
Serial.println("Trying to authenticate block 4 with default KEYA value");
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);

// Read the card ID
cardid = uid[0];
cardid <<= 8;
cardid |= uid[1];
cardid <<= 8;
cardid |= uid[2];
cardid <<= 8;
cardid |= uid[3];
Serial.print("Seems to be a Mifare Classic card 11111111111 #");
Serial.println(cardid);

if (success)
{
Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
uint8_t data[16];
}
}
}

/***********************Check for Capacitive Touch Sensor************************/
// Default address is 0x5A, if tied to 3.3V its 0x5B
if (!cap.begin(0x5A)){
Serial.println ("MPR121 Capacitive Sensor not found.");
while (1);
}
Serial.println("MPR121 Capacitive Sensor found!");
}

void loop() {
delay(10);
time = millis();

// Check for capacitive touch readings
currtouched = cap.touched();

for (uint8_t i=0; i<12; i++) {

if ((currtouched & _BV(i)) && !(lasttouched & _BV(i))) {
Serial.print(i); Serial.print(" touched at "); Serial.println(time);
if (i == 5){
left = left + " " + time;
}

else if (i == 7){
right = right + " " + time;
}
}

if (!(currtouched & _BV(i)) && (lasttouched & _BV(i))) {
Serial.print(i); Serial.print(" released at "); Serial.println(time);

if (i == 5){
left = left + " " + time;

}

else if (i == 11){
right = right + " " + time;

}
}

if(time > 30000 && finalise == 0){
finalise++;
MQTT_publish();

//reset

}
}
lasttouched = currtouched;
}


/********************************FUNCTIONS**************************************************/

void MQTT_publish(){

MQTT_connect();

int cardInt = (int)cardid;

String basePathFeed = "baynesstua/feeds/";
String fullPathStringLeft = basePathFeed + "ID" + cardInt + ".seat-touchleft";
int pathLeft = fullPathStringLeft.length() + 1;
char pathCharLeft[pathLeft];
fullPathStringLeft.toCharArray(pathCharLeft, pathLeft);
Adafruit_MQTT_Publish capacitiveTouchLeft = Adafruit_MQTT_Publish(&mqtt, pathCharLeft);
int leftLen = (left.length() + 1);
char leftChar[leftLen];
left.toCharArray(leftChar, leftLen);
capacitiveTouchLeft.publish(leftChar);
}


void MQTT_connect() {
int8_t ret;

// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}

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

Serial.print("Connecting to MQTT... ");

while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}


Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”