Ahmed Rafiq
Published © GPL3+

Packet QR to MQTT Client

QR-Code scanned at door for remote MQTT-client.

IntermediateFull instructions provided5 hours1,827
Packet QR to MQTT Client

Things used in this project

Hardware components

Raspberry Pi 1 Model B
Raspberry Pi 1 Model B
×1
Logitech c170
×1

Software apps and online services

OpenCV
OpenCV

Story

Read more

Code

qrcam.cpp

C/C++
qr-codeAtDoor
/** @file qrcam.cpp
* Reading QR code using webcam
* and printing at terminal
* only once.
*/

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <zbar.h>

#include "qrmosq.h"

using namespace cv;
using namespace std;
using namespace zbar;

//int main(void)
string qrMqtt::qrcam()
{
	string qrdata;
	VideoCapture cap(0);

	if (!cap.isOpened()){
		cerr << "Cannot open Webcam\n";
		//return -1;
		throw "#FAILED";
	}

	ImageScanner scanner;				// zbar::ImageScanner
	scanner.set_config (ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

	while (1){
	Mat frame;							// Mat - opencv n-dimension array class.
	bool bSuccess = cap.read (frame);
	if (!bSuccess){
	  cout << "cannot read a frame video\n";
	  break;
	}

	Mat gray;
	cvtColor (frame, gray, CV_BGR2GRAY);	// convert frame array to grey 
											// as transform RGB space to GRAY.
	int width = frame.cols;
	int height = frame.rows;
	uchar *raw = (uchar *)gray.data;

	Image image(width, height, "Y800", raw, width * height);		// zbar::Image::Image()
																	// Y800 = 8 bit monochrome format.
																	// GRAY also accepted.

	int n = scanner.scan (image);

	for (Image::SymbolIterator symbol = image.symbol_begin();
	  symbol != image.symbol_end(); ++symbol){

	  qrdata = symbol->get_data();
	  cout << "Main Data ->" << qrdata <<endl;

	  }

	  if (!qrdata.empty()){
		break;
	  }

	  }
	return qrdata;
}

main.cpp

C/C++
qr-codeAtDoor
/**
* @file main.cpp
* call camera, call publisher to send
* msg to subscriber, waits for 5 sec, 
* and recall camera.
*/

#include <iostream>
#include <string>
#include <unistd.h>

#include "qrmosq.h"

using namespace std;

int main(void)
{
	try {

	class qrMqtt *qr2sp;
	//int rc;
	mosqpp::lib_init();

	qr2sp = new qrMqtt ("qr2sp", "pcktatDoor", "192.168.178.100", 1883);

	while (1){

		/// call camera [qrcam()], read QR and send to publish()
		qr2sp->send_msg(qr2sp->qrcam().c_str());

		rc = qr2sp->loop();

		if (rc){
			qr2sp->reconnect();
		}
/** 
* will wait 5 sec, so camera light will be turned off
* for 5 seconds. After 5 sec, camera will active again to read QR-code
*/
		usleep(5000000);	
	}
	mosqpp::lib_cleanup();
	} catch (const exception& e){					/// if exception occured in constructor. see class declaration.
		cerr << "Error on Network Connection.\n" \
					<< "Check mosquitto is running & IP/PORT\n";
	}
	return 0;
}

qrmosq.h

C/C++
qr-codeAtDoor
#ifndef QRMOSQ_H
#define QRMOSQ_H

#include <mosquittopp.h>

class qrMqtt : public mosqpp::mosquittopp
{
  private:
   const char * host;
   const char * id;
   const char * topic;
   int		port;
   int		keepalive;

   void on_connect(int rc);
   void on_disconnect();
   void on_publish(int mid);

  public:
   qrMqtt(const char *id, const char * _topic, const char *host, int port);
   ~qrMqtt();
   bool send_msg(const char *message);
   std::string qrcam();

};

#endif

qrmosq.cpp

C/C++
qr-codeAtDoor
/** @file qrmosq.cpp
* description of qrmosq.h
* and send msg to subscriber
*/

#include <iostream>
#include <cstring>
#include <stdexcept>

#include "qrmosq.h"
#include <mosquittopp.h>

using namespace std;

qrMqtt::qrMqtt(const char * _id, const char * _topic, const char * _host, int _port) : mosquittopp(_id)
{
 this->keepalive = 60;
 this->id = _id;
 this->port= _port;
 this->host = _host;
 this->topic = _topic;
 
 /**
 * error handling
 * whether broker is running ?
 */
 
 if (connect(host, port, keepalive) == MOSQ_ERR_ERRNO){
	 throw runtime_error("##ERROR##");
 }
  
 loop_start();
};

qrMqtt::~qrMqtt()
{
 loop_stop();
}

void qrMqtt::on_connect(int rc)
{
 if (rc == 0){
  cout << " ##-Connected with Broker-## " << std::endl;
  }
 else {
  cout << "##-Unable to Connect Broker-## " << std::endl;
  }
}

bool qrMqtt::send_msg (const char *message)
{
	int ret = publish (NULL, this->topic, strlen(message), message, 2, false);
	
	return (ret == MOSQ_ERR_SUCCESS);
}

void qrMqtt::on_disconnect()
{
 cout << " ##-Disconnected from Broker-## " << std::endl;
}

void qrMqtt::on_publish(int mid)
{
	cout << "## - Message published successfully" << endl;
}

Makefile

Makefile
qr-codeAtDoor
CC=g++
#ARCH=-arch armv6l

INCLUDE_FLAGS=-I/usr/local/include
CFLAGS=-Wall -ggdb -I./lib -I./lib/cpp
LDFLAGS=-L./lib ./lib/cpp/libmosquittopp.so.1 ./lib/libmosquitto.so.1 -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lzbar

.PHONY: all clean

all : qrMqtt

qrMqtt : main.o qrmosq.o qrcam.o
	${CC} $^ -o $@ ${LDFLAGS}

main.o : main.cpp
	${CC} -c $^ -o $@ ${CFLAGS}

qrmosq.o : qrmosq.cpp
	${CC} -c $^ -o $@ ${CFLAGS}

qrcam.o : qrcam.cpp
	${CC} ${ARCH} -c $^ -o $@ ${CFLAGS}
	
clean : 
	-rm -f *.o qrMqtt

Credits

Ahmed Rafiq

Ahmed Rafiq

1 project • 2 followers
From 2.4 - till now, love to talk with computer through keyboard.

Comments