Isaac Martinez
Published © MIT

robotMowerDoor

Encountering a problem in my landscaping business, I needed to help the robot mowers get to the front yard, so I created them a doggy door.

IntermediateFull instructions providedOver 1 day109
robotMowerDoor

Things used in this project

Hardware components

Photon
Particle Photon
×1
DFRobot NFC Module V2.0
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1
Linear Solenoid, 3 VDC
Linear Solenoid, 3 VDC
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1

Software apps and online services

VS Code
Microsoft VS Code

Hand tools and fabrication machines

wood shop
3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

Robot Mower Door

fritzing schematic

Code

Robot Mower Door

C/C++
this code controls the functionality of the robot door.
/* 
 * Project: Robot Mower Door
 * Author: Isaac Martinez
 * Date: 4-11-2024
 */

#include "Particle.h"
#include <DFRobot_PN532.h>
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h"
#include "Adafruit_MQTT/Adafruit_MQTT.h"
#include "credentials.h"

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

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY); 

/****************************** Feeds ***************************************/ 
// Setup Feeds to publish or subscribe 
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> 

Adafruit_MQTT_Subscribe subFeed = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/door-status"); 
Adafruit_MQTT_Publish pubFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/door-status");
Adafruit_MQTT_Subscribe _subFeed = Adafruit_MQTT_Subscribe(&mqtt,AIO_USERNAME "/feeds/reset-button");
Adafruit_MQTT_Publish _pubFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/reset-button");

//************Declare Variables*************//
unsigned int last, lastTime;
float subValue,pubValue;
int randNumber;
int onandoffbutton;
/************Declare Functions*************/
void MQTT_connect();
bool MQTT_ping();



#define BLOCK_SIZE       16
#define  PN532_IRQ        2
#define  INTERRUPT        1
#define  POLLING          0
#define  READ_BLOCK_NO    2
int speakerPin =          D16; // The pin connected to the speaker
int lockPin =         D8; // The pin connected to the lock
int RST_PIN =         D11; // The pin connected to the RST pin

DFRobot_PN532_IIC  nfc(PN532_IRQ, POLLING);
uint8_t dataRead[16] = {0};

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
  pinMode (D7, OUTPUT);
  Serial.begin(9600);
  delay(2000);
  Serial.print("Initializing");
  while (!nfc.begin()) {
    Serial.print(".");
    delay (1000);
  }
  Serial.println();
  Serial.println("Waiting for a card......");

  pinMode(speakerPin, OUTPUT); // Set the speaker pin as output
  pinMode(lockPin, OUTPUT); // Set the lock pin as output
  pinMode(D11, OUTPUT); // Set the RST pin as output

    // Connect to Internet but not Particle Cloud
  WiFi.on();
  WiFi.connect();
  while(WiFi.connecting()) {
    Serial.printf(".");
  }
  Serial.printf("\n\n");

  // Setup MQTT subscription
  mqtt.subscribe(&subFeed);

}

void loop() {
  MQTT_connect();
  MQTT_ping();
    // this is our 'wait for incoming subscription packets' busy subloop 
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(100))) {
    if (subscription == &subFeed) {
      subValue = atoi((char *)subFeed.lastread);
    }
    if(subValue == HIGH){
     digitalWrite (D7,HIGH);
     }
      else{
        digitalWrite(D7,LOW);
        }  
     
    }
  if((millis()-lastTime > 6000)) {
    if(mqtt.Update()) {
      randNumber = random(300);
      Serial.printf("The number is = %i \n",randNumber);
      pubFeed.publish(randNumber);
      Serial.printf("Publishing %0.2f \n",randNumber); 
      } 
    lastTime = millis();
  }


  if (nfc.scan()) {
    if (nfc.readData(dataRead, READ_BLOCK_NO) != 1) {
      Serial.print("Block ");
      Serial.print(READ_BLOCK_NO);
      Serial.println(" read failure!");
    }
    else {
      Serial.print("Block ");
      Serial.print(READ_BLOCK_NO);
      Serial.println(" read success!");

      Serial.print("Data read(string):");
      Serial.println((char *)dataRead);

      // If the read data is "Hello World", play a sound and unlock the lock
      if (strcmp((char *)dataRead, "Hello World !") == 0) {
        Serial.println("Playing tone");
        analogWrite(speakerPin, 128, 500); // Play a 20Hz tone
        digitalWrite(lockPin, HIGH); // Unlock the lock
        delay(8000);
        analogWrite(speakerPin, 0, 500); // Stop the tone
        delay (10000);
        digitalWrite(lockPin, LOW); // Lock the lock
      }

      Serial.print("Data read(HEX):");
      for (int i = 0; i < BLOCK_SIZE; i++) {
        Serial.print(dataRead[i], HEX);
        Serial.print(" ");
        dataRead[i] = 0;
      }
      Serial.println();
    }
    delay(500);
  }
}

void MQTT_connect() {
  int8_t ret;
 
  // Return if already connected.
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.printf("Error Code %s\n",mqtt.connectErrorString(ret));
       Serial.printf("Retrying MQTT connection in 5 seconds...\n");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds and try again
  }
  Serial.printf("MQTT Connected!\n");
}

bool MQTT_ping() {
  static unsigned int last;
  bool pingStatus;

  if ((millis()-last)>120000) {
      Serial.printf("Pinging MQTT \n");
      pingStatus = mqtt.ping();
      if(!pingStatus) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      last = millis();
  }
  return pingStatus;
}

Credits

Isaac Martinez

Isaac Martinez

3 projects • 8 followers

Comments