124 lines
3.4 KiB
C++
124 lines
3.4 KiB
C++
#include <RCSwitch.h>
|
|
#include <DHT.h>
|
|
|
|
// پینها
|
|
#define DHTPIN 2
|
|
#define DHTTYPE DHT11
|
|
#define SOIL_MOISTURE_PIN A0
|
|
#define GAS_SENSOR_PIN A1
|
|
#define RF_TRANSMIT_PIN 9
|
|
|
|
// متن ثابت و کلیدها
|
|
|
|
const String PUBLIC_KEY = "as23f";
|
|
const String DEVICE_ID = "dr142";
|
|
|
|
// اشیاء
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
RCSwitch tx = RCSwitch();
|
|
|
|
// زمانبندی
|
|
unsigned long lastSendTime = 0;
|
|
const unsigned long sendInterval = 5000;
|
|
|
|
// تاخیر بین ارسال هر کاراکتر (ms)
|
|
const unsigned int INTER_CHAR_DELAY_MS = 15;
|
|
|
|
// XOR ساده
|
|
String encryptData(const String &data, const String &key) {
|
|
String out;
|
|
out.reserve(data.length());
|
|
for (int i = 0; i < data.length(); i++) {
|
|
out += (char)(data[i] ^ key[i % key.length()]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
static inline void sendChar(uint8_t c) {
|
|
tx.send((unsigned long)c, 8);
|
|
delay(INTER_CHAR_DELAY_MS);
|
|
}
|
|
|
|
static inline void sendHexByte(uint8_t b) {
|
|
char hex[3];
|
|
sprintf(hex, "%02X", b); // حروف بزرگ
|
|
sendChar(hex[0]);
|
|
sendChar(hex[1]);
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
dht.begin();
|
|
|
|
tx.enableTransmit(RF_TRANSMIT_PIN);
|
|
tx.setProtocol(1);
|
|
tx.setPulseLength(300);
|
|
tx.setRepeatTransmit(3);
|
|
|
|
Serial.println(F("=== Transmitter Started ==="));
|
|
Serial.print(F("Public Key: ")); Serial.println(PUBLIC_KEY);
|
|
Serial.print(F("Device ID : ")); Serial.println(DEVICE_ID);
|
|
}
|
|
|
|
void loop() {
|
|
// خواندن سنسورها
|
|
float temperature = dht.readTemperature();
|
|
float humidity = dht.readHumidity();
|
|
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
|
|
int gasValue = analogRead(GAS_SENSOR_PIN);
|
|
|
|
if (isnan(temperature) || isnan(humidity)) {
|
|
temperature = 0; humidity = 0;
|
|
}
|
|
|
|
soilMoisture = constrain(soilMoisture, 0, 1023);
|
|
gasValue = constrain(gasValue, 0, 1023);
|
|
int tempInt = constrain((int)(temperature * 10), 0, 9999); // چهار رقمی
|
|
int humidityInt = constrain((int)(humidity * 10), 0, 9999); // چهار رقمی
|
|
|
|
// ⚠️ خیلی مهم: ۲۶ بایت برای نَل ترمینیتور
|
|
char dataString[26];
|
|
// قالب دقیقاً 25 کاراکتر تولید میکند: 5 + (S####) + (G####) + (T####) + (H####)
|
|
// 5 + 1+4 + 1+4 + 1+4 + 1+4 = 25
|
|
int n = snprintf(
|
|
dataString, sizeof(dataString),
|
|
"%sS%04dG%04dT%04dH%04d",
|
|
DEVICE_ID.c_str(), soilMoisture, gasValue, tempInt, humidityInt
|
|
);
|
|
|
|
// اگر بنا به هر دلیلی طول غیرمنتظره شد، همینجا خروج
|
|
if (n != 25) {
|
|
Serial.print(F("Format length unexpected: ")); Serial.println(n);
|
|
delay(500);
|
|
return;
|
|
}
|
|
|
|
String plain = String(dataString);
|
|
String enc = encryptData(plain, PUBLIC_KEY);
|
|
|
|
if (millis() - lastSendTime >= sendInterval) {
|
|
Serial.println(F("=== Data to Send ==="));
|
|
Serial.print(F("Original: ")); Serial.println(plain);
|
|
Serial.print(F("Length : ")); Serial.println(plain.length());
|
|
|
|
// 1) ارسال 5 کاراکتر کلید عمومی
|
|
for (int i = 0; i < PUBLIC_KEY.length(); i++) {
|
|
sendChar((uint8_t)PUBLIC_KEY[i]);
|
|
}
|
|
|
|
// 2) ارسال دیتای رمز شده به صورت HEX (هر بایت → دو کاراکتر)
|
|
Serial.print(F("Encrypted HEX: "));
|
|
for (int i = 0; i < enc.length(); i++) {
|
|
uint8_t b = (uint8_t)enc[i];
|
|
char hex[3]; sprintf(hex, "%02X", b);
|
|
Serial.print(hex); Serial.print(' ');
|
|
sendHexByte(b);
|
|
}
|
|
Serial.println();
|
|
|
|
lastSendTime = millis();
|
|
}
|
|
|
|
delay(100);
|
|
}
|