/* * ═══════════════════════════════════════════════════════════════════════════ * ravisha_esp32_sim7600.ino — ESP32 + مودم ۴G (SIM7600) با کتابخانه‌ی TinyGSM * برد : ESP32 DevKit * اینترنت : سیم‌کارت داده روی SIM7600 (4G/LTE) — پشتیبان HTTPS * مناسب : باغ‌های دور از برق و وای‌فای که سرورشان HTTPS اجباری است * * سیم‌بندی (Serial2): * ESP32 GPIO16 (RX) ← SIM7600 TX * ESP32 GPIO17 (TX) → SIM7600 RX * GND مشترک — تغذیه‌ی ماژول جدا و قوی (۲A) * * کتابخانه‌ها: TinyGSM (vsmith) + ArduinoHttpClient * ═══════════════════════════════════════════════════════════════════════════ */ #define TINY_GSM_MODEM_SIM7600 #define TINY_GSM_RX_BUFFER 1024 #include #include const char APN[] = "mci"; // از اپراتور سیم‌کارت بگیر const char GPRS_USER[] = ""; const char GPRS_PASS[] = ""; const char SERVER[] = "arshaiot.shop"; const int PORT = 443; // 80 برای HTTP const char* DEVICE_ID = "ESP32-SIM7600-01"; const char* API_KEY = "RAVISHA-DEV-XXXXXXXXXXXX"; const int SOIL_PIN = 34; const int VALVE_PIN = 25; TinyGsm modem(Serial2); TinyGsmClient net(modem); HttpClient https(net, SERVER, PORT); void modemUp() { Serial2.begin(115200, SERIAL_8N1, 16, 17); delay(3000); modem.restart(); Serial.println(modem.getModemInfo()); if (!modem.gprsConnect(APN, GPRS_USER, GPRS_PASS)) { Serial.println(F("GPRS وصل نشد — APN یا آنتن را چک کنید")); } } int soilPercent() { long s = 0; for (int i = 0; i < 8; i++) { s += analogRead(SOIL_PIN); delay(6); } return constrain(map((int) (s / 8), 2600, 1100, 0, 100), 0, 100); } void sendReading() { String path = "/api/device.php"; String body = String("{\"moisture\":") + soilPercent() + ",\"signal\":" + constrain(map(modem.getSignalQuality(), 0, 30, 0, 100), 0, 100) + "}"; https.beginRequest(); https.post(path); https.sendHeader("Content-Type", "application/json"); https.sendHeader("X-Device-Id", DEVICE_ID); https.sendHeader("X-Api-Key", API_KEY); https.sendHeader("Content-Length", body.length()); https.beginBody(); https.print(body); int status = https.responseStatusCode(); String res = https.responseBody(); https.endRequest(); Serial.printf("POST %d → %s\n", status, res.c_str()); if (status == 200) { if (res.indexOf("\"action\":\"open\"") >= 0) digitalWrite(VALVE_PIN, HIGH); if (res.indexOf("\"action\":\"close\"") >= 0) digitalWrite(VALVE_PIN, LOW); } } void setup() { Serial.begin(115200); pinMode(VALVE_PIN, OUTPUT); digitalWrite(VALVE_PIN, LOW); modemUp(); sendReading(); } void loop() { Serial.println(F("خواب ۱۵ دقیقه‌ای…")); esp_sleep_enable_timer_wakeup(15ULL * 60ULL * 1000000ULL); esp_deep_sleep_start(); }