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
Line 1: Line 1:
 
==Code==
 
==Code==
 +
  
 
  /*
 
  /*
Line 21: Line 22:
 
  #include <Time.h>
 
  #include <Time.h>
 
  #include <DS1302.h>
 
  #include <DS1302.h>
 +
 +
// Create a DS1302 object.
 +
DS1302 rtc(8, 7, 6);
 
   
 
   
 
  // MAC & IP address Arduino
 
  // MAC & IP address Arduino
Line 27: Line 31:
 
  };
 
  };
 
  IPAddress ip(192, 168, 0, 4);
 
  IPAddress ip(192, 168, 0, 4);
  unsigned int localPort = 8888; // local port to listen on
+
  unsigned int localPort = 8888; // local port to listen on
 
   
 
   
 
  // Kirim ke graphite carbon cache di 192.168.0.100:2003 melalui UDP
 
  // Kirim ke graphite carbon cache di 192.168.0.100:2003 melalui UDP
Line 38: Line 42:
 
   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
Line 60: Line 64:
 
   Serial.println(now());
 
   Serial.println(now());
 
   Udp.endPacket();
 
   Udp.endPacket();
 +
  delay(500);
 
  }
 
  }

Revision as of 14:48, 29 February 2016

Code

/*
 UDPSendAnalogInput:
 
 Kirim pakai UDP ke 192.168.0.100:1000
 
 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);
  Time t = rtc.getTime();
  setTime(t.hour,t.min,t.sec,t.date,t.mon,t.year);
}

void loop() {
  Udp.beginPacket(remoteIP, remotePort);
  int sensorReading = analogRead(0);
  Udp.write("test.data ");
  Udp.print(sensorReading); 
  Udp.write(" ");
  Udp.println(now());
    
  Serial.write("test.data ");
  Serial.print(sensorReading); 
  Serial.write(" ");
  Serial.println(now());
  Udp.endPacket();
  delay(500);
}