Advertisement
Boelle

new webasto gsm remote control

Aug 8th, 2015
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.90 KB | None | 0 0
  1.  
  2. //hacked together by is0-mick 2015
  3.  
  4. #include "wbus.h"
  5. #include <GSM.h>
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. #include <SoftwareSerial.h>
  9.  
  10. GSM gsm;
  11.  
  12.  
  13. #define MINS_TO_RUN 15
  14. #define MAX_TEMP 75
  15.  
  16. #define XTAL 18432000
  17. #define TICKS_PER_SECOND 1000
  18. #define DIVIDE (XTAL/4/32/TICKS_PER_SECOND)
  19.  
  20. #define ON 1
  21. #define OFF 0
  22.  
  23. // Set pin numbers:
  24. const int powergsm = 77;
  25. const int ctsgsm = 39;
  26. const int rtsgsm = 29;
  27. const int dcdgsm = 27;
  28. const int dtrgsm = 28;
  29. const int reset = 35;
  30. const int ring = 34;
  31. const int ld1 = 25;
  32. const int ld2 = 26;
  33. const int stato = 76;
  34. const int rele1 = 36;
  35. const int rele2 = 37;
  36. const int sda = 20;
  37. const int scl = 21;
  38. const int in1 = 84;
  39. const int in2 = 83;
  40. const int in3 = 7;
  41. const int stddtmf = 14;
  42. const int q1dtmf = 72;
  43. const int q2dtmf = 73;
  44. const int q3dtmf = 74;
  45. const int q4dtmf = 75;
  46. const int puls = 62;
  47. const int sonda = 63;
  48.  
  49.  
  50. #define led_circ_pump 8
  51. #define tx_line 13 //pin for TX
  52. SoftwareSerial mySerial(12, 13); // RX, TX
  53.  
  54.  
  55. #define ONE_WIRE_BUS sonda // Data wire is plugged into port 2 on the Arduino
  56.  
  57.  
  58. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  59.  
  60.  
  61. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
  62.  
  63. DeviceAddress insideThermometer = { 0x28, 0xF3, 0x19, 0x33, 0x03, 0x00, 0x00, 0x9E }; // Array to hold address for inside thermometer
  64. DeviceAddress outsideThermometer = { 0x28, 0xA0, 0x34, 0xC3, 0x03, 0x00, 0x00, 0x8C }; // Array to hold address for outside thermometer
  65. float tempC=0;
  66.  
  67. #define RESPONSE_TIMEOUT (TICKS_PER_SECOND/2)
  68. #define ERROR_DELAY (1*TICKS_PER_SECOND)
  69. #define NACK_DELAY (5*TICKS_PER_SECOND)
  70. #define RESET_SLEEP (10*TICKS_PER_SECOND)
  71. #define ERROR_TMO_CHK_ADR 250
  72. #define ERROR_NACK 240
  73.  
  74. int Flame; // Status of pin connected to flame sensor (So we can detect if there is a flame or not)
  75. int FlameState; // Status of the flame sensor
  76. int Fan; // Status of pin connected to fan sensor (So we know when the fan is turned on by the heater)
  77. int FanState; // Status of the fan sensor
  78. int Heat; // Status of pin connected to heater(circulation pump) sensor (So we know if the heater is on - might seem odd but sometimes the heater stops early due to low fuel etc)
  79. int HeatState; // Status of the heater sensor
  80. char phone_num[20]; // Array for the phone number string
  81. char string[160]; // Array for the sms, max 160 char's
  82. char inchar;
  83. int temperature1;
  84. int temperature2;
  85.  
  86.  
  87.  
  88. int adc_key_in = 0;
  89. byte rs232_dummy, rs232_byte;
  90. byte bytes_count, cnt;
  91.  
  92. byte error, tries1, tries2;
  93.  
  94. byte msgbuffer[15], message_data[10], message_length;
  95. byte checksum_byte;
  96.  
  97. word rx_ticks, delay_ticks, pump_check_ticks, low_beam_ticks, sleep_ticks;
  98.  
  99. long active_time, post_active_time;
  100.  
  101. char tbs[32];//buffer for printf
  102. bool keepalive = false;
  103. int statustype = 0;
  104. unsigned long currm = millis();
  105. unsigned long prevm = 0;
  106.  
  107. void setup()
  108. {
  109.  
  110. // set the digital pin as output:
  111. pinMode(powergsm, OUTPUT);
  112. pinMode(rtsgsm, OUTPUT);
  113. pinMode(dtrgsm, OUTPUT);
  114. pinMode(reset, OUTPUT);
  115. pinMode(ld1, OUTPUT);
  116. pinMode(ld2, OUTPUT);
  117. pinMode(rele1, OUTPUT);
  118. pinMode(rele2, OUTPUT);
  119. pinMode(sda, OUTPUT);
  120. pinMode(scl, OUTPUT);
  121. digitalWrite(rele1, LOW); // Set relay state to avoid false heater start on power up
  122. digitalWrite(rele2, LOW);
  123.  
  124. // set the digital pin as input:
  125. pinMode(ctsgsm, INPUT);
  126. pinMode(dcdgsm, INPUT);
  127. pinMode(ring, INPUT);
  128. pinMode(stato, INPUT);
  129. pinMode(in1, INPUT); // Cabin fan on/off
  130. pinMode(in2, INPUT); // Heater on/off
  131. pinMode(in3, INPUT); // Flame on/off
  132. pinMode(stddtmf, INPUT);
  133. pinMode(q1dtmf, INPUT);
  134. pinMode(q2dtmf, INPUT);
  135. pinMode(q3dtmf, INPUT);
  136. pinMode(q4dtmf, INPUT);
  137. pinMode(puls, INPUT); // Button on board, not used so far
  138. pinMode(sonda, INPUT); // DS18B20 one wire bus
  139.  
  140. digitalWrite(ld1, HIGH); // Light up LED1 to tell we are starting up
  141.  
  142. Serial.begin(57600);//debug info on serial
  143.  
  144.  
  145. Serial.println("System startup"); // Write on the serial/usb port that we are powering up
  146. gsm.TurnOn(115200); // Power the GSM module on
  147. Serial.println("GSM Module are powered up"); // Write on the serial/usb port that GSM Module are powered up
  148. gsm.InitParam(PARAM_SET_1); // Configure the module
  149. Serial.println("GSM Module are Configured"); // Write on the serial/usb port that GSM Module are configured
  150. gsm.Echo(1); // Enable AT echo
  151.  
  152. Serial.println("AT+CMGF=1"); // Set sms mode to text
  153. delay(30000);
  154. Serial.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt
  155. delay(400);
  156.  
  157. sensors.setResolution(insideThermometer, 12); // Set the resolution to 12 bit
  158. sensors.setResolution(outsideThermometer, 12);
  159. Serial.println("Temp. Sensors set to 12 Bit");
  160.  
  161. FanState = 0; // Read the initial states
  162. HeatState = 0;
  163. FlameState = 0;
  164.  
  165. digitalWrite(ld2, HIGH); // Light up LED2 to tell we are ready
  166.  
  167. }
  168.  
  169.  
  170.  
  171. unsigned char checksum(unsigned char *buf, unsigned char len)
  172. {
  173. //Serial.print("length =");
  174. //Serial.println(len,DEC);
  175. unsigned char chk = 0;
  176. for ( ; len != 0; len--) {
  177. chk ^= *buf++;
  178. }
  179. //Serial.print("checksum = ");
  180. //Serial.println(chk,HEX);
  181. return chk;
  182. }
  183.  
  184. //--------------------------------------------------------------------------------
  185. void send_Serial_byte(byte value)
  186. {
  187. mySerial.write(value);
  188.  
  189. }
  190.  
  191. //--------------------------------------------------------------------------------
  192. byte read_Serial_byte(void)
  193. {
  194. if (mySerial.available() > 0)
  195. {
  196. rs232_byte = mySerial.read();
  197. return 1;
  198. }
  199. return 0;
  200. }
  201.  
  202. //--------------------------------------------------------------------------------
  203. void msg_send(byte cmd)
  204. {
  205. msgbuffer[0] = ((WBUS_CLIENT_ADDR << 4) | WBUS_HOST_ADDR);
  206. msgbuffer[1] = message_length + 2;
  207. msgbuffer[2] = cmd;
  208. for (cnt = 0, bytes_count = 3; cnt < message_length; cnt++, bytes_count++) msgbuffer[bytes_count] = message_data[cnt];
  209. // make checksum
  210. msgbuffer[bytes_count] = 0;//clear old checksum
  211. msgbuffer[bytes_count++] = checksum(msgbuffer, bytes_count);
  212. // Send message
  213. Serial.print("Sending: ");
  214. PrintHex8(msgbuffer, bytes_count);
  215. for (cnt = 0; cnt < bytes_count; cnt++) send_Serial_byte(msgbuffer[cnt]);
  216. mySerial.flush();
  217. // wait if any other transmission
  218. // and purge all receptions
  219. while (mySerial.available() > 0)
  220. {
  221. delay(1);
  222. byte waste = mySerial.read();
  223. }
  224. }
  225.  
  226. //--------------------------------------------------------------------------------
  227. void PrintHex8(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
  228. {
  229. for (int i = 0; i < length; i++) {
  230. Serial.print("0x");
  231. if (data[i] < 0x10) {
  232. Serial.print("0");
  233. }
  234. Serial.print(data[i], HEX);
  235. Serial.print(" ");
  236. }
  237. }
  238.  
  239. //--------------------------------------------------------------------------------
  240. byte msg_recv(byte cmd, byte skip)
  241. {
  242. Serial.print("recv: ");
  243. message_data[0] = 0;
  244. message_data[1] = 0;
  245. message_data[2] = 0;
  246. message_data[3] = 0;
  247. message_data[4] = 0;
  248. message_data[5] = 0;
  249. message_data[6] = 0;
  250. message_data[7] = 0;
  251. message_data[8] = 0;
  252. message_data[9] = 0;
  253. Serial.print("recv: 2");
  254. bytes_count = 0;
  255. unsigned long currentMillis = millis();
  256. unsigned long previousMillis = currentMillis;
  257. do
  258. {
  259. currentMillis = millis();
  260. if (read_Serial_byte() != 0)
  261. {
  262. msgbuffer[bytes_count] = rs232_byte;
  263. bytes_count++;
  264. previousMillis = currentMillis;
  265. }
  266. }
  267. while (currentMillis - previousMillis < RESPONSE_TIMEOUT && bytes_count < sizeof(msgbuffer));
  268. //
  269. // check timeout
  270. if (bytes_count == 0)
  271. {
  272. // Timeout
  273. Serial.println("Timeout");
  274.  
  275.  
  276.  
  277.  
  278.  
  279. return ERROR_TMO_CHK_ADR;
  280. }
  281. Serial.print("\nReceived: ");
  282. PrintHex8(msgbuffer, bytes_count);
  283. // check checksum
  284. if (checksum(msgbuffer, bytes_count) != 0)
  285. {
  286. // Checksum error
  287. Serial.println("Checksum error");
  288. return ERROR_TMO_CHK_ADR;
  289. }
  290. // check address
  291. if (msgbuffer[0] != ((WBUS_HOST_ADDR << 4) | WBUS_CLIENT_ADDR))
  292. {
  293. // Incorrect address
  294. Serial.println("Incorrect address");
  295. return ERROR_TMO_CHK_ADR;
  296. }
  297. // check validity
  298. if (msgbuffer[2] != (cmd | 0x80))
  299. {
  300. // Request was rejected...
  301. Serial.println("Request was rejected");
  302. message_length = 0;
  303. return ERROR_NACK;
  304. }
  305. // copy info to data
  306. message_length = msgbuffer[1] - 2 - skip;
  307. bytes_count = 3 + skip;
  308.  
  309. for (cnt = 0; cnt < message_length; cnt++, bytes_count++)
  310. {
  311. message_data[cnt] = msgbuffer[bytes_count];
  312.  
  313. }
  314. return 0;
  315. }
  316.  
  317. //--------------------------------------------------------------------------------
  318. //
  319. // Send a client W-Bus request and read answer from Heater.
  320. //
  321. void wb_io(byte cmd, byte skip)
  322. {
  323. tries2 = 0;
  324. do
  325. {
  326. if (tries2 != 0) delay(NACK_DELAY);
  327. tries1 = 0;
  328. do
  329. {
  330. if (tries1 != 0) delay(ERROR_DELAY);
  331. SendBreak(50);
  332. msg_send(cmd);
  333. // receive reply
  334. error = msg_recv(cmd, skip);
  335. tries1++;
  336. }
  337. while (tries1 < 10 && error == ERROR_TMO_CHK_ADR); // timeout, bad checksum or invalid address
  338. tries2++;
  339. }
  340. while (tries2 < 2 && error == ERROR_NACK); // request denied
  341. }
  342.  
  343. void SendBreak(byte duration)
  344. {
  345. Serial.println("\n**** BREAK SENT ****\n");
  346. mySerial.end(); // disable Serial1 transmission
  347. pinMode(tx_line, OUTPUT);
  348. digitalWrite(tx_line, 0); // set low the tx pin
  349. delay(duration); // wait 25 ms
  350. digitalWrite(tx_line, 1); // set high the tx pin
  351. delay(duration);
  352. mySerial.begin(2400, SERIAL_8E1); // re-enable Serial1 transmission
  353. delay(25);
  354. }
  355.  
  356. //--------------------------------------------------------------------------------
  357. //
  358. // Turn heater on for time minutes
  359. //
  360. void turnOn(byte time)
  361. {
  362. message_data[0] = time;
  363. message_length = 1;
  364. wb_io(WBUS_CMD_ON_PH, 0);
  365. keepalive=true;
  366. }
  367.  
  368. //--------------------------------------------------------------------------------
  369. //
  370. // Turn heater off
  371. //
  372. void turnOff(void)
  373. {
  374. message_length = 0;
  375. wb_io(WBUS_CMD_OFF, 0);
  376. keepalive=false;
  377. }
  378.  
  379. //--------------------------------------------------------------------------------
  380. //
  381. // keepAlive - poll the heater
  382. //
  383. void keepAlive()
  384. {
  385. message_data[0] = WBUS_CMD_ON_PH; // polling (keep-alive)
  386. message_data[1] = 0;
  387. message_length = 2;
  388. wb_io(WBUS_CMD_CHK, 0);
  389. if (error != 0) return;
  390. }
  391.  
  392. //--------------------------------------------------------------------------------
  393. //
  394. // get status 2
  395. //
  396. void get_status2(void)
  397. {
  398. message_data[0] = QUERY_STATUS2; // status 2 - level of devices
  399. message_length = 1;
  400. wb_io(WBUS_CMD_QUERY, 1);
  401. if (error != 0)
  402. {
  403. return;
  404. }
  405.  
  406. unsigned char glowplug_percentage = message_data[STA2_GP] / 28;//divide by 28 to give char 0-7 for bar display
  407. unsigned char fuelpump_percentage = message_data[STA2_FP] / 28;
  408. unsigned char airfan_percentage = message_data[STA2_CAF] / 28;
  409. unsigned char circpump_percentage = message_data[STA2_CP] / 28;
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417. //sprintf(tbs, "PLUG %c FPUMP %c" , glowplug_percentage,fuelpump_percentage);
  418. //lcd.print(tbs);
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425. }
  426.  
  427. //--------------------------------------------------------------------------------
  428. //
  429. // get status
  430. //
  431. void get_status(void)
  432. {
  433. message_data[0] = QUERY_SENSORS; // sensors - temperature, volts, flame detection, glow plug resistance
  434. message_length = 1;
  435. wb_io(WBUS_CMD_QUERY, 1); if (error != 0) return;
  436. int temp = message_data[SEN_TEMP] - 50;
  437. float voltage = ((message_data[SEN_VOLT] * 256) + message_data[SEN_VOLT + 1]) ;//in millivolts
  438. int FlameOn = message_data[SEN_FD];
  439. int watts = ((message_data[SEN_HE] * 256) + message_data[SEN_HE + 1]);
  440. int resistance = ((message_data[SEN_GPR] * 256) + message_data[SEN_GPR + 1]);
  441.  
  442.  
  443. Serial.println();
  444. sprintf(tbs, "%d%cC V=", temp, 223, voltage, FlameOn); //223 = degree sign
  445. Serial.println(tbs);
  446.  
  447.  
  448.  
  449. sprintf(tbs, " F=%d " , FlameOn);
  450.  
  451.  
  452. sprintf(tbs, "Watts=%d %d%c ", watts, resistance, 244); //244 = ohm sign
  453. Serial.println(tbs);
  454.  
  455. }
  456.  
  457.  
  458.  
  459. void loop()
  460. {
  461. currm = millis();
  462. if (currm - prevm > 2000)
  463. {
  464. prevm = currm;
  465. if (keepalive == true) keepAlive();
  466. switch (statustype)
  467. {
  468. case 1:
  469. case 2:
  470. get_status();
  471. break;
  472. case 3:
  473. case 4:
  474. get_status2();
  475. statustype = 0;
  476. break;
  477. }
  478. statustype++;
  479. }
  480.  
  481. sensors.requestTemperatures();
  482.  
  483. temperature1 = sensors.getTempC(insideThermometer); // Read temp and store it in variable
  484. temperature2 = sensors.getTempC(outsideThermometer);
  485.  
  486. int response=0;
  487.  
  488. Fan = 0; // Read value of fan sensor pin and store it in Fan
  489. Heat = 0; // Read value of heat sensor pin and store it in Heat
  490. Flame = 0;
  491.  
  492.  
  493. // ******************************************************** Flame Sensor Section Start ******************************************************
  494.  
  495.  
  496. if (Flame != FlameState ) // The state has changed!
  497. {
  498.  
  499. if (Flame == ?)
  500. {
  501. sprintf(string,"Flame Off");
  502. Serial.println(string);
  503. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  504. Serial.print("response ");
  505. Serial.println(response);
  506.  
  507. }
  508.  
  509. else
  510. {
  511. sprintf(string,"Flame On");
  512. Serial.println(string);
  513. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  514. Serial.print("response ");
  515. Serial.println(response);
  516. }
  517. FlameState = Flame; // Save the new state in our variable
  518. }
  519.  
  520.  
  521. // ******************************************************** Flame Sensor Section End ********************************************************
  522.  
  523. // ******************************************************** Fan Sensor Section Start ********************************************************
  524.  
  525. if (Fan != FanState ) // The state has changed!
  526. {
  527.  
  528. if (Fan == ?)
  529. {
  530. sprintf(string,"Fan On");
  531. Serial.println(string);
  532. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  533. Serial.print("response ");
  534. Serial.println(response);
  535.  
  536. }
  537.  
  538. else
  539. {
  540. sprintf(string,"Fan Off");
  541. Serial.println(string);
  542. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  543. Serial.print("response ");
  544. Serial.println(response);
  545. }
  546. FanState = Fan; // Save the new state in our variable
  547. }
  548.  
  549.  
  550. // ******************************************************** Fan Sensor Section End ***********************************************************
  551.  
  552. // ******************************************************** Heater Sensor Section Start ******************************************************
  553.  
  554. if (Heat != HeatState ) // The state has changed!
  555. {
  556.  
  557. if (Heat == ?)
  558. {
  559. sprintf(string,"Heater On");
  560. Serial.println(string);
  561. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  562. Serial.print("response ");
  563. Serial.println(response);
  564.  
  565. }
  566.  
  567. else
  568. {
  569. sprintf(string,"Heater Off");
  570. Serial.println(string);
  571. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  572. Serial.print("response ");
  573. Serial.println(response);
  574. }
  575. HeatState = Heat; // Save the new state in our variable
  576. }
  577.  
  578.  
  579. // ******************************************************** Heater Sensor Section End ********************************************************
  580.  
  581. // ********************************************************Incoming SMS Section Start ********************************************************
  582.  
  583.  
  584. if(Serial1.available() >0) // If a character comes in from the cellular module...
  585. {
  586. inchar=Serial1.read();
  587. if (inchar=='@') // Control char, if sms does start with anything else nothing happens and the sms is deleted
  588. {
  589.  
  590. delay(10);
  591.  
  592. inchar=Serial1.read();
  593. if (inchar=='a')
  594. {
  595.  
  596. delay(10);
  597.  
  598. inchar=Serial1.read();
  599. if (inchar=='0')
  600. {
  601. Serial.println("Shutting down heater");
  602. turnOff(); // If 0 after a then turn relay1 off
  603.  
  604. }
  605. else if (inchar=='1')
  606. {
  607. Serial.println("Starting heater");
  608. turnOn(50); // If 1 after a then turn relay1 on
  609.  
  610. }
  611. }
  612.  
  613.  
  614. delay(10);
  615.  
  616.  
  617.  
  618.  
  619. if (inchar=='t')
  620. {
  621.  
  622. delay(10);
  623.  
  624. inchar=Serial1.read();
  625. if (inchar=='0') // If 0 after t then send text with cabin temp
  626. {
  627. int temperature1dec=temperature1;
  628. int temperature1uni=(temperature1*100)-(temperature1dec*100);
  629. sprintf(string,"Kolertemperatur %d.%d Grader",temperature1dec,temperature1uni); // Text to send when requesting cabin temp leave the %d.%d part
  630. Serial.println(string);
  631. response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
  632. Serial.print("response ");
  633. Serial.println(response);
  634. }
  635. else if (inchar=='1') // If 1 after t then send text with coolant temp
  636. {
  637. int temperature2dec=temperature2;
  638. int temperature2uni=(temperature2*100)-(temperature2dec*100);
  639. sprintf(string,"Kabinetemperatur %d.%d Grader",temperature2dec,temperature2uni); // Text to send when requesting coolant temp leave the %d.%d part
  640. Serial.println(string);
  641. response=gsm.SendSMS("+4560686615",string); //+********** insert your telephone number
  642. Serial.print("response ");
  643. Serial.println(response);
  644. }
  645. }
  646. Serial1.println("AT+CMGD=1,4"); // Delete all SMS
  647. }
  648. }
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658. // ********************************************************Incoming SMS Section End ********************************************************
  659.  
  660.  
  661. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement