Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <U8g2lib.h>
- #include <Wire.h>
- #include <WiFi.h>
- #include <HTTPClient.h>
- // ===== Konfigurasi WiFi =====
- const char* ssid = "xxxxxxxxxx";
- const char* password = "xxxxxxxxxx";
- // ===== URL Google Apps Script (deploy sebagai web app) =====
- // Buat script di Google Sheets, publish sebagai web app, dan salin URL-nya.
- // Contoh: https://script.google.com/macros/s/xxxxxxxxxxxxxx/exec
- const String googleSheetsUrl = "https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/exec";
- // ===== Konfigurasi UART untuk Datascan =====
- #define DS_RX 4
- #define DS_TX 5
- HardwareSerial Datascan(1);
- // ===== Konfigurasi OLED I2C =====
- #define OLED_SDA 6
- #define OLED_SCL 7
- U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ OLED_SCL, /* data=*/ OLED_SDA);
- // ===== Tombol navigasi =====
- #define BTN_UP 9
- #define BTN_DOWN 8
- // ===== Variabel global =====
- float channelValues[16] = {0};
- int currentPage = 0; // 0..3
- unsigned long lastRead = 0;
- unsigned long lastDebounceUp = 0;
- unsigned long lastDebounceDown = 0;
- const unsigned long debounceDelay = 50;
- bool lastUpState = HIGH;
- bool lastDownState = HIGH;
- bool updateDisplay = true;
- bool wifiConnected = false;
- // ===== Fungsi kirim perintah dan baca respons sampai prompt =====
- String sendCommandAndWait(String cmd, unsigned long timeout = 3000) {
- Datascan.print(cmd);
- Datascan.print("\r");
- unsigned long start = millis();
- String response = "";
- while (millis() - start < timeout) {
- while (Datascan.available()) {
- char c = Datascan.read();
- response += c;
- if (c == '$' || c == '*') { // prompt diterima
- return response;
- }
- }
- }
- // Timeout – tetap kembalikan data yang sudah terkumpul
- return response;
- }
- // ===== Parsing data dari respons =====
- void parseData(String response) {
- int startIdx = response.indexOf('\n');
- if (startIdx == -1) startIdx = response.indexOf('\r');
- if (startIdx == -1) return;
- String dataLine = response.substring(startIdx + 1);
- dataLine.replace("\r", "");
- dataLine.replace("\n", "");
- int timeoutPos = dataLine.indexOf("[TIMEOUT]");
- if (timeoutPos != -1) {
- dataLine = dataLine.substring(0, timeoutPos);
- }
- dataLine.trim();
- if (dataLine.length() == 0) return;
- int count = 0;
- int pos = 0;
- while (count < 16 && pos < dataLine.length()) {
- int comma = dataLine.indexOf(',', pos);
- String token;
- if (comma == -1) {
- token = dataLine.substring(pos);
- pos = dataLine.length();
- } else {
- token = dataLine.substring(pos, comma);
- pos = comma + 1;
- }
- token.trim();
- if (token.length() > 0) {
- channelValues[count] = token.toFloat();
- } else {
- channelValues[count] = 0.0;
- }
- count++;
- }
- updateDisplay = true;
- }
- // ===== Kirim data ke Google Sheets =====
- void sendDataToGoogleSheets() {
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("WiFi tidak terhubung, gagal kirim data.");
- return;
- }
- HTTPClient http;
- String url = googleSheetsUrl;
- url += "?";
- for (int i = 0; i < 16; i++) {
- url += "ch" + String(i + 1) + "=" + String(channelValues[i], 2);
- if (i < 15) url += "&";
- }
- http.begin(url);
- http.setTimeout(5000); // timeout 5 detik
- int httpCode = http.GET();
- if (httpCode > 0) {
- Serial.printf("HTTP Response code: %d\n", httpCode);
- String payload = http.getString();
- Serial.println("Respon dari Google Sheets: " + payload);
- } else {
- Serial.printf("Error HTTP: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- }
- // ===== Tampilkan data di OLED =====
- void displayData() {
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_6x10_tf);
- int startCh = currentPage * 4 + 1;
- char title[20];
- sprintf(title, "Ch %d-%d", startCh, startCh + 3);
- u8g2.drawStr(0, 10, title);
- for (int i = 0; i < 4; i++) {
- int ch = startCh + i;
- char buffer[40];
- char numStr[12];
- dtostrf(channelValues[ch - 1], 8, 2, numStr);
- sprintf(buffer, "%2d: %s", ch, numStr);
- u8g2.drawStr(0, 22 + i * 12, buffer);
- }
- // Tampilkan status WiFi di pojok kanan bawah
- if (wifiConnected) {
- u8g2.drawStr(110, 62, "WiFi");
- } else {
- u8g2.drawStr(105, 62, "NoWiFi");
- }
- u8g2.sendBuffer();
- }
- // ===== Setup =====
- void setup() {
- Serial.begin(115200);
- // while (!Serial) delay(10);
- // Tombol
- pinMode(BTN_UP, INPUT_PULLUP);
- pinMode(BTN_DOWN, INPUT_PULLUP);
- // OLED
- Wire.begin(OLED_SDA, OLED_SCL);
- u8g2.begin();
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_6x10_tf);
- u8g2.drawStr(0, 20, "Init...");
- u8g2.sendBuffer();
- // WiFi
- Serial.print("Menghubungkan ke WiFi");
- WiFi.begin(ssid, password);
- WiFi.setTxPower(WIFI_POWER_8_5dBm); // Buka komentar jika perlu
- int attempts = 0;
- while (WiFi.status() != WL_CONNECTED && attempts < 30) {
- delay(500);
- Serial.print(".");
- attempts++;
- }
- if (WiFi.status() == WL_CONNECTED) {
- Serial.println("\nWiFi terhubung, IP: " + WiFi.localIP().toString());
- wifiConnected = true;
- } else {
- Serial.println("\nGagal koneksi WiFi, lanjut tanpa WiFi.");
- wifiConnected = false;
- }
- // Datascan
- Serial.println("Memulai komunikasi dengan Datascan...");
- Datascan.begin(9600, SERIAL_8N1, DS_RX, DS_TX);
- delay(1000);
- Datascan.write(0x05); // reset
- delay(100);
- while (Datascan.available()) Datascan.read();
- String resp = sendCommandAndWait("TC");
- Serial.println("TC response: " + resp);
- resp = sendCommandAndWait("RM0");
- Serial.println("RM0 response: " + resp);
- resp = sendCommandAndWait("SC1,16");
- Serial.println("SC1,16 response: " + resp);
- Serial.println("Setup selesai. Membaca data setiap 10 detik.");
- displayData();
- }
- // ===== Loop =====
- void loop() {
- // Baca tombol dengan debouncing
- bool upState = digitalRead(BTN_UP);
- bool downState = digitalRead(BTN_DOWN);
- if (upState == LOW && lastUpState == HIGH && (millis() - lastDebounceUp > debounceDelay)) {
- lastDebounceUp = millis();
- if (currentPage > 0) {
- currentPage--;
- updateDisplay = true;
- }
- }
- lastUpState = upState;
- if (downState == LOW && lastDownState == HIGH && (millis() - lastDebounceDown > debounceDelay)) {
- lastDebounceDown = millis();
- if (currentPage < 3) {
- currentPage++;
- updateDisplay = true;
- }
- }
- lastDownState = downState;
- if (updateDisplay) {
- displayData();
- updateDisplay = false;
- }
- // Baca data setiap 10 detik
- if (millis() - lastRead >= 10000) {
- lastRead = millis();
- String data = sendCommandAndWait("RL1,16");
- Serial.println("=== Pembacaan Datascan (Ch 1-16) ===");
- Serial.print(data);
- Serial.println("=====================================");
- parseData(data); // parsing dan set updateDisplay = true jika berhasil
- // Kirim ke Google Sheets setelah parsing
- sendDataToGoogleSheets();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment