Files
Arduino/14041130/TestLCD/Config.h
2026-02-19 06:17:44 +03:30

157 lines
4.6 KiB
C
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef CONFIG_H
#define CONFIG_H
#include <Arduino.h>
#define I2C_SDA_PIN PB9
#define I2C_SCL_PIN PB8
#define SHT31_ADDRESS 0x44
#define BH1750_ADDRESS 0x23
// ========== تنظیمات Timing (از کد اول) ==========
#define READ_INTERVAL 2000UL // ms - خواندن سنسورها
#define HEALTH_CHECK_INTERVAL 30000UL // ms - بررسی سلامت
#define RECOVERY_COOLDOWN 5000UL // ms - فاصله بین recoveryها
#define MAX_ERROR_COUNT 1000 // حداکثر خطاهای ثبت‌شده
unsigned long lastBh1750Success = 0;
unsigned long lastSht31Success = 0;
uint8_t bh1750ErrorCount = 0;
uint8_t sht31ErrorCount = 0;
const uint8_t MAX_ERRORS = 5;
float lastLightLevel = 0.0;
float lastTemperature = 0.0;
float lastHumidity = 0.0;
// -------------------- پیکربندی --------------------
#define FLASH_CS PB12
#define FLASH_MOSI PB15
#define FLASH_MISO PB14
#define FLASH_SCK PB13
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//#define MQ7_PIN PA2
//#define VCC 4.63
#define RL 790.0 //10000.0
//#define VDIV_RATIO 0.03017 //0.681
//#define SDA_PIN PB9
//#define SCL_PIN PB8
#define PWRKEY_PIN PB5
#define SENSOR_READ_INTERVAL 5000
#define POWER_PIN PA1
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
// #define TFT_CS PB0 // تغییر به پین‌های شما
// #define TFT_DC PA4
// #define TFT_RST PA6
// #define TFT_BL PB1
#define VOLTAGE_DIVIDER_PIN PA0 // پین خواندن ولتاژ
#define BATTERY_VOLTAGE_PIN_A3 PA3 // پین جدید برای باتری
#define VOLTAGE_DIVIDER_RATIO 2.0 // نسبت تقسیم (R1=R2=10k => نسبت = 2)
#define ADC_REF_VOLTAGE 3.3 // ولتاژ مرجع ADC در STM32 (معمولاً 3.3V)
#define ADC_RESOLUTION 4096 // رزولوشن ADC 12-bit = 4096
// -------------------- آدرس‌های حافظه --------------------
#define CONFIG_ADDRESS 0x000000
#define DATA_ADDRESS 0x010000
// -------------------- آدرس حافظه داخلی STM32 --------------------
#define INTERNAL_CONFIG_ADDR 0x8007800 // آخرین صفحه فلش در STM32F103C8
// -------------------- انوم‌ها --------------------
enum SIMType { SIM_UNKNOWN = 0, SIM_HAMRAHE_AVAL = 1, SIM_IRANCELL = 2, SIM_RIGHTEL = 3 };
// -------------------- ساختارها --------------------
struct DeviceConfig {
char signature[4];
char deviceId[16];
char serverPhoneNumber[16];
char serverUrl[64];
unsigned long uploadInterval;
unsigned long smsInterval;
unsigned long saveInterval;
SIMType simType;
bool smsEnabled;
bool verified;
bool valid;
};
struct SensorData {
float temperature;
float humidity;
float coPPM;
float lightLux;
unsigned long timestamp;
uint8_t sent;
float volage;
int power;
int oldPower;
float batteryVoltage;
float batteryPercent;
// اضافه شدن برای نمایشگر
float tempPercent; // درصد دما (0-100)
float humPercent; // درصد رطوبت (0-100)
float lightPercent; // درصد نور (0-100)
float gasPercent; // درصد گاز (0-100)
};
// -------------------- توابع کمکی --------------------
inline String simTypeToString(SIMType type) {
switch(type) {
case SIM_HAMRAHE_AVAL: return "Hamrah Aval";
case SIM_IRANCELL: return "Irancell";
case SIM_RIGHTEL: return "Rightel";
default: return "Unknown";
}
}
inline String simTypeToAPN(SIMType type) {
switch(type) {
case SIM_HAMRAHE_AVAL: return "mcinet";
case SIM_IRANCELL: return "mtnirancell";
case SIM_RIGHTEL: return "rightel";
default: return "mcinet";
}
}
inline String generateVerificationCode(String tokenCode) {
long token = tokenCode.toInt();
long verification = (token * 7 + 12345) % 100000;
char buffer[6];
sprintf(buffer, "%05ld", verification);
return String(buffer);
}
inline void calculatePercentages(SensorData& data) {
// دما: فرض 0-50 درجه سانتیگراد
data.tempPercent = (data.temperature * 100) / 50.0;
if (data.tempPercent > 100) data.tempPercent = 100;
if (data.tempPercent < 0) data.tempPercent = 0;
// رطوبت: 0-100%
data.humPercent = data.humidity;
if (data.humPercent > 100) data.humPercent = 100;
if (data.humPercent < 0) data.humPercent = 0;
// نور: فرض 0-10000 لوکس
data.lightPercent = (data.lightLux * 100) / 10000.0;
if (data.lightPercent > 100) data.lightPercent = 100;
if (data.lightPercent < 0) data.lightPercent = 0;
// گاز: فرض 0-200 ppm (خطای تایپی تصحیح شد)
data.gasPercent = (data.coPPM * 100) / 200.0;
if (data.gasPercent > 100) data.gasPercent = 100;
if (data.gasPercent < 0) data.gasPercent = 0;
}
#endif // CONFIG_H