Kamis, 29 Agustus 2024

COS REAPETER RADIO COMMUNICATION


 


Dasar dari rangkaian ini umumnya difungsikan untuk mengaktifkan switch PTT transmitter ketika receiver menerima signal dari pancaran dari transmitter pengguna lainnya (Reapeter) 😀

Sabtu, 29 Juni 2024


FAN RADIATOR TELEGWEMOS 2 WIFI


//*FAN RADIATOR TELEGWEMOS 2 WIFI*

//*IoT FAN PC - PROJECT ON JUNI 2024- Ide and Design Dunia_budi*

//*DATA TELEGRAM to GOOGLE SHEETS*

//*SETIAP 3 MENIT RELAY ON DAN OFF*

//*Per 15 menit notif to Telegram*

//*Version 1.2*LEBIH CEPAT

 

#include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>

#include <UniversalTelegramBot.h>

#include <ArduinoJson.h>

 

// Wi-Fi settings

const char* ssid1 = "Indonesia1";

const char* password1 = "Rum4hku1";

const char* ssid2 = "MEDIA_BARU";

const char* password2 = "Tvri1234";

 

// Telegram settings

#define BOTtoken "12345678:XXxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

// isikan bot telegram groupmu

#define groupID "-100xxxxxxxxxxxxxxx"

 // isikan 

const char* googleScriptUrl = "https://script.google.com/macros/s/AKfxxxxxxxxxxxxxxx";

 

// Pin definitions for Lolin WeMos D1

const int relayPin = D1;  // GPIO5

const int ledPin = D2;    // GPIO2

 

// State variables

bool relayState = LOW;

bool ledState = HIGH;  // Start with LED off (active LOW)

unsigned long previousMillis = 0;

unsigned long previousLedMillis = 0;

unsigned long lastTelegramNotification = 0;

 

// Intervals

const unsigned long RELAY_INTERVAL = 3 * 60 * 1000; // 3 minutes

const unsigned long LED_INTERVAL = 1000; // 1 second

const unsigned long TELEGRAM_NOTIFICATION_INTERVAL = 15 * 60 * 1000; // 15 minutes

 

WiFiClientSecure client;

UniversalTelegramBot bot(BOTtoken, client);

 

void setup() {

  Serial.begin(115200);

  

  pinMode(relayPin, OUTPUT);

  pinMode(ledPin, OUTPUT);

  digitalWrite(relayPin, relayState);

  digitalWrite(ledPin, ledState);  // Turn off LED (it's active LOW)

 

  connectToWiFi();

 

  client.setInsecure(); // Use this for simplicity, but consider using a proper certificate in production

 

  sendTelegramMessage("Fan IoT PC active");

  sendToGoogleSheets("Fan IoT PC active");

}

 

void loop() {

  unsigned long currentMillis = millis();

 

  // Toggle relay every RELAY_INTERVAL

  if (currentMillis - previousMillis >= RELAY_INTERVAL) {

    previousMillis = currentMillis;

    toggleRelay();

  }

 

  // Blink LED

  if (currentMillis - previousLedMillis >= LED_INTERVAL) {

    previousLedMillis = currentMillis;

    blinkLed();

  }

 

  // Send periodic Telegram notification

  if (currentMillis - lastTelegramNotification >= TELEGRAM_NOTIFICATION_INTERVAL) {

    lastTelegramNotification = currentMillis;

    sendPeriodicTelegramNotification();

  }

}

 

void connectToWiFi() {

  WiFi.mode(WIFI_STA);

  WiFi.disconnect();

  delay(100);

 

  Serial.println("Scanning for Wi-Fi networks...");

  int numberOfNetworks = WiFi.scanNetworks();

 

  for (int i = 0; i < numberOfNetworks; i++) {

    String ssid = WiFi.SSID(i);

    if (ssid == ssid1) {

      WiFi.begin(ssid1, password1);

      Serial.println("Connecting to " + String(ssid1));

      if (waitForConnection()) return;

    } else if (ssid == ssid2) {

      WiFi.begin(ssid2, password2);

      Serial.println("Connecting to " + String(ssid2));

      if (waitForConnection()) return;

    }

  }

 

  Serial.println("Failed to connect to any Wi-Fi network");

}

 

bool waitForConnection() {

  int attempts = 0;

  while (WiFi.status() != WL_CONNECTED && attempts < 20) {

    delay(500);

    Serial.print(".");

    blinkLed(); // Blink LED while connecting

    attempts++;

  }

  if (WiFi.status() == WL_CONNECTED) {

    Serial.println("\nConnected to Wi-Fi");

    return true;

  }

  Serial.println("\nConnection failed");

  return false;

}

 

void toggleRelay() {

  relayState = !relayState;

  digitalWrite(relayPin, relayState);

  String status = relayState ? "Position Fan ON" : "Position Fan OFF";

  sendToGoogleSheets(status);

}

 

void blinkLed() {

  ledState = !ledState;

  digitalWrite(ledPin, ledState);

  Serial.println(ledState ? "LED ON" : "LED OFF"); // Debug output

}

 

void sendPeriodicTelegramNotification() {

  String status = relayState ? "Position Fan ON" : "Position Fan OFF";

  sendTelegramMessage(status);

}

 

void sendTelegramMessage(String message) {

  bot.sendMessage(groupID, message, "");

}

 

String urlEncode(const String &str) {

  String encodedString = "";

  char c;

  char code0;

  char code1;

  for (int i = 0; i < str.length(); i++) {

    c = str.charAt(i);

    if (c == ' ') {

      encodedString += '+';

    } else if (isalnum(c)) {

      encodedString += c;

    } else {

      code1 = (c & 0xf) + '0';

      if ((c & 0xf) > 9) {

        code1 = (c & 0xf) - 10 + 'A';

      }

      c = (c >> 4) & 0xf;

      code0 = c + '0';

      if (c > 9) {

        code0 = c - 10 + 'A';

      }

      encodedString += '%';

      encodedString += code0;

      encodedString += code1;

    }

  }

  return encodedString;

}

 

void sendToGoogleSheets(String status) {

  if (WiFi.status() == WL_CONNECTED) {

    String url = String(googleScriptUrl) + "?status=" + urlEncode(status);

    client.connect("script.google.com", 443);

    client.print(String("GET ") + url + " HTTP/1.1\r\n" +

                 "Host: script.google.com\r\n" +

                 "User-Agent: ESP8266\r\n" +

                 "Connection: close\r\n\r\n");

    while (client.connected()) {

      String line = client.readStringUntil('\n');

      if (line == "\r") {

        break;

      }

    }

    String response = client.readString();

    Serial.println(response);

  }

}