Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //hacked together by is0-mick 2015
- #include "wbus.h"
- #include <GSM.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <SoftwareSerial.h>
- GSM gsm;
- #define MINS_TO_RUN 15
- #define MAX_TEMP 75
- #define XTAL 18432000
- #define TICKS_PER_SECOND 1000
- #define DIVIDE (XTAL/4/32/TICKS_PER_SECOND)
- #define ON 1
- #define OFF 0
- // Set pin numbers:
- const int powergsm = 77;
- const int ctsgsm = 39;
- const int rtsgsm = 29;
- const int dcdgsm = 27;
- const int dtrgsm = 28;
- const int reset = 35;
- const int ring = 34;
- const int ld1 = 25;
- const int ld2 = 26;
- const int stato = 76;
- const int rele1 = 36;
- const int rele2 = 37;
- const int sda = 20;
- const int scl = 21;
- const int in1 = 84;
- const int in2 = 83;
- const int in3 = 7;
- const int stddtmf = 14;
- const int q1dtmf = 72;
- const int q2dtmf = 73;
- const int q3dtmf = 74;
- const int q4dtmf = 75;
- const int puls = 62;
- const int sonda = 63;
- #define led_circ_pump 8
- #define tx_line 13 //pin for TX
- SoftwareSerial mySerial(12, 13); // RX, TX
- #define ONE_WIRE_BUS sonda // Data wire is plugged into port 2 on the Arduino
- OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
- DeviceAddress insideThermometer = { 0x28, 0xF3, 0x19, 0x33, 0x03, 0x00, 0x00, 0x9E }; // Array to hold address for inside thermometer
- DeviceAddress outsideThermometer = { 0x28, 0xA0, 0x34, 0xC3, 0x03, 0x00, 0x00, 0x8C }; // Array to hold address for outside thermometer
- float tempC=0;
- #define RESPONSE_TIMEOUT (TICKS_PER_SECOND/2)
- #define ERROR_DELAY (1*TICKS_PER_SECOND)
- #define NACK_DELAY (5*TICKS_PER_SECOND)
- #define RESET_SLEEP (10*TICKS_PER_SECOND)
- #define ERROR_TMO_CHK_ADR 250
- #define ERROR_NACK 240
- int Flame; // Status of pin connected to flame sensor (So we can detect if there is a flame or not)
- int FlameState; // Status of the flame sensor
- int Fan; // Status of pin connected to fan sensor (So we know when the fan is turned on by the heater)
- int FanState; // Status of the fan sensor
- 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)
- int HeatState; // Status of the heater sensor
- char phone_num[20]; // Array for the phone number string
- char string[160]; // Array for the sms, max 160 char's
- char inchar;
- int temperature1;
- int temperature2;
- int adc_key_in = 0;
- byte rs232_dummy, rs232_byte;
- byte bytes_count, cnt;
- byte error, tries1, tries2;
- byte msgbuffer[15], message_data[10], message_length;
- byte checksum_byte;
- word rx_ticks, delay_ticks, pump_check_ticks, low_beam_ticks, sleep_ticks;
- long active_time, post_active_time;
- char tbs[32];//buffer for printf
- bool keepalive = false;
- int statustype = 0;
- unsigned long currm = millis();
- unsigned long prevm = 0;
- void setup()
- {
- // set the digital pin as output:
- pinMode(powergsm, OUTPUT);
- pinMode(rtsgsm, OUTPUT);
- pinMode(dtrgsm, OUTPUT);
- pinMode(reset, OUTPUT);
- pinMode(ld1, OUTPUT);
- pinMode(ld2, OUTPUT);
- pinMode(rele1, OUTPUT);
- pinMode(rele2, OUTPUT);
- pinMode(sda, OUTPUT);
- pinMode(scl, OUTPUT);
- digitalWrite(rele1, LOW); // Set relay state to avoid false heater start on power up
- digitalWrite(rele2, LOW);
- // set the digital pin as input:
- pinMode(ctsgsm, INPUT);
- pinMode(dcdgsm, INPUT);
- pinMode(ring, INPUT);
- pinMode(stato, INPUT);
- pinMode(in1, INPUT); // Cabin fan on/off
- pinMode(in2, INPUT); // Heater on/off
- pinMode(in3, INPUT); // Flame on/off
- pinMode(stddtmf, INPUT);
- pinMode(q1dtmf, INPUT);
- pinMode(q2dtmf, INPUT);
- pinMode(q3dtmf, INPUT);
- pinMode(q4dtmf, INPUT);
- pinMode(puls, INPUT); // Button on board, not used so far
- pinMode(sonda, INPUT); // DS18B20 one wire bus
- digitalWrite(ld1, HIGH); // Light up LED1 to tell we are starting up
- Serial.begin(57600);//debug info on serial
- Serial.println("System startup"); // Write on the serial/usb port that we are powering up
- gsm.TurnOn(115200); // Power the GSM module on
- Serial.println("GSM Module are powered up"); // Write on the serial/usb port that GSM Module are powered up
- gsm.InitParam(PARAM_SET_1); // Configure the module
- Serial.println("GSM Module are Configured"); // Write on the serial/usb port that GSM Module are configured
- gsm.Echo(1); // Enable AT echo
- Serial.println("AT+CMGF=1"); // Set sms mode to text
- delay(30000);
- Serial.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt
- delay(400);
- sensors.setResolution(insideThermometer, 12); // Set the resolution to 12 bit
- sensors.setResolution(outsideThermometer, 12);
- Serial.println("Temp. Sensors set to 12 Bit");
- FanState = 0; // Read the initial states
- HeatState = 0;
- FlameState = 0;
- digitalWrite(ld2, HIGH); // Light up LED2 to tell we are ready
- }
- unsigned char checksum(unsigned char *buf, unsigned char len)
- {
- //Serial.print("length =");
- //Serial.println(len,DEC);
- unsigned char chk = 0;
- for ( ; len != 0; len--) {
- chk ^= *buf++;
- }
- //Serial.print("checksum = ");
- //Serial.println(chk,HEX);
- return chk;
- }
- //--------------------------------------------------------------------------------
- void send_Serial_byte(byte value)
- {
- mySerial.write(value);
- }
- //--------------------------------------------------------------------------------
- byte read_Serial_byte(void)
- {
- if (mySerial.available() > 0)
- {
- rs232_byte = mySerial.read();
- return 1;
- }
- return 0;
- }
- //--------------------------------------------------------------------------------
- void msg_send(byte cmd)
- {
- msgbuffer[0] = ((WBUS_CLIENT_ADDR << 4) | WBUS_HOST_ADDR);
- msgbuffer[1] = message_length + 2;
- msgbuffer[2] = cmd;
- for (cnt = 0, bytes_count = 3; cnt < message_length; cnt++, bytes_count++) msgbuffer[bytes_count] = message_data[cnt];
- // make checksum
- msgbuffer[bytes_count] = 0;//clear old checksum
- msgbuffer[bytes_count++] = checksum(msgbuffer, bytes_count);
- // Send message
- Serial.print("Sending: ");
- PrintHex8(msgbuffer, bytes_count);
- for (cnt = 0; cnt < bytes_count; cnt++) send_Serial_byte(msgbuffer[cnt]);
- mySerial.flush();
- // wait if any other transmission
- // and purge all receptions
- while (mySerial.available() > 0)
- {
- delay(1);
- byte waste = mySerial.read();
- }
- }
- //--------------------------------------------------------------------------------
- void PrintHex8(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
- {
- for (int i = 0; i < length; i++) {
- Serial.print("0x");
- if (data[i] < 0x10) {
- Serial.print("0");
- }
- Serial.print(data[i], HEX);
- Serial.print(" ");
- }
- }
- //--------------------------------------------------------------------------------
- byte msg_recv(byte cmd, byte skip)
- {
- Serial.print("recv: ");
- message_data[0] = 0;
- message_data[1] = 0;
- message_data[2] = 0;
- message_data[3] = 0;
- message_data[4] = 0;
- message_data[5] = 0;
- message_data[6] = 0;
- message_data[7] = 0;
- message_data[8] = 0;
- message_data[9] = 0;
- Serial.print("recv: 2");
- bytes_count = 0;
- unsigned long currentMillis = millis();
- unsigned long previousMillis = currentMillis;
- do
- {
- currentMillis = millis();
- if (read_Serial_byte() != 0)
- {
- msgbuffer[bytes_count] = rs232_byte;
- bytes_count++;
- previousMillis = currentMillis;
- }
- }
- while (currentMillis - previousMillis < RESPONSE_TIMEOUT && bytes_count < sizeof(msgbuffer));
- //
- // check timeout
- if (bytes_count == 0)
- {
- // Timeout
- Serial.println("Timeout");
- return ERROR_TMO_CHK_ADR;
- }
- Serial.print("\nReceived: ");
- PrintHex8(msgbuffer, bytes_count);
- // check checksum
- if (checksum(msgbuffer, bytes_count) != 0)
- {
- // Checksum error
- Serial.println("Checksum error");
- return ERROR_TMO_CHK_ADR;
- }
- // check address
- if (msgbuffer[0] != ((WBUS_HOST_ADDR << 4) | WBUS_CLIENT_ADDR))
- {
- // Incorrect address
- Serial.println("Incorrect address");
- return ERROR_TMO_CHK_ADR;
- }
- // check validity
- if (msgbuffer[2] != (cmd | 0x80))
- {
- // Request was rejected...
- Serial.println("Request was rejected");
- message_length = 0;
- return ERROR_NACK;
- }
- // copy info to data
- message_length = msgbuffer[1] - 2 - skip;
- bytes_count = 3 + skip;
- for (cnt = 0; cnt < message_length; cnt++, bytes_count++)
- {
- message_data[cnt] = msgbuffer[bytes_count];
- }
- return 0;
- }
- //--------------------------------------------------------------------------------
- //
- // Send a client W-Bus request and read answer from Heater.
- //
- void wb_io(byte cmd, byte skip)
- {
- tries2 = 0;
- do
- {
- if (tries2 != 0) delay(NACK_DELAY);
- tries1 = 0;
- do
- {
- if (tries1 != 0) delay(ERROR_DELAY);
- SendBreak(50);
- msg_send(cmd);
- // receive reply
- error = msg_recv(cmd, skip);
- tries1++;
- }
- while (tries1 < 10 && error == ERROR_TMO_CHK_ADR); // timeout, bad checksum or invalid address
- tries2++;
- }
- while (tries2 < 2 && error == ERROR_NACK); // request denied
- }
- void SendBreak(byte duration)
- {
- Serial.println("\n**** BREAK SENT ****\n");
- mySerial.end(); // disable Serial1 transmission
- pinMode(tx_line, OUTPUT);
- digitalWrite(tx_line, 0); // set low the tx pin
- delay(duration); // wait 25 ms
- digitalWrite(tx_line, 1); // set high the tx pin
- delay(duration);
- mySerial.begin(2400, SERIAL_8E1); // re-enable Serial1 transmission
- delay(25);
- }
- //--------------------------------------------------------------------------------
- //
- // Turn heater on for time minutes
- //
- void turnOn(byte time)
- {
- message_data[0] = time;
- message_length = 1;
- wb_io(WBUS_CMD_ON_PH, 0);
- keepalive=true;
- }
- //--------------------------------------------------------------------------------
- //
- // Turn heater off
- //
- void turnOff(void)
- {
- message_length = 0;
- wb_io(WBUS_CMD_OFF, 0);
- keepalive=false;
- }
- //--------------------------------------------------------------------------------
- //
- // keepAlive - poll the heater
- //
- void keepAlive()
- {
- message_data[0] = WBUS_CMD_ON_PH; // polling (keep-alive)
- message_data[1] = 0;
- message_length = 2;
- wb_io(WBUS_CMD_CHK, 0);
- if (error != 0) return;
- }
- //--------------------------------------------------------------------------------
- //
- // get status 2
- //
- void get_status2(void)
- {
- message_data[0] = QUERY_STATUS2; // status 2 - level of devices
- message_length = 1;
- wb_io(WBUS_CMD_QUERY, 1);
- if (error != 0)
- {
- return;
- }
- unsigned char glowplug_percentage = message_data[STA2_GP] / 28;//divide by 28 to give char 0-7 for bar display
- unsigned char fuelpump_percentage = message_data[STA2_FP] / 28;
- unsigned char airfan_percentage = message_data[STA2_CAF] / 28;
- unsigned char circpump_percentage = message_data[STA2_CP] / 28;
- //sprintf(tbs, "PLUG %c FPUMP %c" , glowplug_percentage,fuelpump_percentage);
- //lcd.print(tbs);
- }
- //--------------------------------------------------------------------------------
- //
- // get status
- //
- void get_status(void)
- {
- message_data[0] = QUERY_SENSORS; // sensors - temperature, volts, flame detection, glow plug resistance
- message_length = 1;
- wb_io(WBUS_CMD_QUERY, 1); if (error != 0) return;
- int temp = message_data[SEN_TEMP] - 50;
- float voltage = ((message_data[SEN_VOLT] * 256) + message_data[SEN_VOLT + 1]) ;//in millivolts
- int FlameOn = message_data[SEN_FD];
- int watts = ((message_data[SEN_HE] * 256) + message_data[SEN_HE + 1]);
- int resistance = ((message_data[SEN_GPR] * 256) + message_data[SEN_GPR + 1]);
- Serial.println();
- sprintf(tbs, "%d%cC V=", temp, 223, voltage, FlameOn); //223 = degree sign
- Serial.println(tbs);
- sprintf(tbs, " F=%d " , FlameOn);
- sprintf(tbs, "Watts=%d %d%c ", watts, resistance, 244); //244 = ohm sign
- Serial.println(tbs);
- }
- void loop()
- {
- currm = millis();
- if (currm - prevm > 2000)
- {
- prevm = currm;
- if (keepalive == true) keepAlive();
- switch (statustype)
- {
- case 1:
- case 2:
- get_status();
- break;
- case 3:
- case 4:
- get_status2();
- statustype = 0;
- break;
- }
- statustype++;
- }
- sensors.requestTemperatures();
- temperature1 = sensors.getTempC(insideThermometer); // Read temp and store it in variable
- temperature2 = sensors.getTempC(outsideThermometer);
- int response=0;
- Fan = 0; // Read value of fan sensor pin and store it in Fan
- Heat = 0; // Read value of heat sensor pin and store it in Heat
- Flame = 0;
- // ******************************************************** Flame Sensor Section Start ******************************************************
- if (Flame != FlameState ) // The state has changed!
- {
- if (Flame == ?)
- {
- sprintf(string,"Flame Off");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- else
- {
- sprintf(string,"Flame On");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- FlameState = Flame; // Save the new state in our variable
- }
- // ******************************************************** Flame Sensor Section End ********************************************************
- // ******************************************************** Fan Sensor Section Start ********************************************************
- if (Fan != FanState ) // The state has changed!
- {
- if (Fan == ?)
- {
- sprintf(string,"Fan On");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- else
- {
- sprintf(string,"Fan Off");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- FanState = Fan; // Save the new state in our variable
- }
- // ******************************************************** Fan Sensor Section End ***********************************************************
- // ******************************************************** Heater Sensor Section Start ******************************************************
- if (Heat != HeatState ) // The state has changed!
- {
- if (Heat == ?)
- {
- sprintf(string,"Heater On");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- else
- {
- sprintf(string,"Heater Off");
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- HeatState = Heat; // Save the new state in our variable
- }
- // ******************************************************** Heater Sensor Section End ********************************************************
- // ********************************************************Incoming SMS Section Start ********************************************************
- if(Serial1.available() >0) // If a character comes in from the cellular module...
- {
- inchar=Serial1.read();
- if (inchar=='@') // Control char, if sms does start with anything else nothing happens and the sms is deleted
- {
- delay(10);
- inchar=Serial1.read();
- if (inchar=='a')
- {
- delay(10);
- inchar=Serial1.read();
- if (inchar=='0')
- {
- Serial.println("Shutting down heater");
- turnOff(); // If 0 after a then turn relay1 off
- }
- else if (inchar=='1')
- {
- Serial.println("Starting heater");
- turnOn(50); // If 1 after a then turn relay1 on
- }
- }
- delay(10);
- if (inchar=='t')
- {
- delay(10);
- inchar=Serial1.read();
- if (inchar=='0') // If 0 after t then send text with cabin temp
- {
- int temperature1dec=temperature1;
- int temperature1uni=(temperature1*100)-(temperature1dec*100);
- sprintf(string,"Kolertemperatur %d.%d Grader",temperature1dec,temperature1uni); // Text to send when requesting cabin temp leave the %d.%d part
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); // +********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- else if (inchar=='1') // If 1 after t then send text with coolant temp
- {
- int temperature2dec=temperature2;
- int temperature2uni=(temperature2*100)-(temperature2dec*100);
- sprintf(string,"Kabinetemperatur %d.%d Grader",temperature2dec,temperature2uni); // Text to send when requesting coolant temp leave the %d.%d part
- Serial.println(string);
- response=gsm.SendSMS("+4560686615",string); //+********** insert your telephone number
- Serial.print("response ");
- Serial.println(response);
- }
- }
- Serial1.println("AT+CMGD=1,4"); // Delete all SMS
- }
- }
- // ********************************************************Incoming SMS Section End ********************************************************
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement