Please login

code ver 1.0

//*****************************************************************************
//** by dataino www.dataino.it                                               **
//** modified 09 Sep 2016                                                    **
//** by G. Carmelo                                                           **
//*****************************************************************************
//* MQTT dataino.it Server                                                   **
//* http://mosquitto.org                                                     **
//*****************************************************************************
// Dataino Network - MQTT - version 001                                      **
// Copyright (c) 2013 Carmelo G.                                             **
// If you require a license, see                                             **
//     http://www.opensource.org/licenses/bsd-license.php                    **
//*****************************************************************************
//                                LIBRARY                                    **
//*****************************************************************************
#include <SPI.h>                  //
#include <Ethernet.h>             //
#include <PubSubClient.h>         //
#include <dataino.h>              //
//*****************************************************************************
//                            PROGRAM VAR - CONST                            **
//*****************************************************************************
#define project 0xB574D0BB           // Project iD
#define key "1234567890123456"       //  
#define IoT 0x??                     //  
#define iotName ??"                  //  
byte server[]={37, 59, 113, 1};      // MQTT ip
char userName[]="????????";          // Reserved not divulgate (your username)
char password[]="??????????";        // Reserved not divulgate (see user data)
bool mqttIsConnected = false;        // mqtt Status
//*****************************************************************************
//             Insert variable and const for your project.                   **
//*****************************************************************************

//*****************************************************************************
//                                Variable setup                             **
//*****************************************************************************
//                  bo  ch  ui8 in  u16 lo  u32 fl  txt gps xy  gam
uint8_t sizes[] = { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 };
uint8_t sizesTxt[] = { };
char const * initValues[] = { 
};
uint8_t ga[] =  };
//*****************************************************************************
//                              Gauge variable                               **
//*****************************************************************************
//*****************************************************************************
//                                callback //
//*****************************************************************************
void DATAINOcallback( char* msg , uint8_t channel , uint8_t varIn, uint8_t rqs ) {
  mainDataino( msg, channel, varIn, rqs );    // call main
}

dataino Dataino( ga, initValues, sizes, sizesTxt, IoT, project, (uint8_t*)key, DATAINOcallback );

//*****************************************************************************
//             Update these with values suitable for your network.           **
//*****************************************************************************
byte iotMac[]     = { 0x90, 0xA2, 0xDA, 0x0D, 0x82, 0x39 };// iot mac Arduino

EthernetClient ethClient;

//*****************************************************************************
//                            callback MQTT channels msg in                  **
//*****************************************************************************
void MQTTcallback(char* topic, byte* payload, unsigned int length) {
  Dataino.readMsg( topic, payload, length );
}
//*****************************************************************************
//                            initialize MQTT                                **
//*****************************************************************************
PubSubClient client( server, 1883, MQTTcallback, ethClient );
//*****************************************************************************
// **                                SETUP                                   **
//*****************************************************************************
void setup() {

  //***************************************************************************
  //                               Start Serial                              **
  //***************************************************************************
  Serial.begin( 115200 );
  Serial.println( "Ready");
  //***************************************************************************
  //                              Start Ethernet                             **
  //***************************************************************************
  Ethernet.begin( iotMac, iotIp, iotDns, iotGateway, iotSubnet );
  delay( 500 );
  Serial.println( F( "MQTT Connecting..." ) );
  //***************************************************************************
  //                      Check if MQTT is connect                           **
  //***************************************************************************
  if ( client.connect( iotName , userName, password )) {
    Serial.println( F( "MQTT OK :-] " ) );
    subscribeChannels();
    mqttIsConnected = true;
  } else {
    Serial.println( F( "MQTT ERROR" ) );
  }
}
// ****************************************************************************
// **                                LOOP                                    **
// ****************************************************************************
void loop() {
  // *************************************************************************
  // **             Check if MQTT client is ok or try to reconnect          **
  // *************************************************************************
  if (!client.loop()) {
    mqttIsConnected = false;
    Serial.println( F( "Client disconnected..." ) );
    if ( client.connect( iotName , userName, password ) ) {
      subscribeChannels();
      mqttIsConnected = true;
    } else {
      Serial.println( F( "Failed.") );
    }
  }
  
  // *************************************************************************
  // **             if MQTT client is ok                                    **
  // *************************************************************************
  if ( mqttIsConnected ) {
  }

}
// ***************************************************************************
//                                     MAIN                                 **
// ***************************************************************************
void mainDataino( char* msg , byte channel , uint8_t varIn , byte rqs )
{

  if ( varIn == CMD ) {

    switch ( Dataino.getCmd() ) {

      case CMD_ALIVE_REQUEST:
        Serial.println( F("[+] Alive request recived") );
        Dataino.setCmd( CMD_ALIVE );
        client.publish( Dataino.createDest( rqs ) , Dataino.msg( CMD ) );
        break;
      case  CMD_SEND_VALUES:
        Serial.println( F("[+] Send Config") );
        
        client.publish( Dataino.createDest( rqs ) , msg );

        if ( Dataino.configCount() > 0 ) {
          Dataino.msg( COMMAND_VALUES , rqs );
        }

        break;
      case  CMD_SEND_VALUE:
        Serial.println( F("[+] Send Value") );

        client.publish( Dataino.createDest( "WB" ) , Dataino.msg( atoi(msg) ) );

        break;
      case  CMD_GET_VALUES:
        Serial.println( F("[+] Send All Values") );
        Dataino.msg( atoi( msg ), rqs );
        break;

      case  CMD_GET_VALUE:
        Serial.println( F("[+] Send Value") );
        client.publish( Dataino.createDest( rqs ) , Dataino.msg( atoi( msg ) ) );
        break;
    }
  } else if( channel == IoT ) {
  
    Serial.println( F("[+] New value recived") );
    
    switch ( varIn ) {

          default:
          break;
      }
      } else {
        Serial.println( F("[+] New value recived other channel") );
        Serial.print( F("[+] channel:") );Serial.println( channel , HEX );
        Serial.print( F("[+] varIn  :") );Serial.println( varIn , HEX );
        Serial.print( F("[+] value  :") );Serial.println( msg );
      }
    }
// ***************************************************************************
//                            Subscribe MQTT channels                       **
// ***************************************************************************
void subscribeChannels()
{

  client.subscribe( Dataino.createDest( IoT ) );
  //client.subscribe( Dataino.createDest( 0x00 ) ); // subcribe othe IoT channell
  
  Dataino.setCmd( CMD_ALIVE );
  client.publish( Dataino.createDest( "WB" ) ,  Dataino.msg( CMD ) );
}

  • Project Name:
  • Private Code:
    B574D0BB
  • Public Code:
    076C03DF
  • Crypt key:
    1234567890123456
[-]|..:| LOG |:..|
[] [LN] X

Insert new IoT




X

Login





X

Register

captcha

X

Password Reset