Click here to Skip to main content
15,564,017 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include 
#include 
#include 

//Singleton instance of radio driver
RH_RF95 rf95;
int led = 13;   // Define LED pin in case we want to use it to demonstrate activity

//Message fields
int SEQ = 0;     // Sequence number
int TYPE = 0;   // Message type
int TAGID = 1;  // Identity of tag. Will eventually be read from an SD card.
int RELAYID = 0;
int TTL = 5;
int thisRSSI = 0;
int firstIteration = 0;   // First time through main loop send a reset (kludge, I know. Should go in setup.

int MESSAGELENGTH = 36;
int TXINTERVAL = 5000; // Time between transmissions (in ms)
double CSMATIME = 10;  // Check the status of the channel every 10 ms


void setup() 
{
 
  //String InputMessage = "hello world" ;
  Serial.begin(9600);
  Serial.println("Please enter your password : "); // hello world is the correct password for now
  // wait for user input through serial
  while(Serial.available() == 0);

  //read the input
  String InputMessage = Serial.readString();
  InputMessage.trim();// trim to remove the return character

  Serial.print("Input message : ");Serial.println (InputMessage);

  
  String Hash = ProcessInputMessage (InputMessage);
  String retrievedString = readStringFromEEPROM(0);
  
  Serial.println("The String we read from EEPROM: ");
  Serial.println(retrievedString);
   
  if (retrievedString != Hash) 
  {
    Serial.println();
    Serial.print("Warning!!! invalid password!");
    
    while( true ){} //Halt the program
  };

  Serial.print("welcome to the programme ");
  
  //writeStringToEEPROM(0, Hash);
  // Initialise LoRa transceiver
  pinMode(led, OUTPUT);
  while (!rf95.init()){
    Serial.println("Initialisation of LoRa receiver failed");
    delay(1000);
  }
  rf95.setFrequency(915.0);   //PB set to use 915 MHz
  rf95.setTxPower(5, false);
  rf95.setSignalBandwidth(500000);
  rf95.setSpreadingFactor(12);
}

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i  55) 
  {
    Serial.println();
    Serial.print("Warning!!! Input strings exceed 55 bytes...");
    
    while( true ){} //Halt the program
  };
  
  WordPointer=LengthOfInputString / 4;//Point to the last word which was written  // its 2 here as 11/4 = 2.75 and as int it will get 2
  NoOfWordsUsed=WordPointer + 1; // so we add +1 here to get the proper count = 3
  
  //Serial.print("WordPointer: ");Serial.println (WordPointer); 
  //Serial.print("NoOfWordsUsed: ");Serial.println (NoOfWordsUsed);   

  
  //Move input strings (ascii) into w[] word by word
  for (WordIndex=0; WordIndex3 
    for (ByteIndex=0; ByteIndex3
      w[WordIndex]=w[WordIndex] 12-2*4 = 4
    //Serial.print("ByteInWord2WriteBit1: ");Serial.println (ByteInWord2WriteBit1); 
  switch (ByteInWord2WriteBit1) { // we can have 4 cases so in this case its 4th one
    case 1:
      w[WordPointer]=w[WordPointer] | 0x80000000;
      break;
    case 2:
      w[WordPointer]=w[WordPointer] | 0x00800000;
      break;
    case 3:
      w[WordPointer]=w[WordPointer] | 0x00008000;
      break;
    case 4:
      w[WordPointer]=w[WordPointer] | 0x00000080;
      break;    
  }
   
  //testing the full array
  //for (z=0;z>7)^((w[i-15]&0x0003ffff)>18)^(w[i-15]>>3);
    s1[i]=((w[i-2] & 0x0001ffff)>17)^((w[i-2] & 0x0007ffff)>19)^(w[i-2]>>10);
    w[i]=w[i-16] + s0[i] + w[i-7] + s1[i] ; }  

  //testing the full array
  //for (z=0;z>6)^((e&0x000007ff)>11)^((e&0x01ffffff)>25);
    ch=(e & f)^((~e)& g);
    temp1=h+S1+ch+k[i]+w[i];
    S0=((a&0x00000003)>2)^((a&0x00001fff)>13)^((a&0x003fffff)>22);
    maj=(a & b)^(a & c)^(b & c);
    temp2=S0+maj;

    //Assign new hash then loop again
    h=g;
    g=f;
    f=e;
    e=d+temp1;
    d=c;
    c=b;
    b=a;
    a=temp1+temp2;
  }

  //Final hash result, add original hash with the last caculated hash 
  h0=h0+a;
  h1=h1+b;
  h2=h2+c;
  h3=h3+d;
  h4=h4+e;
  h5=h5+f;
  h6=h6+g;
  h7=h7+h;
 
 //now we change the unsigned long (32 binary values) to char first and then to string 
  
 char buf0[9]; // need 8 for chars and 1 for null
 ltoa(h0, buf0, 16); // h0 means the reading char, buf0 is the temporary holdering char, 16 means we change it to ascii.
 String hash0(buf0); // changing char to string

 char buf1[8];
 ltoa(h1, buf1, 16);
 String hash1(buf1);
  
 char buf2[8];
 ltoa(h2, buf2, 16);
 String hash2(buf2);
  
 char buf3[8];
 ltoa(h3, buf3, 16);
 String hash3(buf3);
  
 char buf4[8];
 ltoa(h4, buf4, 16);
 String hash4(buf4);
  
 char buf5[8];
 ltoa(h5, buf5, 16);
 String hash5(buf5);
  
 char buf6[8];
 ltoa(h6, buf6, 16);
 String hash6(buf6);
 
 char buf7[8];
 ltoa(h7, buf7, 16);
 String hash7(buf7);

 //next we add the strings together and that is out hash-256 output
 
 String Hash = hash0 + hash1 + hash2 + hash3 + hash4 + hash5 + hash6 + hash7;
 
 Serial.println("SHA256 HASH/DIGEST: ") ;
 
 Serial.println (Hash);

 return Hash; // sending the hash value back so we can use it to authenticate


  
  //Serial.println (h0,BIN);
  //Serial.print (h0,HEX);
  //Serial.println (h1,BIN);
  //Serial.print (h1,HEX);
  //Serial.println (h2,BIN);
  //Serial.print (h2,HEX);
  //Serial.println (h3,BIN);
  //Serial.print (h3,HEX);
  //Serial.println (h4,BIN);
  //Serial.print (h4,HEX);
  //Serial.println (h5,BIN);
  //Serial.print (h5,HEX);
  //Serial.println (h6,BIN);
  //Serial.print (h6,HEX);
  //Serial.println (h7,BIN);
  //Serial.print (h7,HEX);  
  
  }//End

  //-------------------------------------------------------
