I am sending data from Arduino Nano to D1 Mini,
Transmitter is Arduino Nano
Transmitter Code:
#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)");
delay(10000);
printArray(out, link);
link.write(out, 9);
delay(2000);
//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);
}
//passing output and Print
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(" ");
delay(100);
}
Serial.println();
}
//printing output without link pass
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 userdata() {
Serial.println("Sending User Data");
byte user[] = {0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
printArray(user, link);
link.write(user, 9);
delay(100);
tdesTest();
}
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
userdata();
}
}
And receiving same from D1 Mini (ESP8266)
Receiver Code:
//Receiver code
#include <SoftwareSerial.h>
SoftwareSerial link(4, 0); // Rx, Tx
byte greenLED = 13;
byte cString[8];
byte chPos;
byte ch;
byte out[8];
String hexstring = "";
void setup()
{
link.begin(9600); //setup software serial
Serial.begin(9600); //setup serial monitor
pinMode(greenLED, OUTPUT);
}
void loop()
{
while (link.available() > 0)
{
ESP.wdtDisable(); // This helps th avoid not reciving data after second try
//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);
}
if (ch != 0)
{
printArray(cString);
ESP.wdtEnable(1);
Serial.flush();
cString[chPos] = 0; //terminate cString
chPos = 0;
}
}
//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);
hexstring += String(output[i], HEX);
Serial.print(" ");
delay(100);
}
Serial.println();
Serial.println("Your String Sir:");
Serial.print(hexstring);
Serial.println();
ch = 0; // Avoid printing 0s when no data is recived
hexstring = ""; //Clear the memeory of hexstring
}
In Receiver I am receiving data as below:
I want to get data in Single Stream to receiver as Example: 31 30 30 30 30 30 30 30 7E B5 2D 4D 93 CB 08 BA or in receiver join two streams in to a single stream,
On Side Note: 1st Part is Plain Data and 2nd Part will be Encrypted Data
Could anyone please help..
1 Answer 1
Managed Get it Working:
Change the Transmitter Code to Cascade Two Char Streams Before Sending:
Transmitter Code:
#include <DES.h>
#include <SoftwareSerial.h>
SoftwareSerial link(2, 3); // Rx, Tx
DES des;
byte in[8];
String input;
void setup() {
link.begin(9600);
Serial.begin(9600);
Serial.println("Hello! Pleace Enter Your Data to Encrypt");
}
//passing output and Print
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(" ");
delay(100);
}
Serial.println();
}
//printing output without link pass
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 userdata_and_Encrypt() {
Serial.println("Sending User Data");
byte username[] = {0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30};
printArray(username, link);
byte username_data = (username, 9);
delay(100);
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)");
delay(100);
printArray(out, link);
byte password_data = (out, 9);
delay(100);
byte data_stream = username_data + password_data;
printArray(data_stream, link);
link.write(out, 17);
//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(100);
}
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
userdata_and_Encrypt();
}
}
Receiver Code to Accept 16 Bytes
Receiver Code:
//Receiver code
#include <SoftwareSerial.h>
SoftwareSerial link(4, 0); // Rx, Tx
byte greenLED = 13;
byte cString[8];
byte chPos;
byte ch;
byte out[8];
String hexstring = "";
static String data = "";
void setup()
{
link.begin(9600); //setup software serial
Serial.begin(9600); //setup serial monitor
pinMode(greenLED, OUTPUT);
}
void loop()
{
while (link.available() > 0)
{
ESP.wdtDisable(); // This helps th avoid not reciving data after second try
//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);
}
if (ch != 0)
{
printArray(cString);
ESP.wdtEnable(1);
Serial.flush();
cString[chPos] = 0; //terminate cString
chPos = 0;
}
}
//printing output
void printArray(byte output[])
{
for (int i = 0; i < 16; i++)
{
if (output[i] < 0x10)
{
Serial.print("0");
}
Serial.print(output[i], HEX);
hexstring += String(output[i], HEX);
Serial.print(" ");
delay(100);
}
Serial.println();
Serial.println("Your String Sir:");
Serial.println(hexstring);
Serial.println();
ch = 0; // Avoid printing 0s when no data is recived
hexstring = ""; //Clear the memeory of hexstring
}
Working Output:
hexstring
, which can be concat to the next one.