experiments with Pylontech/GroWatt PV tech
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

  1. /**
  2. * SyncArduinoClock.
  3. *
  4. * portIndex must be set to the port connected to the Arduino
  5. *
  6. * The current time is sent in response to request message from Arduino
  7. * or by clicking the display window
  8. *
  9. * The time message is 11 ASCII text characters; a header (the letter 'T')
  10. * followed by the ten digit system time (unix time)
  11. */
  12. import processing.serial.*;
  13. import java.util.Date;
  14. import java.util.Calendar;
  15. import java.util.GregorianCalendar;
  16. public static final short portIndex = 0; // select the com port, 0 is the first port
  17. public static final String TIME_HEADER = "T"; //header for arduino serial time message
  18. public static final char TIME_REQUEST = 7; // ASCII bell character
  19. public static final char LF = 10; // ASCII linefeed
  20. public static final char CR = 13; // ASCII linefeed
  21. Serial myPort; // Create object from Serial class
  22. void setup() {
  23. size(200, 200);
  24. println(Serial.list());
  25. println(" Connecting to -> " + Serial.list()[portIndex]);
  26. myPort = new Serial(this,Serial.list()[portIndex], 9600);
  27. println(getTimeNow());
  28. }
  29. void draw()
  30. {
  31. textSize(20);
  32. textAlign(CENTER);
  33. fill(0);
  34. text("Click to send\nTime Sync", 0, 75, 200, 175);
  35. if ( myPort.available() > 0) { // If data is available,
  36. char val = char(myPort.read()); // read it and store it in val
  37. if(val == TIME_REQUEST){
  38. long t = getTimeNow();
  39. sendTimeMessage(TIME_HEADER, t);
  40. }
  41. else
  42. {
  43. if(val == LF)
  44. ; //igonore
  45. else if(val == CR)
  46. println();
  47. else
  48. print(val); // echo everying but time request
  49. }
  50. }
  51. }
  52. void mousePressed() {
  53. sendTimeMessage( TIME_HEADER, getTimeNow());
  54. }
  55. void sendTimeMessage(String header, long time) {
  56. String timeStr = String.valueOf(time);
  57. myPort.write(header); // send header and time to arduino
  58. myPort.write(timeStr);
  59. myPort.write('\n');
  60. }
  61. long getTimeNow(){
  62. // java time is in ms, we want secs
  63. Date d = new Date();
  64. Calendar cal = new GregorianCalendar();
  65. long current = d.getTime()/1000;
  66. long timezone = cal.get(cal.ZONE_OFFSET)/1000;
  67. long daylight = cal.get(cal.DST_OFFSET)/1000;
  68. return current + timezone + daylight;
  69. }