//*************************************************************************
//** created 2014                                                        **
//** by dataino www.dataino.it                                           **
//** modified 18 Aug 2014                                                **
//** by G. Carmelo                                                       **
//*************************************************************************
// Dataino Serial version 001
// Copyright (c) 2015 Carmelo G.
// If you require a license, see
//     http://www.opensource.org/licenses/bsd-license.php
//*************************************************************************
//**                               LIBRARY                               **
//*************************************************************************

//*************************************************************************
//**               INCOMING OUTGOING GAUGE VARIABLE SETTING              **
//*************************************************************************
bool newDataLoRa = false;         // New data on LoRa
int RXi = 0;                      // Char count read in LoRa
long int sendLiveTime = 60000;    // Alive time
long int liveTime = millis();     // Start alive timer
uint8_t el = { 0x0A };            // end of Line char
const int MAX_STR_LEN = 128;      // the max string lenght
char strBufRX[MAX_STR_LEN];       // static buffer for computation serial RX
char stringBuffer[MAX_STR_LEN];   // static buffer for computation
char Live[10]      = "$L;#"; // Live message
char GetLive[2]    = "L";         // Get Value Command
char GetValues[2]  = "G";         // Get Value Command
char SetVar[2]     = "S";         // Set Value Command
char StartSend[2]  = "$";         // Transmission header
char EndSend[2]    = "#";         // Transmission footer
char IOT[6]        = "";     // IoT code
char authkey[41]   = "";// Auth key
unsigned long Time = millis();    //
boolean VAR_IN = false;           // switch fon incoming variable
boolean VAL_IN = false;           // switch fon incoming value
int valId = 0;                    // Variable ID
char *dtostrf(double val, signed char width, unsigned char prec, char *s);
//*************************************************************************
//**               INCOMING OUTGOING GAUGE VARIABLE SETTING              **
//*************************************************************************

//*************************************************************************
//**                                SETUP                                **
//*************************************************************************
void setup() {
  //***********************************************************************
  //**                           log serial                              **
  //***********************************************************************
  // Open serial communications and wait for port to open:
  // set the data rate for the SoftwareSerial port
  Serial.begin( 57600 );
  delay(1000);
  //***********************************************************************
  //**                        lora modem serial                          **
  //***********************************************************************
  // Open serial communications and wait for port to open:
  // set the data rate for the SoftwareSerial port
  Serial1.begin( 9600 );
  delay(1000);
  String msg = obfuscate_message( Live, 99 , 1 );
  Serial1.println( msg );
  Serial.println( F("Live") );
}
//*************************************************************************
//**                                  LOOP                               **
//*************************************************************************
void loop() {
  //***********************************************************************
  //**                          RX on Lora Modem                         **
  //***********************************************************************
  serialEvent1(); //call LoRa serial event
 
  //***********************************************************************
  //**                         New data on Lora Modem  ?                 **
  //***********************************************************************
  if ( newDataLoRa == true ) {
    
    int rxP = RXi;
    
    newDataLoRa = false;
    
    RXi = 0;
    
    int sec = (strBufRX[rxP - 2] - 48) * 10 + (strBufRX[rxP - 1] - 48) * 1;
    
    char tempCs[ rxP - 2 ];

    for ( int i = 0 ; i < rxP - 2 ; ++i ) {
      tempCs[i] = strBufRX[ i ];
    }

    String msg = obfuscate_message( tempCs, sec , -1 );

    char temp[rxP - 2];
    msg.toCharArray(  temp,  rxP - 1 );

    if ( temp[ 0 ] == '$' && temp[ rxP - 3 ] == '#' ) {

      char message[ rxP - 7 ];

      int i = 0;
      boolean messageForMe = true ;
      // *****************************************************************
      i = 3;

      for ( i ; i < 8 ; ++i ) {
        if ( temp[ i ] != IOT[ i - 3 ] ) {
          messageForMe = false;
        }
      }

      // *****************************************************************
      if ( messageForMe == true ) {

        i = 0;

        for ( i ; i < 2 ; ++i ) {
          message[i] = temp[ i + 1 ];
        }

        i = 9;

        for ( i ; i < rxP - 2 ; ++i ) {
          message[ i - 7 ] = temp[ i ];
        }

        message[ i - 7 ] = '\0';

        split_message( message );

      }
    }
  }
  //***********************************************************************
  //**                          Live Message                             **
  //***********************************************************************
  if ( millis() - liveTime > sendLiveTime ) {
    liveTime = millis();
    String msg = obfuscate_message( Live, 99 , 1 );
    Serial1.println( msg );
    Serial.println( F("Live") );
  }

  //***********************************************************************
  //**                              Time Set                             **
  //***********************************************************************

  Time = millis();

}
//***********************************************************************
//**                          RX on Lora Modem                         **
//***********************************************************************
void serialEvent1() {
  while (Serial1.available()) {

    char c = Serial1.read();

    if ( c == el ) {
      newDataLoRa = true;
      strBufRX[RXi] = '\0';         // Null terminate the string
      break;
    } else {
      strBufRX[RXi] = c;
      RXi++;
    }
  }
}
//*************************************************************************
//**                  What did I get?, A command, a value?               **
//*************************************************************************
//**                   SPLIT MESSAGE RX FUNCTION                         **
//*************************************************************************
void split_message( char* string )
{
  Serial.print("Split:");
  Serial.println( string );

  char *str;
  char *p;
  strncpy(stringBuffer, string, MAX_STR_LEN);      // copy source string

  //  ***********************************************************************
  //  **split using comma loop while str is not null get subsequent tokens **
  //  ***********************************************************************
  for (str = strtok_r(stringBuffer, ";", &p); str; str = strtok_r(NULL, ";", &p))
  {

    //  *********************************************************************
    //  **                  Request if sistem is Online                    **
    //  *********************************************************************

    if ( Comp( stringBuffer, GetLive ) == 0 ) {
      String msg = obfuscate_message( Live, 99 , 1 );
      Serial1.println( msg );
      Serial.println( "Live!" );
    }

    //***********************************************************************
    //**  Get all Value ( Synchronize the web interface with the system )  **
    //***********************************************************************
    if ( Comp( stringBuffer, GetValues ) == 0 ) {
 
    }
    //***********************************************************************
    //**                             End trasmission                       **
    //***********************************************************************
    if ( Comp(str, EndSend) == 0 )
    {
      VAL_IN = false;
      VAR_IN = false;
    }
    //***********************************************************************
    //**                             Value on Recive                       **
    //***********************************************************************
    if ( VAR_IN == false && VAL_IN == true )
    {
      switch ( valId ) {

        default:
          Serial.println(F("Variable UNKNOW"));
      }
    }
    //********************************************************************
    //**                       Recive id Variable                       **
    //********************************************************************
    if ( VAR_IN == true && VAL_IN == false )
    {
      valId  = atoi(str);
      VAR_IN = false;
      VAL_IN = true;
    }

    //********************************************************************
    //**                    Variable Name do not exist                  **
    //********************************************************************
    if ( VAR_IN == true && VAL_IN == false )
    {
      valId =  0;
      VAR_IN = false;
      VAL_IN = true;
    }
    //********************************************************************
    //**                         Variable On Recive                     **
    //********************************************************************
    if ( Comp(str, SetVar) == 0 && VAR_IN == false && VAL_IN == false ) {
      VAR_IN = true;
      VAL_IN = false;
    } //END if
  } // END for
}
//*************************************************************************
//**                            Compare Value                            **
//*************************************************************************
char Comp( char* inData, char* This )
{

  if ( strcmp( inData, This )  == 0 ) {
    return (0);
  } else {
    return (1);
  }

}
//*************************************************************************
//**                      obfuscate transmission                         **
//** string: the string to obfuscate or deobfuscate                      **
//** second: second time                                                 **
//** sign:   1 Obfuscate , -1 Deobfuscate                                **
//*************************************************************************
String obfuscate_message( String string , int second , int sign ) {
  int shift = second;
  unsigned int stringLen = string.length();
  unsigned int diffLen = 0;
  unsigned int endFor = 0;
  unsigned int i = 0;
  char sec[3];

  if ( second == 99 )  {
    diffLen = 3 ;
    endFor  = 1 ;
    char msec[10];
    sprintf( msec, "%010d" , millis() );
    int x =  ( msec[ 8 ] - 48) * 10 + (msec[ 9 ] - 48) * 1;
    second = map(x, 0, 99, 0, 40);
    shift = int( second );
    sprintf( sec, "%02i" , second );

  } else {
    diffLen = 0 ;
    endFor  = 1 ;
  }

  char obfscateMsg[ stringLen + diffLen ];

  for ( i ; i <= stringLen - endFor ; i++ ) {

    unsigned int pointer = i + shift;

    if ( pointer > 39 ) {
      pointer = pointer - 40;
    }

    if ( pointer > 78 ) {
      pointer = pointer - 79;
    }

    obfscateMsg[i] = char( int( string[i] ) + ( int(authkey[pointer]) * sign ) );

  }

  if ( sign == 1 )  {

    obfscateMsg[ stringLen ]    = sec[ 0 ];
    obfscateMsg[ stringLen + 1] = sec[ 1 ];

    Serial.print( F("[+] TX - ") );
    Serial.println( string );
  } else {

    Serial.print( F("[+] RX - ") );
    Serial.println( obfscateMsg );
  }
  return obfscateMsg;
}
//*************************************************************************
//**                       SEND VALUE TO MONITOR                         **
//*************************************************************************
void SendValue( int SendVar )
{
  delay( 1000 );

  char sendTemp[ MAX_STR_LEN ];
  char sendVal[ 64 ];

  switch ( SendVar )
  {
 
  }

  sprintf( sendTemp, "%sV;%s;%i;%s%s", StartSend, IOT, SendVar, sendVal, EndSend );

  String msg = obfuscate_message( sendTemp, 99 , 1 );
  Serial1.println( msg );
  
}

X

Login




X

Register

captcha

X

Password Reset