View difference between Paste ID: ueELQUwC and 78iG3aNK
SHOW: | | - or go back to the newest paste.
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
int Oldtemp;                                                                                                    // Temp since last flag
67
int Oldtemp1;
68
int Tempflag;                                                                                                   // Trigger variable (if temp has raised by X degrees)
69
char phone_num[20];                                                                                             // Array for the phone number string 
70
char string[160];                                                                                               // Array for the sms, max 160 char's
71
char inchar;
72
int temperature1;
73
int temperature2;
74
75
76
void setup() 
77
78
{
79
  // set the digital pin as output:
80
  pinMode(powergsm, OUTPUT);      
81
  pinMode(rtsgsm, OUTPUT); 
82
  pinMode(dtrgsm, OUTPUT); 
83
  pinMode(reset, OUTPUT); 
84
  pinMode(ld1, OUTPUT); 
85
  pinMode(ld2, OUTPUT); 
86
  pinMode(rele1, OUTPUT); 
87
  pinMode(rele2, OUTPUT); 
88
  pinMode(sda, OUTPUT); 
89
  pinMode(scl, OUTPUT); 
90
  digitalWrite(rele1, LOW);                                                                                     // Set relay state to avoid false heater start on power up
91
  digitalWrite(rele2, LOW);
92
  
93
  // set the digital pin as input:
94
  pinMode(ctsgsm, INPUT);      
95
  pinMode(dcdgsm, INPUT); 
96
  pinMode(ring, INPUT); 
97
  pinMode(stato, INPUT); 
98
  pinMode(in1, INPUT);                                                                                          // Flame on/off       
99
  pinMode(in2, INPUT);                                                                                          // Heater on/off      
100
  pinMode(in3, INPUT);                                                                                          // Cabin fan on/off
101
  pinMode(stddtmf, INPUT); 
102
  pinMode(q1dtmf, INPUT); 
103
  pinMode(q2dtmf, INPUT); 
104
  pinMode(q3dtmf, INPUT); 
105
  pinMode(q4dtmf, INPUT); 
106
  pinMode(puls, INPUT);                                                                                         // Button on board, not used so far
107
  pinMode(sonda, INPUT);                                                                                        // DS18B20 one wire bus
108
  
109
  digitalWrite(ld1, HIGH);                                                                                      // Light up LED1 to tell we are starting up
110
  
111
  
112
  Serial.begin(9600);                                                                                           // Start serial port at 9600 bps
113
  Serial.println("System startup");                                                                             // Write on the serial/usb port that we are powering up  
114
  gsm.TurnOn(115200);                                                                                           // Power the GSM module on
115
  Serial.println("GSM Module are powered up");                                                                  // Write on the serial/usb port that GSM Module are powered up
116
  gsm.InitParam(PARAM_SET_1);                                                                                   // Configure the module
117
  Serial.println("GSM Module are Configured");                                                                  // Write on the serial/usb port that GSM Module are configured  
118
  gsm.Echo(1);                                                                                                  // Enable AT echo 
119
  
120
  Serial.println("AT+CMGF=1");                                                                                  // Set sms mode to text
121
  delay(30000);
122
  Serial.println("AT+CNMI=3,3,0,0");                                                                            // Set module to send SMS data to serial out upon receipt
123
  delay(200);  
124
  
125
  delay(200);
126
                
127
  sensors.setResolution(insideThermometer, 12);                                                                 // Set the resolution to 12 bit 
128
  sensors.setResolution(outsideThermometer, 12);                                                           
129
  Serial.println("Temp. Sensors set to 12 Bit"); 
130
  FlameState = digitalRead(in1);                                                                                 // Read the initial states
131
  HeatState = digitalRead(in2);
132
  FanState = digitalRead(in3);                                                                           
133
  Tempflag = 0;                                                                          
134
  digitalWrite(ld2, HIGH);                                                                                      // Light up LED2 to tell we are ready
135
}
136
137
138
void loop()
139
140
141
142
{
143
  
144
  
145
  
146
sensors.requestTemperatures();
147
    
148
      temperature1 = sensors.getTempC(insideThermometer);                                                                                            // Read temp and store it in variable
149
      temperature2 = sensors.getTempC(outsideThermometer);
150
  
151
      int response=0;
152
153
Flame = digitalRead(in1);                                                                                                                             // Read value of flamesensor pin and store it in Flame
154
Heat = digitalRead(in2);                                                                                                                              // Read value of fan sensor pin and store it in Fan
155
Fan = digitalRead(in3);                                                                                                                            // Read value of flame sensor pin and store it in Flame   
156
157
  
158
// ******************************************************** Send Temp SMS Section Start ******************************************************    
159
    
160
 if(digitalRead(in2) == 0 && temperature1 > 50)                                                                                                      // If heater is on and coolant temp is above then send text. This is to let user know engine is now above "cold start" temp and ready. Here its set to 50 degree C
161
  
162
    {
163
     if (Tempflag = 1)
164
      
165
    {  
166
     
167
      sprintf(string,"Bil klar");                                                                                                                  // Engine Ready Text
168
      Serial.println(string);
169-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
169+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
170
      Serial.print("response ");
171
      Serial.println(response);
172
      Tempflag = 0;                                                                                                                                  // Tempflag set to 0 to avoid endless text's
173
    }
174
    }
175
// ******************************************************** Send Temp SMS Section End  ****************************************************** 
176
    
177
// ******************************************************** Flame Sensor Section Start ******************************************************
178
179
180
 if (Flame != FlameState )                                                                                                                           // The state has changed!
181
    {   
182
    
183
    if (Flame == LOW)   
184
      {
185
      sprintf(string,"Flame On");
186
      Serial.println(string);
187-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
187+
      response=gsm.SendSMS("++45********",string);                                                                                                    // +********** insert your telephone number
188
      Serial.print("response ");
189
      Serial.println(response);
190
191
    }
192
    
193
    else  
194
      {
195
      sprintf(string,"Flame Off");
196
      Serial.println(string);
197-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
197+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
198
      Serial.print("response ");
199
      Serial.println(response);
200
    }
201
    FlameState = Flame;                                                                                                                              // Save the new state in our variable
202
    }
203
    
204
    
205
// ******************************************************** Flame Sensor Section End ********************************************************
206
207
// ******************************************************** Fan Sensor Section Start ********************************************************
208
    
209
    if (Fan != FanState )                                                                                                                            // The state has changed!
210
    {   
211
    
212
    if (Fan == LOW)   
213
      {
214
      sprintf(string,"Fan On");
215
      Serial.println(string);
216-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
216+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
217
      Serial.print("response ");
218
      Serial.println(response);
219
220
    }
221
    
222
    else  
223
      {
224
      sprintf(string,"Fan Off");
225
      Serial.println(string);
226-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
226+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
227
      Serial.print("response ");
228
      Serial.println(response);
229
    }
230
    FanState = Fan;                                                                                                                                  // Save the new state in our variable
231
    }
232
    
233
    
234
// ******************************************************** Fan Sensor Section End ***********************************************************
235
    
236
// ******************************************************** Heater Sensor Section Start ******************************************************
237
238
     if (Heat != HeatState )                                                                                                                         // The state has changed!
239
    {   
240
    
241
    if (Heat == LOW)   
242
      {
243
      sprintf(string,"Heater On");
244
      Serial.println(string);
245-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
245+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
246
      Serial.print("response ");
247
      Serial.println(response);
248
249
    }
250
    
251
    else  
252
      {
253
      sprintf(string,"Heater Off");
254
      Serial.println(string);
255-
      response=gsm.SendSMS("+4560686615",string);                                                                                                    // +********** insert your telephone number
255+
      response=gsm.SendSMS("+45********",string);                                                                                                    // +********** insert your telephone number
256
      Serial.print("response ");
257
      Serial.println(response);
258
    }
259
    HeatState = Heat;                                                                                                                                // Save the new state in our variable
260
    }
261
    
262
 
263
// ******************************************************** Heater Sensor Section End ********************************************************   
264
    
265
// ********************************************************Incoming SMS Section Start ********************************************************
266
267
                                                                    
268
    if(Serial1.available() >0)                                                                                                                       // If a character comes in from the cellular module...
269
    {
270
      inchar=Serial1.read();    
271
      if (inchar=='@')                                                                                                                               // Control char, if sms does start with anything else nothing happens and the sms is deleted
272
      {
273
        
274
        delay(10);
275
        
276
        inchar=Serial1.read();  
277
        if (inchar=='a')
278
        {
279
        
280
          delay(10);
281
        
282
          inchar=Serial1.read();
283
          if (inchar=='0')
284
          {
285
            digitalWrite(rele1, LOW);                                                                                                                // If 0 after a then turn relay1 off
286
            Serial.println("Relay1 Off");
287
             Tempflag = 0;                                                                                                                           // Set tempflag to 0 to avoid multiple engine ready text's
288
          } 
289
          else if (inchar=='1')
290
          {
291
            digitalWrite(rele1, HIGH);                                                                                                               // If 1 after a then turn relay1 on
292
            Serial.println("Relay1 On");
293
             Tempflag = 1;                                                                                                                           // Set tempflag to 1 to enable engine ready text
294
          }
295
          }
296
          
297
          delay(10);
298
          
299
          inchar=Serial1.read();  
300
          if (inchar=='b')
301
          {
302
              inchar=Serial1.read();
303
            if (inchar=='0')
304
            {
305
              digitalWrite(rele2, LOW);                                                                                                              // If 0 after b then turn relay2 off
306
              Serial.println("Relay2 Off");
307
            } 
308
            else if (inchar=='1')
309
            {
310
              digitalWrite(rele2, HIGH);                                                                                                             // If 1 after a then turn relay2 on
311
              Serial.println("Relay2 On");
312
            }
313
            }
314
            
315
            delay(10);
316
          
317
          inchar=Serial1.read();  
318
          if (inchar=='t')
319
          {
320
              inchar=Serial1.read();
321
            if (inchar=='0')                                                                                                                         // If 0 after t then send text with cabin temp
322
            {
323
              int temperature1dec=temperature1;
324
              int temperature1uni=(temperature1*100)-(temperature1dec*100);
325
              sprintf(string,"Kabinetemperatur %d.%d Grader",temperature1dec,temperature1uni);
326
              Serial.println(string);
327-
              response=gsm.SendSMS("+4560686615",string);                                                                                            // +********** insert your telephone number
327+
              response=gsm.SendSMS("+45********",string);                                                                                            // +********** insert your telephone number
328
              Serial.print("response ");
329
              Serial.println(response);
330
            } 
331
            else if (inchar=='1')                                                                                                                    // If 1 after t then send text with coolant temp
332
            {
333
              int temperature2dec=temperature2;
334
              int temperature2uni=(temperature2*100)-(temperature2dec*100);
335
              sprintf(string,"Kølertemperatur %d.%d Grader",temperature2dec,temperature2uni);
336
              Serial.println(string);
337-
              response=gsm.SendSMS("+4560686615",string);                                                                                            //+********** insert your telephone number
337+
              response=gsm.SendSMS("+45********",string);                                                                                            //+********** insert your telephone number
338
              Serial.print("response ");
339
              Serial.println(response);
340
              }
341
              }
342
            Serial1.println("AT+CMGD=1,4");                                                                                                          // Delete all SMS
343
          }
344
        }
345
      }
346
    
347
348
// ********************************************************Incoming SMS Section End ********************************************************