參考:
1. http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/#
開箱/使用心得/技術文章
1. 在terminal以curl指令傳送json格式單次封包 至GELP HTTP input
=> (port 設為 12202)
ex. curl -XPOST http://140.116.163.151:12202/gelf -d ‘{“short_message”:”trytryRS”}’
ex. curl -XPOST http://140.116.163.151:12202/gelf -d ‘{“short_message”:”trytryRS”, “host”:”rongson”, “facility”:”rongsons mac”, “_test”:”hello”}’
2. 以tcp client傳送單次plaintext封包
=> (port 12203)
import java.io.*; import java.net.*; class TcpClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); Socket clientSocket = new Socket("140.116.163.151", 12203); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); System.out.print("Input: "); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); System.out.println("OVER"); //modifiedSentence = inFromServer.readLine(); //System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } }
3. udp client傳送單次plaintext封包
=> (port 12204)
import java.io.*; import java.net.*; // 1. 本程式必須與 UdpServer.java 程式搭配執行,先執行 UdpServer 再執行本程式。 // 2. 本程式必須有一個參數,指定伺服器的 IP。 // 用法範例: java UdpClient 127.0.0.1 public class udpClient extends Thread { int port; // port : 連接埠 InetAddress server; // InetAddress 是 IP, 此處的 server 指的是伺服器 IP String msg; // 欲傳送的訊息,每個 UdpClient 只能傳送一個訊息。 public static void main(String args[]) throws Exception { for (int i=0; i<100; i++) { // 建立 UdpClient,設定傳送對象與傳送訊息。 udpClient client = new udpClient(args[0], 12204, "UdpClient : "+i+"th message"); client.run(); // 啟動 UdpClient 開始傳送。 } } public udpClient(String pServer, int pPort, String pMsg) throws Exception { port = pPort; // 設定連接埠 server = InetAddress.getByName(pServer); // 將伺服器網址轉換為 IP。 msg = pMsg; // 設定傳送訊息。 } public void run() { try { byte buffer[] = msg.getBytes(); // 將訊息字串 msg 轉換為位元串。 // 封裝該位元串成為封包 DatagramPacket,同時指定傳送對象。 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, server, port); DatagramSocket socket = new DatagramSocket(); // 建立傳送的 UDP Socket。 socket.send(packet); // 傳送 socket.close(); // 關閉 UDP socket. } catch (Exception e) { e.printStackTrace(); } // 若有錯誤產生,列印函數呼叫堆疊。 } }
4. 以Java TCPClient 寫入Json格式 傳送到 graylog GELF TCP input
import java.io.*; import java.net.*; import org.json.*; class TcpClientJson { private String host; private int port; private Socket socket; private final String DEFAULT_HOST = "localhost"; public void connect(String host, int port) throws IOException { this.host = host; this.port = port; socket = new Socket(host, port); System.out.println("Client has been connected.."); } public JSONObject receiveJSON() throws IOException { InputStream in = socket.getInputStream(); ObjectInputStream i = new ObjectInputStream(in); JSONObject line = null; try { line = (JSONObject) i.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return line; } public void sendJSON() throws IOException { JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("RS", "Hello Java Json"); jsonObject2.put("host", "RS"); jsonObject2.put("facility", "test"); jsonObject2.put("_foo","bar"); jsonObject2.put("short_message","hello"); OutputStream out = socket.getOutputStream(); DataOutputStream o = new DataOutputStream(out); System.out.println("Hello sendJson"); o.writeBytes(jsonObject2.toString()+'\0'); System.out.println(jsonObject2.toString()); out.flush(); //System.out.println("Sent to server: " + " " + jsonObject2.get("key").toString()); } public static void main(String argv[]) throws Exception { TcpClientJson client = new TcpClientJson(); try{ client.connect("140.116.221.54", 12299); // For JSON call sendJSON(JSON json) & receiveJSON(); client.sendJSON(); //client.receiveJSON(); } catch (ConnectException e) { System.err.println(client.host + " connect refused"); return; } catch(UnknownHostException e){ System.err.println(client.host + " Unknown host"); client.host = client.DEFAULT_HOST; return; } catch (NoRouteToHostException e) { System.err.println(client.host + " Unreachable"); return; } catch (IllegalArgumentException e){ System.err.println(client.host + " wrong port"); return; } catch(IOException e){ System.err.println(client.host + ' ' + e.getMessage()); System.err.println(e); } finally { try { client.socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String getUserString(){ BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Input: "); String sentence="hello"; try{ sentence = inFromUser.readLine(); }catch(IOException e){ System.out.println(e); } return sentence; } }
* 記得在java tcpclient端傳送前,要在結尾加入+’\0’,否則graylog gelf tcp input會收到流量,但不會顯示資料
參考:https://github.com/Graylog2/graylog2-server/issues/643
還有part.2
http://rongson.twbbs.org/wordpress/sending-differen…g-server(part-2)/
1. 試貼java: pastie
———–
2. 產生html原始碼:https://tohtml.com/java/
import java.util.Scanner; import java.io.BufferedReader; class Hello{ private static String message; public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("你素隨?: "); message = input.nextLine(); String name= message; System.out.println("安捏! " + message + " 哩賀~~~~~"); System.out.print(message +" 來跟大家說一句祝福的話吧: "); message = input.nextLine(); System.out.println("yoyoyo, " + name + " 要跟大家說這些話,大家要認真聽哦!"); System.out.println(": "+ message); } public static void dosomething(){ } }
3. 換個, pastebin
4 . 再用個ideone
5. try內建外掛:Shortcodes Ultimate
6. try內建外掛:simple code highlighter
import java.util.Scanner; import java.io.BufferedReader; class Hello{ private static String message; public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("你素隨?: "); message = input.nextLine(); String name= message; System.out.println("安捏! " + message + " 哩賀~~~~~"); System.out.print(message +" 來跟大家說一句祝福的話吧: "); message = input.nextLine(); System.out.println("yoyoyo, " + name + " 要跟大家說這些話,大家要認真聽哦!"); System.out.println(": "+ message); } public static void dosomething(){ } }
有時候會在terminal下工作,切換到常用的資料夾時
不想用vim編輯時,就會想開finder,再用編輯器開啟檔案去做點事情
但開啟finder後,還要慢慢切到資料夾,超級麻煩!
1. 在terminal切好資料夾後,想開起finder直接到此資料夾:
=> open .
2. 在finder想切到terminal下工作:
=> 先開啟terminal,打好cd+space,再將finder的資料夾拖進去terminal,再按enter
1. Noun Project: https://thenounproject.com/search/?q=park
2. IconFinder: https://www.iconfinder.com
風格多樣,有彩色圖片,付費圖片頗多,但可勾選Free的搜尋
付費圖片會有陰影,所以不能直接抓圖
3. FlatIcon: http://www.flaticon.com
圖片風格多樣,大部分可以免費下載,
如果免費下載要做商業用途,記得通知作者、網站!
4. easyicon: http://www.easyicon.net
很可愛的網站,可以選icon要什麼顏色的,超酷!
個人覺得很實用~~~
5. Premiumpixel: http://www.premiumpixels.com/page/1/?s=icon
圖片風格精緻,但大小不能調整,下載下來為一張圖片,須自行擷取小張圖
參考:
1. icon 圖示免費下載!介面設計師用得到的9個免費圖庫:
http://www.playpcesor.com/2015/03/icon-free-download.html
參考:
Arduino 連接 DHT11感測空氣中的溫濕度,以光敏電阻感測是否有人經過,再以LCD顯示目前空氣溫濕度。
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define dht_dpin 2
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, NEGATIVE); // 設定 LCD I2C 位址
dht DHT;
int newPhotoValue, oldPhotoValue, diff;
bool flag=false;
void setup() {
lcd.begin(16, 2); // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光
lcd.clear();
lcd.backlight();
lcd.noCursor();
delay(1000);
lcd.setCursor(0, 0); // 設定游標位置在第一行行首
lcd.print(“Hello, world!”);
lcd.setCursor(0, 1); // 設定游標位置在第二行行首
lcd.print(“Shrimp”);
delay(1000);
lcd.clear();
}
void loop() {
//lcd.clear();
newPhotoValue = analogRead(A0);
//lcd.print(newPhotoValue);
if(flag){
lcd.setCursor(0,0);
lcd.print(“Welcome to RS’s”);
lcd.setCursor(0,1);
lcd.print(“Weather Station”);
flag = false;
}
diff= oldPhotoValue-newPhotoValue;
if(abs(diff)>15){
unsigned long time = millis();
while(millis() – time < 1000){
lcd.clear();
DHT.read11(dht_dpin);
lcd.setCursor(0,0);
lcd.print(“Humi: “);
lcd.print(DHT.humidity);
lcd.print(“%”);
lcd.setCursor(0,1);
lcd.print(“temp: “);
lcd.print(DHT.temperature);
lcd.print(“oC”);
delay(500);
}
flag= true;
}
delay(1000);
oldPhotoValue= newPhotoValue;
}
DHT11, LCD:
https://drive.google.com/folderview?id=0B9fmfEayrYvMRU0wejUyNUFGMkk&usp=sharing
參考:
1. 以arduino uno 燒錄 arduino pro mini:
http://sky4s.blogspot.tw/2014/04/arduino-unoarduino-pro-mini-use-arduino.html
補充: 燒錄時,板子記得選arduino pro mini
2. LCD I2C- G.T.WANG:
http://sky4s.blogspot.tw/2014/04/arduino-unoarduino-pro-mini-use-arduino.html
3. DHT11- 聰明人求知心切:
http://alex9ufoexploer.blogspot.tw/2013/04/arduino-humidity-sensor-dht11-tutorial_6.html
以Serial方式,傳送指令給arduino,若收到A,LED亮,收到B,LED暗
PS. 可以把LED換成繼電器(Relay)(可控制電器)
繼電器上的
NC: Normal Close,
C: Common,
NO: Normal Open
code:
int led = 2;
char incomingByte; // for incoming serial data
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
if(Serial.available())
{
incomingByte = Serial.read();
Serial.print(“I received: “);
Serial.println(incomingByte);
if(incomingByte== ‘A’){
digitalWrite(led, HIGH);
}
if(incomingByte== ‘B’){
digitalWrite(led, LOW);
}
}
}
– 在putty上讀取特殊鍵盤值:
若亮度有一定變化就會讓LED亮
code:
int led = 2;
static int oldValue = 0;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
int value = analogRead(A0);
int a = abs(value-oldValue);
Serial.println(a);
if(a > 50){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}
delay(50);
oldValue = value;
}