Sending different data types and different ways to graylog server(part. 1)

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”}’

1Graylog_Web_Interface

 

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();
    }
}

tcp

Graylog_Web_Interface

 

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(); }    // 若有錯誤產生,列印函數呼叫堆疊。
    }
}

Graylog_Web_Interface

 

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;
    }
}

Graylog_Web_Interface

* 記得在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)/

Using differant platforms and wordpress plugins to display codes

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(){

	}
}

How to swtich between finder & terminal in Mac OS X?

有時候會在terminal下工作,切換到常用的資料夾時

不想用vim編輯時,就會想開finder,再用編輯器開啟檔案去做點事情

但開啟finder後,還要慢慢切到資料夾,超級麻煩!

 

1. 在terminal切好資料夾後,想開起finder直接到此資料夾:

=> open .

 

2. 在finder想切到terminal下工作:

=> 先開啟terminal,打好cd+space,再將finder的資料夾拖進去terminal,再按enter

 

App Icon素材下載

1. Noun Project: https://thenounproject.com/search/?q=park

黑白簡易的icon,適合做概念圖Noun_Project_Search

2. IconFinder: https://www.iconfinder.com

風格多樣,有彩色圖片,付費圖片頗多,但可勾選Free的搜尋

付費圖片會有陰影,所以不能直接抓圖

Document_icons_-_Download_29301_free__amp__premium_icons_on_Iconfinder

3. FlatIcon: http://www.flaticon.com

圖片風格多樣,大部分可以免費下載,

如果免費下載要做商業用途,記得通知作者、網站!

推~~~~佛心網站
Search_results_for_Document_-_Flaticon

4. easyicon: http://www.easyicon.net

很可愛的網站,可以選icon要什麼顏色的,超酷!

個人覺得很實用~~~

document_PNG、ICO、ICNS_Icons_search_and_download_easyicon_net

5. Premiumpixel: http://www.premiumpixels.com/page/1/?s=icon

圖片風格精緻,但大小不能調整,下載下來為一張圖片,須自行擷取小張圖

Search_Results_icon

參考:

1. icon 圖示免費下載!介面設計師用得到的9個免費圖庫:
http://www.playpcesor.com/2015/03/icon-free-download.html

如何利用wordpress架網站(全)

  1. 申請免費空間(Free Hostia)
    https://www.freehostia.com
  2. 申請免費網域(Twbbs.org)
    http://twbbs.org/?q=bbs_register
    設定‧修改_網域___TWBBS_org_自由網域
  3. 利用免費空間安裝網站、同時安裝一個MySQL Server(Free Hostia-> WordPress)
    => 安裝器-> 程序安裝器-> 安裝wordpress
    程序安裝器___Free_Hostia_控制面板
  4. 將網域導向自己的網站
    => 在twbbs.org內申請完後,需等待一天左右的審核時間
    => 我的域名-> 以寄存的域名
    寄存的域名___Free_Hostia_控制面板
  5. 利用wordpress設定網站!
    => rongson.twbbs.org/wordpress/wp_admin
  6. 再來如果只是要經營部落格,就不需要管Free Hostia 和 Twbbs了,
    只需要直接進到你的網站後台(rongson.twbbs.org/wordpress)就好
  7. 從google blogger搬家到wordpress,先從blogger匯出xml檔,再從wordpress後台以xml檔匯入

參考:

  1. 申請Free Hostia教學:老牌穩定免費空間申請教學- 香腸炒魷魚
    https://sofree.cc/sinup-freehostia/
  2. 申請Twbbs.org教學:如何申請免費自由網域twbbs.org?- 香腸炒魷魚
    https://sofree.cc/twbbs-org/
  3. 完整wordpress架站教學:[懶人包] WordPress 架站教學:安裝/搬家/空間/設定/佈景/外掛/優化/安全/常見問題https://sofun.tw/wordpress-tech/#WordPress-Setup

Arduino Project: Weather station (小型氣象站)

Arduino 連接 DHT11感測空氣中的溫濕度,以光敏電阻感測是否有人經過,再以LCD顯示目前空氣溫濕度。

Arduino:

#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;
}

library:

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

Arduino: Serial

以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上讀取特殊鍵盤值:

int led = 2;
int 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(“received!:  “);
     Serial.println(incomingByte);
    if(incomingByte== 65){
      Serial.println(“catch up”);
    }
    if(incomingByte== 66){
      Serial.println(“catch down”);
    }
    if(incomingByte== 67){
      Serial.println(“catch right”);
    }
    if(incomingByte== 68){
      Serial.println(“catch left”);
    }
    
  }
}
參考:
1. http://www.rebol.com/docs/core23/rebolcore-18.html


Arduino: 光敏電阻

若亮度有一定變化就會讓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;

}