old codes

This commit is contained in:
2025-12-19 11:50:13 +03:30
parent 735258f6ec
commit 66a02f4584
15 changed files with 5446 additions and 0 deletions

165
old/sender01/sender01.ino Normal file
View File

@@ -0,0 +1,165 @@
#include <RCSwitch.h>
#include <DHT.h>
// پین‌ها
#define DHTPIN 2
#define DHTTYPE DHT11
#define SOIL_MOISTURE_PIN A3
#define GAS_SENSOR_PIN A2
#define RF_TRANSMIT_PIN 9
const float RL = 10.0; // مقاومت Load روی ماژول (کیلو اهم)
float R0 = 50; // بعد از کالیبراسیون مقدار واقعی جایگزین کنید
// متن ثابت و کلیدها
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 = 10000;
// تاخیر بین ارسال هر کاراکتر (ms)
const unsigned int INTER_CHAR_DELAY_MS = 1;
// 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);
}
int getSoilMoistureInt() {
int adcValue = analogRead(SOIL_MOISTURE_PIN);
// مقادیر کالیبراسیون: خاک خشک و خاک خیس
const int adcDry = 1023; // خاک کاملاً خشک
const int adcWet = 400; // خاک کاملاً خیس
// محاسبه درصد رطوبت با یک رقم اعشار
float soilPercent = (float)(adcDry - adcValue) / (adcDry - adcWet) * 100.0;
// محدود کردن بین 0 تا 100
if (soilPercent > 100.0) soilPercent = 100.0;
if (soilPercent < 0.0) soilPercent = 0.0;
// ضرب در 10 و تبدیل به عدد صحیح
int soilInt = (int)(soilPercent * 10 + 0.5); // +0.5 برای گرد کردن صحیح
return soilInt;
}
int readGas() {
long sum = 0;
for (int i = 0; i < 5; i++) {
sum += analogRead(GAS_SENSOR_PIN);
delay(10);
}
float adcValue = sum / 5.0; // میانگین ADC
float vrl = (adcValue / 1023.0) * 5.0; // ولتاژ خروجی
if (vrl < 0.001) vrl = 0.001; // جلوگیری از تقسیم بر صفر
float Rs = (5.0 - vrl) * RL / vrl; // مقاومت سنسور (kΩ)
float ratio = Rs / R0; // نسبت Rs/R0
// ثابت‌های تقریبی از دیتاشیت MQ7 (CO curve)
float m = -0.77;
float b = 0.36;
float ppm_log = (log10(ratio) - b) / m;
int ppm = round(pow(10, ppm_log));
return ppm; // خروجی اعشاری
}
void loop() {
// خواندن سنسورها
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int soilMoisture = getSoilMoistureInt();
int gasValue = readGas(); //analogRead(GAS_SENSOR_PIN);
if (isnan(temperature) || isnan(humidity)) {
temperature = 0; humidity = 0;
}
soilMoisture = constrain(soilMoisture, 0, 9999);
gasValue = constrain(gasValue, 0, 9999);//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);
}