197 lines
4.7 KiB
C++
197 lines
4.7 KiB
C++
#include <Arduino.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <Fonts/FreeSans9pt7b.h>
|
|
#include <Fonts/FreeSansBold12pt7b.h>
|
|
#include <Fonts/FreeSansBold18pt7b.h>
|
|
#include <Adafruit_SHT31.h>
|
|
#include <BH1750.h>
|
|
#include <SPI.h>
|
|
|
|
// -------------------- فایلهای پروژه --------------------
|
|
#include "Config.h"
|
|
#include "Memory.h"
|
|
#include "Sensors.h"
|
|
#include "EC200U.h"
|
|
#include "Display.h"
|
|
#include "Voltage_Reader.h"
|
|
|
|
// -------------------- متغیرهای سراسری --------------------
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
|
BH1750 lightMeter;
|
|
Adafruit_SHT31 sht31;
|
|
HardwareSerial EC200U(USART3);
|
|
SPIClass flashSPI(FLASH_MOSI, FLASH_MISO, FLASH_SCK);
|
|
|
|
DeviceConfig config;
|
|
SensorData currentData;
|
|
|
|
// متغیرهای موقت برای پردازش پیامکها
|
|
String tempPhoneNumber = "";
|
|
String tempTokenCode = "";
|
|
bool awaitingSMS2 = false;
|
|
|
|
unsigned long lastSensorRead = 0;
|
|
unsigned long lastUpload = 0;
|
|
unsigned long lastDisplayChange = 0;
|
|
unsigned long lastNetworkStatusUpdate = 0;
|
|
|
|
bool networkConnected = false;
|
|
int displayMode = 0;
|
|
String lastError = "";
|
|
String currentAPN = "";
|
|
String lastMessage = "";
|
|
|
|
// وضعیت سنسورها و اتصالات
|
|
bool sht31Connected = false;
|
|
bool lightSensorConnected = false;
|
|
bool coSensorConnected = false;
|
|
bool simConnected = false;
|
|
bool networkRegistered = false;
|
|
int signalStrength = 0;
|
|
|
|
// متغیرهای CO کالیبراسیون
|
|
unsigned long systemStartTime = 0;
|
|
const long calibrationPeriod = 600000; // 10 دقیقه
|
|
bool calibrationComplete = false;
|
|
float MQ7_R0 = 10000.0;
|
|
|
|
bool isInCall = false;
|
|
|
|
|
|
bool powerState = false;
|
|
|
|
// -------------------- Setup --------------------
|
|
void setup() {
|
|
Serial1.begin(115200);
|
|
|
|
pinMode(PWRKEY_PIN, OUTPUT);
|
|
digitalWrite(PWRKEY_PIN, LOW);
|
|
|
|
pinMode(POWER_PIN, INPUT);
|
|
|
|
powerState = digitalRead(POWER_PIN) == HIGH;
|
|
|
|
delay(1000);
|
|
|
|
Serial1.println("\n\n🚀 IoT Device Starting...");
|
|
|
|
systemStartTime = millis();
|
|
|
|
pinMode(MQ7_PIN, INPUT);
|
|
|
|
Wire.setSDA(SDA_PIN);
|
|
Wire.setSCL(SCL_PIN);
|
|
Wire.begin();
|
|
|
|
// 1. ولتاژ را تست کن
|
|
bool voltageOK = voltageReader.testCircuit();
|
|
|
|
if (!voltageOK) {
|
|
Serial1.println("⚠️ Voltage issue detected!");
|
|
}
|
|
|
|
// 2. اطلاعات دیباگ ولتاژ
|
|
voltageReader.debugInfo();
|
|
|
|
// 3. خواندن اولیه ولتاژ
|
|
float initialVoltage = voltageReader.readVoltage();
|
|
Serial1.print("Initial voltage: ");
|
|
Serial1.print(initialVoltage, 2);
|
|
Serial1.println("V");
|
|
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
|
|
Serial1.println("❌ OLED failed!");
|
|
}
|
|
|
|
sht31.begin(0x44);
|
|
lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE);
|
|
|
|
flashInit();
|
|
readConfig();
|
|
|
|
calibrateMQ7();
|
|
|
|
awaitingSMS2 = false;
|
|
tempPhoneNumber = "";
|
|
tempTokenCode = "";
|
|
|
|
EC200U.begin(115200);
|
|
initEC200U();
|
|
if (config.verified) {
|
|
Serial1.println("Device ID: " + String(config.deviceId));
|
|
currentAPN = simTypeToAPN(config.simType);
|
|
//initEC200U();
|
|
} else {
|
|
Serial1.println("⚠️ Not configured - Waiting for SMS");
|
|
}
|
|
|
|
readSensors();
|
|
|
|
updateNetworkStatus();
|
|
lastNetworkStatusUpdate = millis();
|
|
|
|
Serial1.println("✅ Ready - CO calibration: " + String(calibrationComplete ? "Complete" : "In progress"));
|
|
|
|
if (config.verified && networkConnected) {
|
|
Serial1.println("\n🚀 First upload - sending data immediately...");
|
|
uploadData();
|
|
}
|
|
}
|
|
|
|
// -------------------- Loop --------------------
|
|
void loop() {
|
|
if (!isInCall) {
|
|
digitalWrite(PWRKEY_PIN, LOW);
|
|
}
|
|
|
|
bool currentPower = digitalRead(POWER_PIN) == HIGH;
|
|
|
|
if (currentPower != powerState) {
|
|
|
|
powerState = currentPower;
|
|
|
|
if (powerState) {
|
|
Serial.println("5V Connected");
|
|
} else {
|
|
Serial.println("5V Disconnected");
|
|
}
|
|
lastUpload -= 86400000L;
|
|
}
|
|
|
|
checkSMS();
|
|
|
|
if (!calibrationComplete && (millis() - systemStartTime >= calibrationPeriod)) {
|
|
calibrationComplete = true;
|
|
Serial1.println("✅ CO calibration period completed!");
|
|
}
|
|
|
|
if (millis() - lastSensorRead > SENSOR_READ_INTERVAL) {
|
|
readSensors();
|
|
|
|
float voltage = voltageReader.readVoltage();
|
|
Serial1.print("📊 System Voltage: ");
|
|
Serial1.print(voltage, 2);
|
|
Serial1.println("V");
|
|
|
|
currentData.volage=voltage;
|
|
|
|
if(currentPower)
|
|
currentData.power=1;
|
|
else
|
|
currentData.power=0;
|
|
|
|
unsigned long uploadIntervalMs = config.uploadInterval * 60000UL;
|
|
if (millis() - lastUpload > uploadIntervalMs) {
|
|
Serial1.print("Upload interval reached (");
|
|
Serial1.print(config.uploadInterval);
|
|
Serial1.println(" min), starting upload...");
|
|
uploadData();
|
|
}
|
|
}
|
|
|
|
updateDisplay();
|
|
delay(100);
|
|
}
|