Difference between revisions of "Arduino: Ethernet UDP Send isi Analog Input 0 ke Graphite di Server Timing dengan RTC DS1302"

From OnnoWiki
Jump to navigation Jump to search
(New page: ==Code== /* UDPSendAnalogInput: Kirim pakai UDP ke 192.168.0.100:1000 Format test.data analogdata unixtime Sinkronisasi waktu menggunakan DS1302 created 27 Feb 201...)
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Code==
 
==Code==
 +
  
 
  /*
 
  /*
 
   UDPSendAnalogInput:
 
   UDPSendAnalogInput:
 
    
 
    
   Kirim pakai UDP ke 192.168.0.100:1000
+
   Kirim pakai UDP ke 192.168.0.100:2003
 
    
 
    
 
   Format
 
   Format
 
   test.data analogdata unixtime
 
   test.data analogdata unixtime
 
    
 
    
   Sinkronisasi waktu menggunakan DS1302
+
   Sinkronisasi waktu menggunakan DS1302  
 
   
 
   
 
   created 27 Feb 2016
 
   created 27 Feb 2016
Line 22: Line 23:
 
  #include <DS1302.h>
 
  #include <DS1302.h>
 
   
 
   
  #define TIME_HEADER  "T"  // Header tag for serial time sync message
+
  // Create a DS1302 object.
  #define TIME_REQUEST  7   // ASCII bell character requests a time sync message
+
  DS1302 rtc(8, 7, 6);
 
 
 
   
 
   
  // Enter a MAC address and IP address for your controller below.
+
  // MAC & IP address Arduino
// The IP address will be dependent on your local network:
 
 
  byte mac[] = {
 
  byte mac[] = {
 
   0x02, 0xCA, 0xFF, 0xEE, 0xBA, 0xBE
 
   0x02, 0xCA, 0xFF, 0xEE, 0xBA, 0xBE
Line 38: Line 37:
 
  IPAddress remoteIP(192,168,0,100);
 
  IPAddress remoteIP(192,168,0,100);
 
  unsigned int remotePort = 2003;
 
  unsigned int remotePort = 2003;
 
+
 
  void setup() {
 
  void setup() {
 
   // start the Ethernet and UDP:
 
   // start the Ethernet and UDP:
 
   Ethernet.begin(mac, ip);
 
   Ethernet.begin(mac, ip);
 
   Udp.begin(localPort);
 
   Udp.begin(localPort);
   Serial.begin(9600);
+
   Serial.begin(9600);  
 
+
 
   // Inisialisasi waktu dari DS1302
 
   // Inisialisasi waktu dari DS1302
 
   rtc.writeProtect(false);
 
   rtc.writeProtect(false);
 
   rtc.halt(false);
 
   rtc.halt(false);
 +
  // The following lines can be commented out to use the values already stored in the DS1302
 +
  // rtc.setDOW(TUESDAY);        // Set Day-of-Week to MONDAY
 +
  // rtc.setTime(8,44,0);    // Set the time to 08:00:00 (24hr format)
 +
  // rtc.setDate(1,3,2016);  // Set the date to Mar 1, 2016
 +
 
 
   Time t = rtc.getTime();
 
   Time t = rtc.getTime();
 
   setTime(t.hour,t.min,t.sec,t.date,t.mon,t.year);
 
   setTime(t.hour,t.min,t.sec,t.date,t.mon,t.year);
Line 53: Line 57:
 
   
 
   
 
  void loop() {
 
  void loop() {
 +
  // Time t = rtc.getTime();
 +
  // time_t tm = now();
 
   Udp.beginPacket(remoteIP, remotePort);
 
   Udp.beginPacket(remoteIP, remotePort);
 
   int sensorReading = analogRead(0);
 
   int sensorReading = analogRead(0);
   Udp.write("test.data ");
+
   Udp.write("test.ds1302 ");
 
   Udp.print(sensorReading);  
 
   Udp.print(sensorReading);  
 
   Udp.write(" ");
 
   Udp.write(" ");
 
   Udp.println(now());
 
   Udp.println(now());
   
+
     
   Serial.write("test.data ");
+
   Serial.write("test.ds1302 ");
 
   Serial.print(sensorReading);  
 
   Serial.print(sensorReading);  
 
   Serial.write(" ");
 
   Serial.write(" ");
 
   Serial.println(now());
 
   Serial.println(now());
 
   Udp.endPacket();
 
   Udp.endPacket();
 +
  delay(100);
 
  }
 
  }

Latest revision as of 15:52, 3 March 2016

Code

/*
 UDPSendAnalogInput:
 
 Kirim pakai UDP ke 192.168.0.100:2003
 
 Format
 test.data analogdata unixtime
 
 Sinkronisasi waktu menggunakan DS1302 

 created 27 Feb 2016
 by Onno W. Purbo
*/

#include <SPI.h>   // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <stdio.h>
#include <Time.h>
#include <DS1302.h>

// Create a DS1302 object.
DS1302 rtc(8, 7, 6);

// MAC & IP address Arduino
byte mac[] = {
  0x02, 0xCA, 0xFF, 0xEE, 0xBA, 0xBE
};
IPAddress ip(192, 168, 0, 4);
unsigned int localPort = 8888; // local port to listen on

// Kirim ke graphite carbon cache di 192.168.0.100:2003 melalui UDP
EthernetUDP Udp;
IPAddress remoteIP(192,168,0,100);
unsigned int remotePort = 2003;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600); 

  // Inisialisasi waktu dari DS1302
  rtc.writeProtect(false);
  rtc.halt(false);
  // The following lines can be commented out to use the values already stored in the DS1302
  // rtc.setDOW(TUESDAY);        // Set Day-of-Week to MONDAY
  // rtc.setTime(8,44,0);     // Set the time to 08:00:00 (24hr format)
  // rtc.setDate(1,3,2016);   // Set the date to Mar 1, 2016
  
  Time t = rtc.getTime();
  setTime(t.hour,t.min,t.sec,t.date,t.mon,t.year);
}

void loop() {
  // Time t = rtc.getTime();
  // time_t tm = now();
  Udp.beginPacket(remoteIP, remotePort);
  int sensorReading = analogRead(0);
  Udp.write("test.ds1302 ");
  Udp.print(sensorReading); 
  Udp.write(" ");
  Udp.println(now());
      
  Serial.write("test.ds1302 ");
  Serial.print(sensorReading); 
  Serial.write(" ");
  Serial.println(now());
  Udp.endPacket();
  delay(100);
}