Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1.  
  2. //hacked together by is0-mick 2015
  3.  
  4. #include "wbus.h"
  5.  
  6. #include "SIM900.h"
  7.  
  8. #include <SoftwareSerial.h>
  9.  
  10. #include "sms.h"
  11.  
  12. SMSGSM sms;
  13.  
  14. char number[]="3921234567";
  15.  
  16. char message[180];
  17.  
  18. byte stat;
  19.  
  20. char pos;
  21.  
  22. char *p;
  23.  
  24. int i=0;
  25.  
  26. #define MINS_TO_RUN 15
  27. #define MAX_TEMP 75
  28.  
  29. #define XTAL 18432000
  30. #define TICKS_PER_SECOND 1000
  31. #define DIVIDE (XTAL/4/32/TICKS_PER_SECOND)
  32.  
  33. #define ON 1
  34. #define OFF 0
  35.  
  36. #define led_circ_pump 8
  37. #define tx_line 18 //pin for TX
  38.  
  39.  
  40. #define RESPONSE_TIMEOUT (TICKS_PER_SECOND/2)
  41. #define ERROR_DELAY (1*TICKS_PER_SECOND)
  42. #define NACK_DELAY (5*TICKS_PER_SECOND)
  43. #define RESET_SLEEP (10*TICKS_PER_SECOND)
  44. #define ERROR_TMO_CHK_ADR 250
  45. #define ERROR_NACK 240
  46.  
  47.  
  48.  
  49. int adc_key_in = 0;
  50. byte rs232_dummy, rs232_byte;
  51. byte bytes_count, cnt;
  52.  
  53. byte error, tries1, tries2;
  54.  
  55. byte msgbuffer[15], message_data[10], message_length;
  56. byte checksum_byte;
  57.  
  58. word rx_ticks, delay_ticks, pump_check_ticks, low_beam_ticks, sleep_ticks;
  59.  
  60. long active_time, post_active_time;
  61. byte start_pressed, stop_pressed;
  62. char tbs[32];//buffer for printf
  63. bool keepalive = false;
  64. int statustype = 0;
  65. unsigned long currm = millis();
  66. unsigned long prevm = 0;
  67.  
  68. void setup()
  69. {
  70.  
  71. Serial.begin(9600);
  72.  
  73. if (gsm.begin(2400))
  74.  
  75. Serial.println("\nstatus=READY");
  76.  
  77. else Serial.println("\nstatus=IDLE");
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86. unsigned char checksum(unsigned char *buf, unsigned char len)
  87. {
  88. //Serial.print("length =");
  89. //Serial.println(len,DEC);
  90. unsigned char chk = 0;
  91. for ( ; len != 0; len--) {
  92. chk ^= *buf++;
  93. }
  94. //Serial.print("checksum = ");
  95. //Serial.println(chk,HEX);
  96. return chk;
  97. }
  98.  
  99. //--------------------------------------------------------------------------------
  100. void send_Serial_byte(byte value)
  101. {
  102. Serial1.write(value);
  103. }
  104.  
  105. //--------------------------------------------------------------------------------
  106. byte read_Serial_byte(void)
  107. {
  108. if (Serial1.available() > 0)
  109. {
  110. rs232_byte = Serial1.read();
  111. return 1;
  112. }
  113. return 0;
  114. }
  115.  
  116. //--------------------------------------------------------------------------------
  117. void msg_send(byte cmd)
  118. {
  119. msgbuffer[0] = ((WBUS_CLIENT_ADDR << 4) | WBUS_HOST_ADDR);
  120. msgbuffer[1] = message_length + 2;
  121. msgbuffer[2] = cmd;
  122. for (cnt = 0, bytes_count = 3; cnt < message_length; cnt++, bytes_count++) msgbuffer[bytes_count] = message_data[cnt];
  123. // make checksum
  124. msgbuffer[bytes_count] = 0;//clear old checksum
  125. msgbuffer[bytes_count++] = checksum(msgbuffer, bytes_count);
  126. // Send message
  127. Serial.print("Sending: ");
  128. PrintHex8(msgbuffer, bytes_count);
  129. for (cnt = 0; cnt < bytes_count; cnt++) send_Serial_byte(msgbuffer[cnt]);
  130. Serial1.flush();
  131. // wait if any other transmission
  132. // and purge all receptions
  133. while (Serial1.available() > 0)
  134. {
  135. delay(1);
  136. byte waste = Serial1.read();
  137. }
  138. }
  139.  
  140. //--------------------------------------------------------------------------------
  141. void PrintHex8(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
  142. {
  143. for (int i = 0; i < length; i++) {
  144. Serial.print("0x");
  145. if (data[i] < 0x10) {
  146. Serial.print("0");
  147. }
  148. Serial.print(data[i], HEX);
  149. Serial.print(" ");
  150. }
  151. }
  152.  
  153. //--------------------------------------------------------------------------------
  154. byte msg_recv(byte cmd, byte skip)
  155. {
  156. Serial.print("recv: ");
  157. message_data[0] = 0;
  158. message_data[1] = 0;
  159. message_data[2] = 0;
  160. message_data[3] = 0;
  161. message_data[4] = 0;
  162. message_data[5] = 0;
  163. message_data[6] = 0;
  164. message_data[7] = 0;
  165. message_data[8] = 0;
  166. message_data[9] = 0;
  167. Serial.print("recv: 2");
  168. bytes_count = 0;
  169. unsigned long currentMillis = millis();
  170. unsigned long previousMillis = currentMillis;
  171. do
  172. {
  173. currentMillis = millis();
  174. if (read_Serial_byte() != 0)
  175. {
  176. msgbuffer[bytes_count] = rs232_byte;
  177. bytes_count++;
  178. previousMillis = currentMillis;
  179. }
  180. }
  181. while (currentMillis - previousMillis < RESPONSE_TIMEOUT && bytes_count < sizeof(msgbuffer));
  182. //
  183. // check timeout
  184. if (bytes_count == 0)
  185. {
  186. // Timeout
  187. Serial.println("Timeout");
  188.  
  189. delay(500);
  190.  
  191. return ERROR_TMO_CHK_ADR;
  192. }
  193. Serial.print("\nReceived: ");
  194. PrintHex8(msgbuffer, bytes_count);
  195. // check checksum
  196. if (checksum(msgbuffer, bytes_count) != 0)
  197. {
  198. // Checksum error
  199. Serial.println("Checksum error");
  200. return ERROR_TMO_CHK_ADR;
  201. }
  202. // check address
  203. if (msgbuffer[0] != ((WBUS_HOST_ADDR << 4) | WBUS_CLIENT_ADDR))
  204. {
  205. // Incorrect address
  206. Serial.println("Incorrect address");
  207. return ERROR_TMO_CHK_ADR;
  208. }
  209. // check validity
  210. if (msgbuffer[2] != (cmd | 0x80))
  211. {
  212. // Request was rejected...
  213. Serial.println("Request was rejected");
  214. message_length = 0;
  215. return ERROR_NACK;
  216. }
  217. // copy info to data
  218. message_length = msgbuffer[1] - 2 - skip;
  219. bytes_count = 3 + skip;
  220.  
  221. for (cnt = 0; cnt < message_length; cnt++, bytes_count++)
  222. {
  223. message_data[cnt] = msgbuffer[bytes_count];
  224.  
  225. }
  226. return 0;
  227. }
  228.  
  229. //--------------------------------------------------------------------------------
  230. //
  231. // Send a client W-Bus request and read answer from Heater.
  232. //
  233. void wb_io(byte cmd, byte skip)
  234. {
  235. tries2 = 0;
  236. do
  237. {
  238. if (tries2 != 0) delay(NACK_DELAY);
  239. tries1 = 0;
  240. do
  241. {
  242. if (tries1 != 0) delay(ERROR_DELAY);
  243. SendBreak(50);
  244. msg_send(cmd);
  245. // receive reply
  246. error = msg_recv(cmd, skip);
  247. tries1++;
  248. }
  249. while (tries1 < 10 && error == ERROR_TMO_CHK_ADR); // timeout, bad checksum or invalid address
  250. tries2++;
  251. }
  252. while (tries2 < 2 && error == ERROR_NACK); // request denied
  253. }
  254.  
  255. void SendBreak(byte duration)
  256. {
  257. Serial.println("\n**** BREAK SENT ****\n");
  258. Serial1.end(); // disable Serial1 transmission
  259. pinMode(tx_line, OUTPUT);
  260. digitalWrite(tx_line, 0); // set low the tx pin
  261. delay(duration); // wait 25 ms
  262. digitalWrite(tx_line, 1); // set high the tx pin
  263. delay(duration);
  264. Serial1.begin(2400, SERIAL_8E1); // re-enable Serial1 transmission
  265. delay(25);
  266. }
  267.  
  268. //--------------------------------------------------------------------------------
  269. //
  270. // Turn heater on for time minutes
  271. //
  272. void turnOn(byte time)
  273. {
  274. message_data[0] = time;
  275. message_length = 1;
  276. wb_io(WBUS_CMD_ON_PH, 0);
  277. keepalive=true;
  278. }
  279.  
  280. //--------------------------------------------------------------------------------
  281. //
  282. // Turn heater off
  283. //
  284. void turnOff(void)
  285. {
  286. message_length = 0;
  287. wb_io(WBUS_CMD_OFF, 0);
  288. keepalive=false;
  289. }
  290.  
  291. //--------------------------------------------------------------------------------
  292. //
  293. // keepAlive - poll the heater
  294. //
  295. void keepAlive()
  296. {
  297. message_data[0] = WBUS_CMD_ON_PH; // polling (keep-alive)
  298. message_data[1] = 0;
  299. message_length = 2;
  300. wb_io(WBUS_CMD_CHK, 0);
  301. if (error != 0) return;
  302. }
  303.  
  304. //--------------------------------------------------------------------------------
  305. //
  306. // get status 2
  307. //
  308. void get_status2(void)
  309. {
  310. message_data[0] = QUERY_STATUS2; // status 2 - level of devices
  311. message_length = 1;
  312. wb_io(WBUS_CMD_QUERY, 1);
  313. if (error != 0)
  314. {
  315. return;
  316. }
  317.  
  318. unsigned char glowplug_percentage = message_data[STA2_GP] / 28;//divide by 28 to give char 0-7 for bar display
  319. unsigned char fuelpump_percentage = message_data[STA2_FP] / 28;
  320. unsigned char airfan_percentage = message_data[STA2_CAF] / 28;
  321. unsigned char circpump_percentage = message_data[STA2_CP] / 28;
  322.  
  323. lcd.setCursor(0, 0);
  324. lcd.print("PLUG ");
  325. lcd.write(glowplug_percentage);
  326. lcd.print(" FPUMP ");
  327. lcd.write(fuelpump_percentage);
  328. lcd.print(" ");
  329. //sprintf(tbs, "PLUG %c FPUMP %c" , glowplug_percentage,fuelpump_percentage);
  330. //lcd.print(tbs);
  331. lcd.setCursor(0, 1);
  332. lcd.print("FAN ");
  333. lcd.write(airfan_percentage);
  334. lcd.print(" CPUMP ");
  335. lcd.write(circpump_percentage);
  336. lcd.print(" ");
  337. }
  338.  
  339. //--------------------------------------------------------------------------------
  340. //
  341. // get status
  342. //
  343. void get_status(void)
  344. {
  345. message_data[0] = QUERY_SENSORS; // sensors - temperature, volts, flame detection, glow plug resistance
  346. message_length = 1;
  347. wb_io(WBUS_CMD_QUERY, 1); if (error != 0) return;
  348. int temp = message_data[SEN_TEMP] - 50;
  349. float voltage = ((message_data[SEN_VOLT] * 256) + message_data[SEN_VOLT + 1]) ;//in millivolts
  350. int FlameOn = message_data[SEN_FD];
  351. int watts = ((message_data[SEN_HE] * 256) + message_data[SEN_HE + 1]);
  352. int resistance = ((message_data[SEN_GPR] * 256) + message_data[SEN_GPR + 1]);
  353.  
  354. lcd.setCursor(0, 0);
  355. Serial.println();
  356. sprintf(tbs, "%d%cC V=", temp, 223, voltage, FlameOn); //223 = degree sign
  357. Serial.println(tbs);
  358.  
  359. lcd.print(tbs);
  360. lcd.print(voltage / 1000);//to give volts
  361. sprintf(tbs, " F=%d " , FlameOn);
  362. lcd.print(tbs);
  363. lcd.setCursor(0, 1);
  364. sprintf(tbs, "Watts=%d %d%c ", watts, resistance, 244); //244 = ohm sign
  365. Serial.println(tbs);
  366. lcd.print(tbs);
  367. }
  368.  
  369.  
  370. void loop()
  371. {
  372.  
  373. number[0]='\0';
  374.  
  375. pos=sms.IsSMSPresent(SMS_UNREAD);
  376.  
  377. Serial.println((int)pos);
  378.  
  379. if((int)pos>0&&(int)pos<=20){
  380.  
  381. Serial.print("Sms detected at pos ");
  382.  
  383. Serial.println((int)pos);
  384.  
  385. message[0]='\0';
  386.  
  387. stat=sms.GetAuthorizedSMS((int)pos,number,message,180,8,10);
  388.  
  389. if(stat==GETSMS_AUTH_SMS){
  390.  
  391. p=strstr(message,"Start");
  392.  
  393. if(p){
  394.  
  395. Serial.println("Starting");
  396.  
  397.  
  398.  
  399. turnOn(30);//30 mins
  400.  
  401. }
  402.  
  403. else{
  404.  
  405. p=strstr(message,"Stop");
  406.  
  407. if(p){
  408.  
  409. Serial.println("Stopping");
  410.  
  411. turnOff();
  412.  
  413. }
  414.  
  415. }
  416.  
  417. else{
  418.  
  419. p=strstr(message,"status1");
  420.  
  421. if(p){
  422.  
  423. Serial.println("Getting status 1 data");
  424.  
  425. get_status();
  426.  
  427.  
  428.  
  429.  
  430. Serial.println("Sending status 1 data via sms");
  431.  
  432. sms.SendSMS(number,message);
  433.  
  434. }
  435.  
  436. }
  437.  
  438. else{
  439.  
  440. p=strstr(message,"status2");
  441.  
  442. if(p){
  443.  
  444. Serial.println("Getting status 2 data");
  445.  
  446. get_status2();
  447.  
  448.  
  449.  
  450. Serial.println("Sending status 2 data via sms");
  451.  
  452. sms.SendSMS(number,message);
  453.  
  454.  
  455. }
  456.  
  457. }
  458.  
  459. sms.DeleteSMS((int)pos);
  460.  
  461. }
  462. }
  463.  
  464. delay(5000);
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement