Advertisement
Guest User

Untitled

a guest
Oct 29th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.87 KB | None | 0 0
  1. /* TDGino Engine Preheater Control
  2.  
  3. Sketch to control a Webasto BBW46 engine preheater and to send sms status messages
  4.  
  5. Created 2012
  6. by Bo Herrmannsen
  7.  
  8. This code is in the public domain.
  9.  
  10. The TDGino will need a extra input at pin 7, you will need 2 resistors and a optocoupler for this
  11.  
  12. */
  13.  
  14. #include <GSM.h>
  15. #include <OneWire.h>
  16. #include <DallasTemperature.h>
  17.  
  18. GSM gsm;
  19.  
  20.  
  21. // Set pin numbers:
  22. const int powergsm = 77;
  23. const int ctsgsm = 39;
  24. const int rtsgsm = 29;
  25. const int dcdgsm = 27;
  26. const int dtrgsm = 28;
  27. const int reset = 35;
  28. const int ring = 34;
  29. const int ld1 = 25;
  30. const int ld2 = 26;
  31. const int stato = 76;
  32. const int rele1 = 36;
  33. const int rele2 = 37;
  34. const int sda = 20;
  35. const int scl = 21;
  36. const int in1 = 84;
  37. const int in2 = 83;
  38. const int in3 = 7;
  39. const int stddtmf = 14;
  40. const int q1dtmf = 72;
  41. const int q2dtmf = 73;
  42. const int q3dtmf = 74;
  43. const int q4dtmf = 75;
  44. const int puls = 62;
  45. const int sonda = 63;
  46.  
  47.  
  48. #define ONE_WIRE_BUS sonda // Data wire is plugged into port 2 on the Arduino
  49.  
  50.  
  51. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  52.  
  53.  
  54. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
  55.  
  56. DeviceAddress insideThermometer = { 0x28, 0xF3, 0x19, 0x33, 0x03, 0x00, 0x00, 0x9E }; // Array to hold address for inside thermometer
  57. DeviceAddress outsideThermometer = { 0x28, 0xA0, 0x34, 0xC3, 0x03, 0x00, 0x00, 0x8C }; // Array to hold address for outside thermometer
  58. float tempC=0;
  59.  
  60. int Flame; // Status of pin connected to flame sensor (So we can detect if there is a flame or not)
  61. int FlameState; // Status of the flame sensor
  62. int Fan; // Status of pin connected to fan sensor (So we know when the fan is turned on by the heater)
  63. int FanState; // Status of the fan sensor
  64. 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)
  65. int HeatState; // Status of the heater sensor
  66. char phone_num[20]; // Array for the phone number string
  67. char string[160]; // Array for the sms, max 160 char's
  68. char inchar;
  69. int temperature1;
  70. int temperature2;
  71.  
  72.  
  73. void setup()
  74.  
  75. {
  76.  
  77. // set the digital pin as output:
  78. pinMode(powergsm, OUTPUT);
  79. pinMode(rtsgsm, OUTPUT);
  80. pinMode(dtrgsm, OUTPUT);
  81. pinMode(reset, OUTPUT);
  82. pinMode(ld1, OUTPUT);
  83. pinMode(ld2, OUTPUT);
  84. pinMode(rele1, OUTPUT);
  85. pinMode(rele2, OUTPUT);
  86. pinMode(sda, OUTPUT);
  87. pinMode(scl, OUTPUT);
  88. digitalWrite(rele1, LOW); // Set relay state to avoid false heater start on power up
  89. digitalWrite(rele2, LOW);
  90.  
  91. // set the digital pin as input:
  92. pinMode(ctsgsm, INPUT);
  93. pinMode(dcdgsm, INPUT);
  94. pinMode(ring, INPUT);
  95. pinMode(stato, INPUT);
  96. pinMode(in1, INPUT); // Flame on/off
  97. pinMode(in2, INPUT); // Heater on/off
  98. pinMode(in3, INPUT); // Cabin fan on/off
  99. pinMode(stddtmf, INPUT);
  100. pinMode(q1dtmf, INPUT);
  101. pinMode(q2dtmf, INPUT);
  102. pinMode(q3dtmf, INPUT);
  103. pinMode(q4dtmf, INPUT);
  104. pinMode(puls, INPUT); // Button on board, not used so far
  105. pinMode(sonda, INPUT); // DS18B20 one wire bus
  106.  
  107. digitalWrite(ld1, HIGH); // Light up LED1 to tell we are starting up
  108.  
  109. Serial.begin(9600); // Start serial port at 9600 bps
  110. Serial.println("System startup"); // Write on the serial/usb port that we are powering up
  111. gsm.TurnOn(115200); // Power the GSM module on
  112. Serial.println("GSM Module are powered up"); // Write on the serial/usb port that GSM Module are powered up
  113. gsm.InitParam(PARAM_SET_1); // Configure the module
  114. Serial.println("GSM Module are Configured"); // Write on the serial/usb port that GSM Module are configured
  115. gsm.Echo(1); // Enable AT echo
  116.  
  117. Serial.println("AT+CMGF=1"); // Set sms mode to text
  118. delay(30000);
  119. Serial.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt
  120. delay(400);
  121.  
  122. sensors.setResolution(insideThermometer, 12); // Set the resolution to 12 bit
  123. sensors.setResolution(outsideThermometer, 12);
  124. Serial.println("Temp. Sensors set to 12 Bit");
  125. FlameState = digitalRead(in1); // Read the initial states
  126. HeatState = digitalRead(in2);
  127. FanState = digitalRead(in3);
  128. digitalWrite(ld2, HIGH); // Light up LED2 to tell we are ready
  129.  
  130. }
  131.  
  132.  
  133. void loop()
  134.  
  135. {
  136.  
  137.  
  138. sensors.requestTemperatures();
  139.  
  140. temperature1 = sensors.getTempC(insideThermometer); // Read temp and store it in variable
  141. temperature2 = sensors.getTempC(outsideThermometer);
  142.  
  143. int response=0;
  144.  
  145. Flame = digitalRead(in1); // Read value of flamesensor pin and store it in Flame
  146. Heat = digitalRead(in2); // Read value of fan sensor pin and store it in Fan
  147. Fan = digitalRead(in3); // Read value of flame sensor pin and store it in Flame
  148.  
  149.  
  150. // ******************************************************** Flame Sensor Section Start ******************************************************
  151.  
  152.  
  153. if (Flame != FlameState ) // The state has changed!
  154. {
  155.  
  156. if (Flame == LOW)
  157. {
  158. sprintf(string,"Flame On");
  159. Serial.println(string);
  160. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  161. Serial.print("response ");
  162. Serial.println(response);
  163.  
  164. }
  165.  
  166. else
  167. {
  168. sprintf(string,"Flame Off");
  169. Serial.println(string);
  170. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  171. Serial.print("response ");
  172. Serial.println(response);
  173. }
  174. FlameState = Flame; // Save the new state in our variable
  175. }
  176.  
  177.  
  178. // ******************************************************** Flame Sensor Section End ********************************************************
  179.  
  180. // ******************************************************** Fan Sensor Section Start ********************************************************
  181.  
  182. if (Fan != FanState ) // The state has changed!
  183. {
  184.  
  185. if (Fan == LOW)
  186. {
  187. sprintf(string,"Fan On");
  188. Serial.println(string);
  189. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  190. Serial.print("response ");
  191. Serial.println(response);
  192.  
  193. }
  194.  
  195. else
  196. {
  197. sprintf(string,"Fan Off");
  198. Serial.println(string);
  199. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  200. Serial.print("response ");
  201. Serial.println(response);
  202. }
  203. FanState = Fan; // Save the new state in our variable
  204. }
  205.  
  206.  
  207. // ******************************************************** Fan Sensor Section End ***********************************************************
  208.  
  209. // ******************************************************** Heater Sensor Section Start ******************************************************
  210.  
  211. if (Heat != HeatState ) // The state has changed!
  212. {
  213.  
  214. if (Heat == LOW)
  215. {
  216. sprintf(string,"Heater On");
  217. Serial.println(string);
  218. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  219. Serial.print("response ");
  220. Serial.println(response);
  221.  
  222. }
  223.  
  224. else
  225. {
  226. sprintf(string,"Heater Off");
  227. Serial.println(string);
  228. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  229. Serial.print("response ");
  230. Serial.println(response);
  231. }
  232. HeatState = Heat; // Save the new state in our variable
  233. }
  234.  
  235.  
  236. // ******************************************************** Heater Sensor Section End ********************************************************
  237.  
  238. // ********************************************************Incoming SMS Section Start ********************************************************
  239.  
  240.  
  241. if(Serial1.available() >0) // If a character comes in from the cellular module...
  242. {
  243. inchar=Serial1.read();
  244. if (inchar=='@') // Control char, if sms does start with anything else nothing happens and the sms is deleted
  245. {
  246.  
  247. delay(10);
  248.  
  249. inchar=Serial1.read();
  250. if (inchar=='a')
  251. {
  252.  
  253. delay(10);
  254.  
  255. inchar=Serial1.read();
  256. if (inchar=='0')
  257. {
  258. digitalWrite(rele1, LOW); // If 0 after a then turn relay1 off
  259. Serial.println("Relay1 Off");
  260.  
  261. }
  262. else if (inchar=='1')
  263. {
  264. digitalWrite(rele1, HIGH); // If 1 after a then turn relay1 on
  265. Serial.println("Relay1 On");
  266. }
  267. }
  268.  
  269.  
  270. delay(10);
  271.  
  272.  
  273. if (inchar=='b')
  274. {
  275.  
  276. delay(10);
  277.  
  278. inchar=Serial1.read();
  279. if (inchar=='0')
  280. {
  281. digitalWrite(rele2, LOW); // If 0 after b then turn relay2 off
  282. Serial.println("Relay2 Off");
  283. }
  284. else if (inchar=='1')
  285. {
  286. digitalWrite(rele2, HIGH); // If 1 after a then turn relay2 on
  287. Serial.println("Relay2 On");
  288. }
  289. }
  290.  
  291. delay(10);
  292.  
  293.  
  294. if (inchar=='t')
  295. {
  296.  
  297. delay(10);
  298.  
  299. inchar=Serial1.read();
  300. if (inchar=='0') // If 0 after t then send text with cabin temp
  301. {
  302. int temperature1dec=temperature1;
  303. int temperature1uni=(temperature1*100)-(temperature1dec*100);
  304. sprintf(string,"Kabinetemperatur %d.%d Grader",temperature1dec,temperature1uni); // Text to send when requesting cabin temp leave the %d.%d part
  305. Serial.println(string);
  306. response=gsm.SendSMS("+45********",string); // +********** insert your telephone number
  307. Serial.print("response ");
  308. Serial.println(response);
  309. }
  310. else if (inchar=='1') // If 1 after t then send text with coolant temp
  311. {
  312. int temperature2dec=temperature2;
  313. int temperature2uni=(temperature2*100)-(temperature2dec*100);
  314. sprintf(string,"Kolertemperatur %d.%d Grader",temperature2dec,temperature2uni); // Text to send when requesting coolant temp leave the %d.%d part
  315. Serial.println(string);
  316. response=gsm.SendSMS("+45********",string); //+********** insert your telephone number
  317. Serial.print("response ");
  318. Serial.println(response);
  319. }
  320. }
  321. Serial1.println("AT+CMGD=1,4"); // Delete all SMS
  322. }
  323. }
  324. }
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333. // ********************************************************Incoming SMS Section End ********************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement