dianartanto

Datascan7320 ke Google Sheet

Aug 4th, 2026 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.02 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3. #include <Wire.h>
  4. #include <WiFi.h>
  5. #include <HTTPClient.h>
  6.  
  7. // ===== Konfigurasi WiFi =====
  8. const char* ssid     = "xxxxxxxxxx";
  9. const char* password = "xxxxxxxxxx";
  10.  
  11. // ===== URL Google Apps Script (deploy sebagai web app) =====
  12. // Buat script di Google Sheets, publish sebagai web app, dan salin URL-nya.
  13. // Contoh: https://script.google.com/macros/s/xxxxxxxxxxxxxx/exec
  14. const String googleSheetsUrl = "https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/exec";
  15.  
  16. // ===== Konfigurasi UART untuk Datascan =====
  17. #define DS_RX  4
  18. #define DS_TX  5
  19. HardwareSerial Datascan(1);
  20.  
  21. // ===== Konfigurasi OLED I2C =====
  22. #define OLED_SDA 6
  23. #define OLED_SCL 7
  24. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ OLED_SCL, /* data=*/ OLED_SDA);
  25.  
  26. // ===== Tombol navigasi =====
  27. #define BTN_UP   9
  28. #define BTN_DOWN 8
  29.  
  30. // ===== Variabel global =====
  31. float channelValues[16] = {0};
  32. int currentPage = 0;                 // 0..3
  33. unsigned long lastRead = 0;
  34. unsigned long lastDebounceUp = 0;
  35. unsigned long lastDebounceDown = 0;
  36. const unsigned long debounceDelay = 50;
  37. bool lastUpState = HIGH;
  38. bool lastDownState = HIGH;
  39. bool updateDisplay = true;
  40. bool wifiConnected = false;
  41.  
  42. // ===== Fungsi kirim perintah dan baca respons sampai prompt =====
  43. String sendCommandAndWait(String cmd, unsigned long timeout = 3000) {
  44.   Datascan.print(cmd);
  45.   Datascan.print("\r");
  46.   unsigned long start = millis();
  47.   String response = "";
  48.   while (millis() - start < timeout) {
  49.     while (Datascan.available()) {
  50.       char c = Datascan.read();
  51.       response += c;
  52.       if (c == '$' || c == '*') {    // prompt diterima
  53.         return response;
  54.       }
  55.     }
  56.   }
  57.   // Timeout – tetap kembalikan data yang sudah terkumpul
  58.   return response;
  59. }
  60.  
  61. // ===== Parsing data dari respons =====
  62. void parseData(String response) {
  63.   int startIdx = response.indexOf('\n');
  64.   if (startIdx == -1) startIdx = response.indexOf('\r');
  65.   if (startIdx == -1) return;
  66.  
  67.   String dataLine = response.substring(startIdx + 1);
  68.   dataLine.replace("\r", "");
  69.   dataLine.replace("\n", "");
  70.   int timeoutPos = dataLine.indexOf("[TIMEOUT]");
  71.   if (timeoutPos != -1) {
  72.     dataLine = dataLine.substring(0, timeoutPos);
  73.   }
  74.   dataLine.trim();
  75.   if (dataLine.length() == 0) return;
  76.  
  77.   int count = 0;
  78.   int pos = 0;
  79.   while (count < 16 && pos < dataLine.length()) {
  80.     int comma = dataLine.indexOf(',', pos);
  81.     String token;
  82.     if (comma == -1) {
  83.       token = dataLine.substring(pos);
  84.       pos = dataLine.length();
  85.     } else {
  86.       token = dataLine.substring(pos, comma);
  87.       pos = comma + 1;
  88.     }
  89.     token.trim();
  90.     if (token.length() > 0) {
  91.       channelValues[count] = token.toFloat();
  92.     } else {
  93.       channelValues[count] = 0.0;
  94.     }
  95.     count++;
  96.   }
  97.   updateDisplay = true;
  98. }
  99.  
  100. // ===== Kirim data ke Google Sheets =====
  101. void sendDataToGoogleSheets() {
  102.   if (WiFi.status() != WL_CONNECTED) {
  103.     Serial.println("WiFi tidak terhubung, gagal kirim data.");
  104.     return;
  105.   }
  106.  
  107.   HTTPClient http;
  108.   String url = googleSheetsUrl;
  109.   url += "?";
  110.   for (int i = 0; i < 16; i++) {
  111.     url += "ch" + String(i + 1) + "=" + String(channelValues[i], 2);
  112.     if (i < 15) url += "&";
  113.   }
  114.  
  115.   http.begin(url);
  116.   http.setTimeout(5000); // timeout 5 detik
  117.   int httpCode = http.GET();
  118.  
  119.   if (httpCode > 0) {
  120.     Serial.printf("HTTP Response code: %d\n", httpCode);
  121.     String payload = http.getString();
  122.     Serial.println("Respon dari Google Sheets: " + payload);
  123.   } else {
  124.     Serial.printf("Error HTTP: %s\n", http.errorToString(httpCode).c_str());
  125.   }
  126.   http.end();
  127. }
  128.  
  129. // ===== Tampilkan data di OLED =====
  130. void displayData() {
  131.   u8g2.clearBuffer();
  132.   u8g2.setFont(u8g2_font_6x10_tf);
  133.  
  134.   int startCh = currentPage * 4 + 1;
  135.   char title[20];
  136.   sprintf(title, "Ch %d-%d", startCh, startCh + 3);
  137.   u8g2.drawStr(0, 10, title);
  138.  
  139.   for (int i = 0; i < 4; i++) {
  140.     int ch = startCh + i;
  141.     char buffer[40];
  142.     char numStr[12];
  143.     dtostrf(channelValues[ch - 1], 8, 2, numStr);
  144.     sprintf(buffer, "%2d: %s", ch, numStr);
  145.     u8g2.drawStr(0, 22 + i * 12, buffer);
  146.   }
  147.  
  148.   // Tampilkan status WiFi di pojok kanan bawah
  149.   if (wifiConnected) {
  150.     u8g2.drawStr(110, 62, "WiFi");
  151.   } else {
  152.     u8g2.drawStr(105, 62, "NoWiFi");
  153.   }
  154.  
  155.   u8g2.sendBuffer();
  156. }
  157.  
  158. // ===== Setup =====
  159. void setup() {
  160.   Serial.begin(115200);
  161.   // while (!Serial) delay(10);
  162.  
  163.   // Tombol
  164.   pinMode(BTN_UP, INPUT_PULLUP);
  165.   pinMode(BTN_DOWN, INPUT_PULLUP);
  166.  
  167.   // OLED
  168.   Wire.begin(OLED_SDA, OLED_SCL);
  169.   u8g2.begin();
  170.   u8g2.clearBuffer();
  171.   u8g2.setFont(u8g2_font_6x10_tf);
  172.   u8g2.drawStr(0, 20, "Init...");
  173.   u8g2.sendBuffer();
  174.  
  175.   // WiFi
  176.   Serial.print("Menghubungkan ke WiFi");
  177.   WiFi.begin(ssid, password);
  178.   WiFi.setTxPower(WIFI_POWER_8_5dBm);  // Buka komentar jika perlu
  179.  
  180.   int attempts = 0;
  181.   while (WiFi.status() != WL_CONNECTED && attempts < 30) {
  182.     delay(500);
  183.     Serial.print(".");
  184.     attempts++;
  185.   }
  186.   if (WiFi.status() == WL_CONNECTED) {
  187.     Serial.println("\nWiFi terhubung, IP: " + WiFi.localIP().toString());
  188.     wifiConnected = true;
  189.   } else {
  190.     Serial.println("\nGagal koneksi WiFi, lanjut tanpa WiFi.");
  191.     wifiConnected = false;
  192.   }
  193.  
  194.   // Datascan
  195.   Serial.println("Memulai komunikasi dengan Datascan...");
  196.   Datascan.begin(9600, SERIAL_8N1, DS_RX, DS_TX);
  197.   delay(1000);
  198.  
  199.   Datascan.write(0x05); // reset
  200.   delay(100);
  201.   while (Datascan.available()) Datascan.read();
  202.  
  203.   String resp = sendCommandAndWait("TC");
  204.   Serial.println("TC response: " + resp);
  205.   resp = sendCommandAndWait("RM0");
  206.   Serial.println("RM0 response: " + resp);
  207.   resp = sendCommandAndWait("SC1,16");
  208.   Serial.println("SC1,16 response: " + resp);
  209.  
  210.   Serial.println("Setup selesai. Membaca data setiap 10 detik.");
  211.   displayData();
  212. }
  213.  
  214. // ===== Loop =====
  215. void loop() {
  216.   // Baca tombol dengan debouncing
  217.   bool upState   = digitalRead(BTN_UP);
  218.   bool downState = digitalRead(BTN_DOWN);
  219.  
  220.   if (upState == LOW && lastUpState == HIGH && (millis() - lastDebounceUp > debounceDelay)) {
  221.     lastDebounceUp = millis();
  222.     if (currentPage > 0) {
  223.       currentPage--;
  224.       updateDisplay = true;
  225.     }
  226.   }
  227.   lastUpState = upState;
  228.  
  229.   if (downState == LOW && lastDownState == HIGH && (millis() - lastDebounceDown > debounceDelay)) {
  230.     lastDebounceDown = millis();
  231.     if (currentPage < 3) {
  232.       currentPage++;
  233.       updateDisplay = true;
  234.     }
  235.   }
  236.   lastDownState = downState;
  237.  
  238.   if (updateDisplay) {
  239.     displayData();
  240.     updateDisplay = false;
  241.   }
  242.  
  243.   // Baca data setiap 10 detik
  244.   if (millis() - lastRead >= 10000) {
  245.     lastRead = millis();
  246.  
  247.     String data = sendCommandAndWait("RL1,16");
  248.     Serial.println("=== Pembacaan Datascan (Ch 1-16) ===");
  249.     Serial.print(data);
  250.     Serial.println("=====================================");
  251.  
  252.     parseData(data);  // parsing dan set updateDisplay = true jika berhasil
  253.  
  254.     // Kirim ke Google Sheets setelah parsing
  255.     sendDataToGoogleSheets();
  256.   }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment