GithubHelp home page GithubHelp logo

willie84 / raspberry-pi-mqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from opensensorsio/raspberry-pi-mqtt

0.0 1.0 0.0 6 KB

A raspberry pi interface for mqtt & OSIO

C++ 98.52% Makefile 1.48%

raspberry-pi-mqtt's Introduction

raspberry-pi-mqtt

This is an OpenSensors client for Raspberry PI board.

Hardware prerequisites

You need a Raspberry PI board to start using this library.

Software prerequisites

First you need to install operating system on your Raspbery PI board. We have tested our library on Raspbian. You can install any other system what you want. There is nice instalation guide. Secondly you should keep in mind that you must build your application on operating system installed on your Raspberry. You should do so because lined C/C++ program depends on processor architecture. So to use program on Ubuntu 14.04 (for example) link it on Ubuntu. To run it on Raspbian link it on Raspbian. In our library we use mosquitto MQTT client library. Therefore to link your future program you need install libmosquitto0-dev first (example listed below working for Ubuntu):

apt-get install libmosquitto0-dev

This will install necessary binaries and mosquitto.h files on your system.

Getting Started

To use library in your first client program include raspberry_osio_client.h in your program:

#include "raspberry_osio_client.h"

Library reference

This client library realized as C++ class named RaspberryOSIOClient. There are such public methods to work with it:

Constructors which initialize necessary connection parameters. There are some overrides of it ("server name" and "callback" parameters are not mandatory):

  RaspberryOSIOClient(char * userName, char * deviceId, char * devicePassword); 
  RaspberryOSIOClient(char * userName, char * deviceId, char * devicePassword, char * serverName);
  RaspberryOSIOClient(char * userName, char * deviceId, char * devicePassword, void (*callback)(char*,char*,unsigned int));
  RaspberryOSIOClient(char * userName, char * deviceId, char * devicePassword, char * serverName, void (*callback)(char*,char*,unsigned int));

Method to process iteration of receiving data (callback supplied in constructor (if necessary)):

int RaspberryOSIOClient::loop();

Method to publish message to topic:

int RaspberryOSIOClient::publish(char* topic, char* payload);

Method to subscribe for topic:

int RaspberryOSIOClient::subscribe(char* topic);

Subscribe to a Message

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

using namespace std;

RaspberryOSIOClient * client = 0;


/*
 * Handler for incoming messages.
 */	
void onMessage(char* topic, char* payload, unsigned int length) 
{
  char* clearMessage = new char[length + 1];
  memset(clearMessage, 0, length + 1);
  memcpy(clearMessage, payload, length);
  
  cout << "Topic: " << topic << ", message: " << clearMessage;

  // Break communication cycle when receive "exit".
  if (strcmp(clearMessage, "exit") == 0)
  {
    client->disconnect();
  }
}

int main() 
{
  // Our raspberry MQTT client instance.
  client = new RaspberryOSIOClient("username", "deviceod", "7E4fHOQJ", onMessage);

  cout << "Client started. When \"exit\" message is received, the program will finish its work." << endl;

  // Subscribe for topic.
  int result = client->subscribe("/users/username/test");

  cout << "Subscribing result: " << (result == OSIO_ERROR_SUCCESS ? "success" : "error") << endl;

  // Main communication loop to process messages.
  do
  {
    // Save loop iteration state (OSIO_ERROR_SUCCESS if all ok).
    result = client->loop();
    // Just show that we are alive.
    cout << ".\r\n";
    // Wait 1 second.
    sleep(1);
  }
  while(result == OSIO_ERROR_SUCCESS); // Break if loop not returned OSIO_ERROR_SUCCESS.

  delete client;
  return 0;
}

Publish a Message

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

using namespace std;

RaspberryOSIOClient * client = 0;


int main() 
{
  // Our raspberry MQTT client instance.
  client = new RaspberryOSIOClient("username", "deviceid", "7s4ZHOQJ");

  cout << "Client started." << endl;
  cout << "Publishing \"hi!\" message: ";

  result = client->publish("/users/username/test", "hi!");

  cout << (result == OSIO_ERROR_SUCCESS ? "success" : "error") << endl;

  delete client;
  return 0;
}

Run example

You can build sample program using makefile and run it on your operating system. This sample combines publishing and sunscribing to topic.

raspberry-pi-mqtt's People

Contributors

andrey-barkanov avatar yods avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.