Mouamle

Processing code bit

Jun 11th, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import processing.serial.*;
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. Serial myPort;
  6. String inString = "null";
  7.  
  8. // the MLabel object // http://pastebin.com/M3eLpVMQ
  9.     MLabel l1;
  10.     MLabel status;
  11. // the MLabel object // http://pastebin.com/M3eLpVMQ
  12.  
  13. int value = 0;
  14.  
  15. void setup(){
  16.   size(450, 600);
  17.   myPort = new Serial(this, "COM9", 9600);
  18.   myPort.bufferUntil('\n');
  19.  
  20.   l1 = new MLabel("", 170, 200, 40, 120, 60, 20);  
  21.   status = new MLabel("st", 200, 80, 40, 120, 60, 20);
  22.   frameRate(60);
  23. }
  24.  
  25. void draw(){
  26.   background(39,174,96,255);
  27.   l1.render();
  28.   status.render();
  29.  
  30.   //l1.setText("B8E7B4FE");
  31.   status.setText(Integer.toString(value));
  32.  
  33. }
  34.  
  35.  
  36. void serialEvent(Serial p) {
  37.   String inString2 = p.readStringUntil('\n');
  38.  
  39.   if ( check(inString2, "F0B4BB43") || check(inString2, "37F4421F") || check(inString2,"E49BBA9A") ){
  40.     value++;
  41.     myPort.write(1);
  42.   }else if ( check(inString2, "45481702") || check(inString2, "765542FB") || check(inString2,"30C7A2FF") ){
  43.     value--;
  44.     myPort.write(2);
  45.   }
  46.  
  47.   if ( check(inString2, "B8E7B4FE") ){
  48.     String s = get("sendMessage?chat_id=121414901&text=" + Integer.toString(value));
  49.     if ( s.contains("\"ok\":true") ){
  50.      
  51.       Thread t = new Thread(
  52.         new Runnable(){
  53.            public void run(){
  54.              l1.setText("sent");
  55.              myPort.write(10);
  56.              try{ Thread.sleep(1000); }catch(Exception ex){}
  57.              l1.setText(" ");  
  58.            }
  59.         }
  60.       );
  61.       t.start();
  62.      
  63.     }
  64.   }
  65.   inString = inString2;
  66.   println(inString);
  67. }
  68.  
  69.  
  70. boolean check(String s, String code){
  71.   if ( s.contains(code) ) return true; return false;
  72. }
  73.  
  74.  
  75. public  String get(String method){
  76.       String inputLine = null;
  77.       try {
  78.             URL http = new URL("https://api.telegram.org/bot" + "Your bot token" +"/" + method);
  79.             URLConnection con = http.openConnection();
  80.             BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  81.             while ((inputLine = in.readLine()) != null)
  82.                 return inputLine;
  83.             in.close();
  84.     } catch (Exception e) {}
  85.     return inputLine;
  86. }
Add Comment
Please, Sign In to add comment