SHARE
TWEET

Leaf Climate Control

a guest Mar 2nd, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <mcp_can.h>
  2. #include <SPI.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. #define rxPin 7
  6. #define txPin 8
  7.  
  8. MCP_CAN CAN0(10);
  9.  
  10. int numbersCount = 2;
  11. String phoneNumbers[] = {"+48600123456", "+48500123456"};
  12. int phoneNumberLength = 12;
  13.  
  14. int climateControlThreshold = 15;
  15.  
  16. SoftwareSerial phone = SoftwareSerial(rxPin, txPin);
  17. int iterator = 0;
  18. int state = 0;
  19. unsigned long lastStateTimestamp = 0;
  20. boolean enabledClimateControl = false;
  21. unsigned long lastClipTimestamp = 0;
  22.  
  23. void setup()  
  24. {
  25.   Serial.begin(115200);
  26.  
  27.   if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
  28.   {
  29.     Serial.println("MCP2515 Initialized Successfully!");
  30.   }
  31.   else
  32.   {
  33.     Serial.println("Error Initializing MCP2515...");
  34.   }
  35.  
  36.   CAN0.setMode(MCP_NORMAL);
  37.  
  38.   pinMode(rxPin, INPUT);
  39.   pinMode(txPin, OUTPUT);
  40.   phone.begin(9600);
  41.   delay(5000);
  42.   phone.println("AT+CLIP=1");
  43. }
  44.  
  45. void loop(){
  46.   if (millis() - lastClipTimestamp > 86400000UL)
  47.   {
  48.     lastClipTimestamp = millis();
  49.     phone.println("AT+CLIP=1");
  50.   }
  51.   if ((enabledClimateControl) && (millis() - lastStateTimestamp > 60000UL * climateControlThreshold))
  52.   {
  53.     enabledClimateControl = !disableClimateControl();
  54.     Serial.println("Disabled climate control");    
  55.   }
  56.   if (phone.available())
  57.   {
  58.     int input = phone.read();
  59.     if (isStringMatch(input))
  60.     {
  61.       iterator++;
  62.     }
  63.     else
  64.     {
  65.       iterator = 0;
  66.     }
  67.     if (iterator == phoneNumberLength)
  68.     {
  69.       if (millis() - lastStateTimestamp > 7000)
  70.       {
  71.         state = 0;
  72.       }
  73.       lastStateTimestamp = millis();
  74.       state++;
  75.       if ((state == 1) && (enabledClimateControl))
  76.       {
  77.         phone.println("ATH");
  78.         enabledClimateControl = !disableClimateControl();
  79.         Serial.println("Disabled climate control");
  80.       }
  81.       if ((state == 3) && (!enabledClimateControl))
  82.       {
  83.         phone.println("ATH");
  84.         enabledClimateControl = enableClimateControl();
  85.         Serial.println("Enabled climate control");
  86.       }
  87.     }
  88.   }
  89. }
  90.  
  91. boolean isStringMatch(int input)
  92. {
  93.   for (int i = 0; i < numbersCount; i++)
  94.   {
  95.     if ((iterator < phoneNumberLength) && (phoneNumbers[i].charAt(iterator) == input))
  96.     {
  97.       return true;
  98.     }
  99.   }
  100.   return false;
  101. }
  102.  
  103. boolean enableClimateControl()
  104. {
  105.   byte dataWakeUp[1] = {0x00};
  106.   byte sndStat = CAN0.sendMsgBuf(0x68C, 0, 1, dataWakeUp);
  107.   if (sndStat != CAN_OK)
  108.   {
  109.     return false;
  110.   }
  111.  
  112.   delay(50);
  113.  
  114.   byte dataClimateEnable[1] = {0x4E};
  115.   sndStat = CAN0.sendMsgBuf(0x56E, 0, 1, dataClimateEnable);
  116.   if (sndStat != CAN_OK)
  117.   {
  118.     return false;
  119.   }
  120.  
  121.   return true;
  122. }
  123.  
  124. boolean disableClimateControl()
  125. {
  126.   byte dataClimateDisable[1] = {0x56};
  127.   return (CAN0.sendMsgBuf(0x56E, 0, 1, dataClimateDisable) == CAN_OK);
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top