Advertisement
Guest User

mill wall heater

a guest
Feb 28th, 2019
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. // mill kommandoer
  2. char settPower[] = {0x00, 0x10, 0x06, 0x00, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Powertoggle er pos 5
  3. char settTemp[] = {0x00, 0x10, 0x08, 0x00, 0x43, 0x04, 0x0A, 0x01, 0x12, 0x02}; //, 0x20, 0x03, 0x0A}; // temperatursetting er pos 8
  4.  
  5. sendCmd(settPower, sizeof(settPower), 0x01); // På
  6. sendCmd(settPower, sizeof(settPower), 0x00); // Av
  7.  
  8. sendCmd(settTemp, sizeof(settTemp), temperatur); // 5-35, men mills mikrokontroller aksepterer andre verdier. ;)
  9.  
  10. ---- Utklipp fra publiseringsfunksjon (mill kontroller -> mqtt ut) ---
  11.  
  12. if (receivedChars[4] == 0xC9 ) { // Filtrer ut unødig informasjon
  13.      
  14.       if (receivedChars[6] != 0 ) {
  15.         settpunkt = receivedChars[6], DEC;
  16.       }
  17.       if (receivedChars[7] != 0 ) {
  18.         temperatur = receivedChars[7], DEC;
  19.       }
  20.        if (strstr (power, "On")) {
  21.         if (receivedChars[11] == 0x00 ) {
  22.           sprintf(aktiv, "Idle");
  23.         } else if (receivedChars[11] == 0x01 ) {
  24.           sprintf(aktiv, "Heat");
  25.         }
  26.        }
  27.  
  28.  
  29.  
  30. --- Seriedata inn funksjon --------------
  31.  
  32. void recvWithStartEndMarkers() {
  33.     static boolean recvInProgress = false;
  34.     static byte ndx = 0;
  35.     char startMarker = 0x5A;
  36.     char endMarker = 0x5B;
  37.     char rc;
  38.  
  39.     while (Serial.available() > 0 && newData == false) {
  40.         rc = Serial.read();
  41.  
  42.         if (recvInProgress == true) {
  43.             if (rc != endMarker) {
  44.                 receivedChars[ndx] = rc;
  45.                 ndx++;
  46.                 if (ndx >= numChars) {
  47.                     ndx = numChars - 1;
  48.                 }
  49.             }
  50.             else {
  51.                 recvInProgress = false;
  52.                 ndx = 0;
  53.                 newData = true;
  54.             }
  55.         }
  56.  
  57.         else if (rc == startMarker) {
  58.             recvInProgress = true;
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. --- Seriedata ut til mill mikrokontroller ---
  65.  
  66. void sendCmd(char* arrayen, int len, int kommando) {
  67.    
  68.     if (arrayen[4] == 0x43){  // Temperatur
  69.       arrayen[8] = kommando;
  70.     }
  71.     if (arrayen[4] == 0x47){ // Power av/på
  72.         arrayen[5] = kommando;
  73.         arrayen[len] = (byte)0x00;  // Padding..
  74.     }
  75.     char crc = checksum(arrayen, len+1);
  76.     Serial.write((byte)0x5A); // Startbyte
  77.     for (int i = 0; i < len+1; i++) { // Beskjed
  78.       Serial.write((byte)arrayen[i]);
  79.     }
  80.     Serial.write((byte)crc); // Kontrollbyte
  81.     Serial.write((byte)0x5B); // Stoppbyte
  82. }
  83.  
  84. --- Funksjon for summering av kontrollbyte ---
  85.  
  86. unsigned char checksum(char *buf, int len) {
  87.  
  88.   unsigned char chk = 0;
  89.   for ( ; len != 0; len--) {
  90.     chk += *buf++;
  91.   }
  92.   return chk;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement