0

I am trying to send data between Arduino Nano and D1 Mini (ESP 8266 Module)

My Code as below in Nano which will be the transmeting ,

#include <DES.h>
#include <SoftwareSerial.h>
SoftwareSerial link(2, 3); // Rx, Tx
DES des;
byte in[8];
String  input;
char text[20];
char charVal[6];


char buf[30];
void setup() {
  link.begin(9600);
  Serial.begin(9600);
  Serial.println("Hello! Pleace Enter Your Data to Encrypt");

}

void tdesTest() {
  byte out[8];
  byte key[] = {
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
    0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
  };

  Serial.println();
  Serial.println("====== Triple-DES test ======");

  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  /*
    //decrypt
    for (int i = 0; i < 8; i++)
    {
      in[i] = out[i];
    }
    Serial.print("Decrypt...");
    time = micros();
    des.tripleDecrypt(out, in, key);
    time = micros() - time;
    Serial.print("done. (");
    Serial.print(time);
    Serial.println(" micros)");
    printArray(out);
    delay(2000);
  */
}

//printing output
void printArray(byte output[])
{
  for (int i = 0; i < 8; i++)
  {
    if (output[i] < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(output[i], HEX);
    Serial.print(" ");
    delay(100);

  }
  Serial.println();

}

void loop() {

  while (Serial.available() > 0) {

    String  input = Serial.readString(); // read the incoming data as string
    memset(in, 0, 9); // Erase the contents of in[]
    input.toCharArray((char *)in, 9); // Copy up to 8 bytes from the string
    tdesTest();


  }
}

In printArray(byte output[])I am getting Output as HEX Byte as below

enter image description here

I am trying to send the this encrypted data to from Arduino nano to D1 Mini,

I have wrote a small Receiver Application in D1 Mini and It works , Receiver Code:

//Receiver code
#include <SoftwareSerial.h>
SoftwareSerial link(4, 0); // Rx, Tx

byte greenLED = 13;
char cString[2000];
byte chPos = 0;
byte ch = 0;
char dataStr[6];

void setup()
{
  link.begin(9600); //setup software serial
  Serial.begin(9600);    //setup serial monitor
  pinMode(greenLED, OUTPUT);
}

void loop()
{

  while (link.available())
  {
    ESP.wdtDisable();


    //read incoming char by char:
    ch = link.read();
    cString[chPos] = ch;
    chPos++;

    digitalWrite(greenLED, HIGH); //flash led to show data is arriving
    delay(20);
    digitalWrite(greenLED, LOW);

  }

  cString[chPos] = 0; //terminate cString
  chPos = 0;

  Serial.print(cString);
  ESP.wdtEnable(1);
  Serial.flush();

}

I want to pass this HEX data Generated to D1 Mini from Arduino Nano,

I have wrote test code in Arduino Nano for Transmitter(Nano) and between Receiver (D1 Mini) it works,

//Transmitter Code for Testing Connections
#include <SoftwareSerial.h>
SoftwareSerial link(2, 3); // Rx, Tx
byte greenLED = 12;
char text[20] ;
char charVal[6];

void setup()
{
  link.begin(9600);
  Serial.begin(9600);

}

void loop()
{
  strcat(text, "Message"); //append to empty string
  Serial.println(text); //print to local screen for debug*
  link.println(text);

  delay(10000);
}

Please help me to send this HEX data in single shot to D1 Mini. enter image description here

1 Answer 1

1

You can change your printArray function to work on provided Print instance:

void printArray(byte output[], Print& serial)
{
  for (int i = 0; i < 8; i++)
  {
    if (output[i] < 0x10)
    {
      serial.print("0");
    }
    serial.print(output[i], HEX);
    serial.print(" ");
  }
  serial.println();

}

then you can use it as printArray(out, Serial); or printArray(out, link);.

15
  • 1
    Thanks for the comment, I have applied your solution like this: link and getting this error link Commented Jul 3, 2021 at 15:31
  • 1
    Please help me to correct it if it's a wrong way I am using Commented Jul 3, 2021 at 15:32
  • 1
    Could you elaborate please ? Commented Jul 3, 2021 at 15:42
  • 1
    Could you show it in the code ? Commented Jul 3, 2021 at 15:48
  • 1
    byte is char, char is byte. you can do link.write(out, 8); Commented Jul 3, 2021 at 16:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.