/* * ═══════════════════════════════════════════════════════════════════════════ * ravisha_esp32_wifi.ino — ایستگاه «رویشا» روی ESP32 (دو هسته، وای‌فای + BT) * برد : ESP32 DevKit / ESP32-WROOM-32 * اینترنت : وای‌فای؛ برای HTTPS هم آماده است (نمونه در انتها) * سنسورها : رطوبت خاک آنالوگ + دمای DS18B20 (اختیاری) + سه شیر برقی * مقصد : POST /api/device.php با هدرهای X-Device-Id و X-Api-Key * ═══════════════════════════════════════════════════════════════════════════ */ #include #include #include #include const char* WIFI_SSID = "BAQ-2G"; const char* WIFI_PASS = "12345678"; const char* API_URL = "http://arshaiot.shop/api/device.php"; const char* CMD_URL = "http://arshaiot.shop/api/commands.php"; // خواندن دستور (اختیاری) const char* DEVICE_ID = "ESP32-A4CF12B9"; const char* API_KEY = "RAVISHA-DEV-NORTH-7F3A91"; const int SOIL_PIN = 34; // ADC1 (با Wi-Fi کار می‌کند) const int VALVE_PINS[3] = {25, 26, 27}; const int TEMP_PIN = 4; // DS18B20 OneWire oneWire(TEMP_PIN); DallasTemperature ds(&oneWire); void valvesAll(bool open) { for (int i = 0; i < 3; i++) digitalWrite(VALVE_PINS[i], open ? HIGH : LOW); } int soilPercent() { long sum = 0; for (int i = 0; i < 8; i++) { sum += analogRead(SOIL_PIN); delay(6); } int raw = (int) (sum / 8); return constrain(map(raw, 2600, 1100, 0, 100), 0, 100); // کالیبراسیون خودت را بگذار } void wifiConnect() { WiFi.mode(WIFI_STA); WiFi.setHostname(DEVICE_ID); WiFi.begin(WIFI_SSID, WIFI_PASS); for (int i = 0; i < 40 && WiFi.status() != WL_CONNECTED; i++) delay(500); } bool postReading() { if (WiFi.status() != WL_CONNECTED) wifiConnect(); if (WiFi.status() != WL_CONNECTED) return false; ds.requestTemperatures(); float t = ds.getTempCByIndex(0); int m = soilPercent(); int batt = constrain(map(analogRead(35), 1800, 2400, 0, 100), 0, 100); // اگر باتری وصل است HTTPClient http; http.begin(API_URL); http.setTimeout(8000); http.addHeader("Content-Type", "application/json"); http.addHeader("X-Device-Id", DEVICE_ID); http.addHeader("X-Api-Key", API_KEY); String body = String("{\"moisture\":") + m; if (t > -50) body += ",\"temperature\":" + String(t, 1); body += ",\"battery\":" + String(batt); /* ── v27: خط ارتباط و IP خودِ دستگاه ── */ body += ",\"transport\":\"wifi\""; body += ",\"device_ip\":\"" + WiFi.localIP().toString() + "\""; body += ",\"wifi_ssid\":\"" + WiFi.SSID() + "\""; body += ",\"wifi_rssi\":" + String(WiFi.RSSI()) + "}"; int code = http.POST(body); String res = http.getString(); http.end(); Serial.printf("POST %d %s\n", code, res.c_str()); if (code == 200) { if (res.indexOf("\"action\":\"open\"") >= 0) valvesAll(true); if (res.indexOf("\"action\":\"close\"") >= 0) valvesAll(false); } return code == 200; } void setup() { Serial.begin(115200); for (int i = 0; i < 3; i++) { pinMode(VALVE_PINS[i], OUTPUT); digitalWrite(VALVE_PINS[i], LOW); } ds.begin(); wifiConnect(); postReading(); // اولین داده بلافاصله } void loop() { static unsigned long lastSend = 0, lastCmd = 0; if (millis() - lastSend > 15UL * 60UL * 1000UL) { lastSend = millis(); postReading(); } /* خواندن صریح دستورها (اگر در پاسخ خوانش نبود) */ if (millis() - lastCmd > 60UL * 1000UL) { lastCmd = millis(); if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String url = String(CMD_URL) + "?device=" + DEVICE_ID + "&key=" + API_KEY; http.begin(url); int code = http.GET(); if (code == 200) { String res = http.getString(); if (res.indexOf("\"action\":\"open\"") >= 0) valvesAll(true); if (res.indexOf("\"action\":\"close\"") >= 0) valvesAll(false); } http.end(); } } delay(1000); } /* * ── اگر سرور HTTPS است (ESP32 از TLS پشتیبانی می‌کند) ── * WiFiClientSecure client; * client.setInsecure(); // تست؛ برای امنیت کامل setCACert() * http.begin(client, "https://arshaiot.shop/api/device.php"); */