void loop() {
  // Generate message intermittently (10 seconds)
  uint8_t buf[MESSAGELENGTH];
  uint8_t len = sizeof(buf);
  char str[MESSAGELENGTH]; 
  if (firstIteration == 0) // kludge to send out a type 9 as first message. Should eventually be in setup.
    {
      TYPE = 9;
      firstIteration = 1;
    }
  else
    TYPE = 0;
    
  SEQ++;
  sprintf(str, "%5d %5d %5d %5d %5d %5d", SEQ, TYPE, TAGID, RELAYID, TTL, thisRSSI);
  for (int i=0; i < MESSAGELENGTH; i++)
    buf[i] = str[i];   
  rf95.setModeIdle(); // some obscure bug causing loss of every second message  
    
  // Channel should be idle but if not wait for it to go idle
  while (rf95.isChannelActive())
  {
    delay(CSMATIME);   // wait for channel to go idle by checking frequently
    Serial.println("Tag node looping on isChannelActive()"); //DEBUG
  }
    
  // Transmit message
  Serial.println("Transmitted message:   SEQ  TYPE  TAGID  RELAYID  TTL  RSSI"); 
  Serial.print("Transmitted message: ");  //DEBUG
  Serial.println((char*)buf);           //DEBUG
  
  rf95.send(buf, sizeof(buf));
  rf95.waitPacketSent();
  delay(TXINTERVAL);

}
//-------------------------------------------------------


What I have tried:

This is lora radio message receive program. I have combined two separate coding to run the program. But Now I need to remove unnecessary coding from this
Posted
Updated 4-Oct-22 3:48am
v2
Comments
Richard MacCutchan 4-Oct-22 6:53am    
Look at the code line by line. If the line looks like it is not necessary then remove it.
BernardIE5317 4-Oct-22 8:49am    
You might consider a genetic algorithm combined w/ AI to find the correct arrangement of code of which only a finite number exists ... so no problem. An interesting little project. More interesting than the original perhaps. Happy Coding Or you might consider joining a Coding site and asking its members to write the genetic algorithm/AI program for you.

1 solution

Grabbing chunks of code from the internet and "bolting them together" isn't development: it doesn't produce high quality code.

What it does produce is a mess that might or might not do what you want, but that will probably need some changes to meet your exact requirements.

And that means that without knowing exactly what you want to achieve with your app - and we have no idea - we can't tell you what is "necessary" and what is "unnecessary".
And since that code doesn't even appear to compile:
C++
for (int i = 0; i  55) 
  {
There isn't anything we can recommend, other than say "work out what each part is meant to do, and how it does it - then use it as a template to write your own code to do exactly what you want". That way, you will get better code with no "unnecessary coding".
 
Share this answer
 
Comments
CPallini 4-Oct-22 1:57am    
5.The OP request is really funny.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900