Lovkesh Chauhan / Mbed OS SmartKitchen_MiniProject

Dependencies:   C12832 LM75B MQ2 MQTT

Committer:
lovkesh_chauhan
Date:
Tue Jan 04 11:10:00 2022 +0000
Revision:
24:74f170dfd425
Parent:
21:a68bd76740f9
Project Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 17:0811bdbdd78a 2 * Copyright (c) 2014, 2015 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 17:0811bdbdd78a 15 * Ian Craggs - make sure QoS2 processing works, and add device headers
icraggs 1:a1d5c7a6acbc 16 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 17
icraggs 2:638c854c0695 18 /**
icraggs 2:638c854c0695 19 This is a sample program to illustrate the use of the MQTT Client library
icraggs 2:638c854c0695 20 on the mbed platform. The Client class requires two classes which mediate
icraggs 2:638c854c0695 21 access to system interfaces for networking and timing. As long as these two
icraggs 2:638c854c0695 22 classes provide the required public programming interfaces, it does not matter
icraggs 2:638c854c0695 23 what facilities they use underneath. In this program, they use the mbed
icraggs 2:638c854c0695 24 system libraries.
Jan Jongboom 20:49c9daf2b0ff 25
icraggs 2:638c854c0695 26 */
Jan Jongboom 20:49c9daf2b0ff 27
Jan Jongboom 21:a68bd76740f9 28 // change this to 1 to output messages to LCD instead of serial
lovkesh_chauhan 24:74f170dfd425 29 #define USE_LCD 1
icraggs 1:a1d5c7a6acbc 30
icraggs 18:07a79d8f01c3 31 #if USE_LCD
icraggs 17:0811bdbdd78a 32 #include "C12832.h"
icraggs 2:638c854c0695 33
Jan Jongboom 20:49c9daf2b0ff 34 // the actual pins are defined in mbed_app.json and can be overridden per target
Jan Jongboom 20:49c9daf2b0ff 35 C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
icraggs 17:0811bdbdd78a 36
lovkesh_chauhan 24:74f170dfd425 37 #define logMessage lcd.cls();lcd.locate(0,0);lcd.printf
Jan Jongboom 20:49c9daf2b0ff 38
Jan Jongboom 20:49c9daf2b0ff 39 #else
Jan Jongboom 20:49c9daf2b0ff 40
Jan Jongboom 20:49c9daf2b0ff 41 #define logMessage printf
icraggs 18:07a79d8f01c3 42
icraggs 18:07a79d8f01c3 43 #endif
icraggs 18:07a79d8f01c3 44
lovkesh_chauhan 24:74f170dfd425 45 #define MQTTCLIENT_QOS2 0
lovkesh_chauhan 24:74f170dfd425 46
lovkesh_chauhan 24:74f170dfd425 47 #include "easy-connect.h" //Ethernet Connection library
lovkesh_chauhan 24:74f170dfd425 48 #include "MQTTNetwork.h" //MQTT Connection
lovkesh_chauhan 24:74f170dfd425 49 #include "MQTTmbed.h" //MQTT Connection
lovkesh_chauhan 24:74f170dfd425 50 #include "MQTTClient.h" //MQTT Connection
lovkesh_chauhan 24:74f170dfd425 51 #include "string.h";
lovkesh_chauhan 24:74f170dfd425 52
lovkesh_chauhan 24:74f170dfd425 53 #include "mbed.h"
lovkesh_chauhan 24:74f170dfd425 54 //#include "platform/mbed_thread.h"
lovkesh_chauhan 24:74f170dfd425 55 #include "FP.h" //For function pointer usage
lovkesh_chauhan 24:74f170dfd425 56 #include "MQ2.h" //Smoke and Gas detector library
lovkesh_chauhan 24:74f170dfd425 57 #include "LM75B.h" //Temperature sensor
lovkesh_chauhan 24:74f170dfd425 58
lovkesh_chauhan 24:74f170dfd425 59 #define ON 1
lovkesh_chauhan 24:74f170dfd425 60 #define OFF 0
lovkesh_chauhan 24:74f170dfd425 61
lovkesh_chauhan 24:74f170dfd425 62 #define PUMP PTC1 //Relay Signal for controlling pump
lovkesh_chauhan 24:74f170dfd425 63 #define TRIGGER PTB18 //Trigger pin for ultrasonic sensor
lovkesh_chauhan 24:74f170dfd425 64 #define ECHO PTB19 //Echo pin for ultrasonic sensor
lovkesh_chauhan 24:74f170dfd425 65 #define MQ2_SIG PTC10 //Smoke and Gas sensor output pin
lovkesh_chauhan 24:74f170dfd425 66 #define ALARM_SIG PTC8 //Alarm Signal Pin
lovkesh_chauhan 24:74f170dfd425 67 #define FAN_SIG PTC9 //Fan signal pin
lovkesh_chauhan 24:74f170dfd425 68
lovkesh_chauhan 24:74f170dfd425 69 //Global variables to update the ThingSpeak channel fields.
lovkesh_chauhan 24:74f170dfd425 70 int WATER_LEVEL = 0;
lovkesh_chauhan 24:74f170dfd425 71 float CO_level = 0;
lovkesh_chauhan 24:74f170dfd425 72 float SMOKE_level = 0;
lovkesh_chauhan 24:74f170dfd425 73 float GAS_level = 0;
lovkesh_chauhan 24:74f170dfd425 74 float TEMPERATURE = 0;
lovkesh_chauhan 24:74f170dfd425 75 int FAN_STATE = 0;
lovkesh_chauhan 24:74f170dfd425 76 int ALARM_STATE = 0;
lovkesh_chauhan 24:74f170dfd425 77 int PUMP_STATE = 0;
lovkesh_chauhan 24:74f170dfd425 78
lovkesh_chauhan 24:74f170dfd425 79 class indicator{ //class to allow easy usage of RGB LED
lovkesh_chauhan 24:74f170dfd425 80 private:
lovkesh_chauhan 24:74f170dfd425 81 DigitalOut RED; //LED1
lovkesh_chauhan 24:74f170dfd425 82 DigitalOut BLUE; //LED2
lovkesh_chauhan 24:74f170dfd425 83 DigitalOut GREEN; //LED3
lovkesh_chauhan 24:74f170dfd425 84 Ticker blink; //Ticker to start blinking
lovkesh_chauhan 24:74f170dfd425 85 bool red_set; //bool to check if RED is ON
lovkesh_chauhan 24:74f170dfd425 86 bool green_set; //bool to check if GREEN is ON
lovkesh_chauhan 24:74f170dfd425 87 bool blue_set; //bool to check if BLUE is ON
lovkesh_chauhan 24:74f170dfd425 88
lovkesh_chauhan 24:74f170dfd425 89 void blink_effect(){ //function to flip LEDs to generate blink effect
lovkesh_chauhan 24:74f170dfd425 90 if(red_set)
lovkesh_chauhan 24:74f170dfd425 91 RED = !RED;
lovkesh_chauhan 24:74f170dfd425 92 if(green_set)
lovkesh_chauhan 24:74f170dfd425 93 GREEN = !GREEN;
lovkesh_chauhan 24:74f170dfd425 94 if(blue_set)
lovkesh_chauhan 24:74f170dfd425 95 BLUE = !BLUE;
lovkesh_chauhan 24:74f170dfd425 96 }
lovkesh_chauhan 24:74f170dfd425 97
lovkesh_chauhan 24:74f170dfd425 98 public:
lovkesh_chauhan 24:74f170dfd425 99 indicator(PinName _LED1, PinName _LED2, PinName _LED3):RED(_LED1, 1), GREEN(_LED2, 1), BLUE(_LED3, 1){ //Initialize RGB LEDs with requried PINS.
lovkesh_chauhan 24:74f170dfd425 100 red_set = false; green_set = false; blue_set = false; //All LED intially OFF.
lovkesh_chauhan 24:74f170dfd425 101 }
lovkesh_chauhan 24:74f170dfd425 102
lovkesh_chauhan 24:74f170dfd425 103 void red_led(){ //Set Only RED LED
lovkesh_chauhan 24:74f170dfd425 104 RED = 0; red_set = true;
lovkesh_chauhan 24:74f170dfd425 105 GREEN = 1; green_set = false;
lovkesh_chauhan 24:74f170dfd425 106 BLUE = 1; blue_set = false;
lovkesh_chauhan 24:74f170dfd425 107 }
lovkesh_chauhan 24:74f170dfd425 108
lovkesh_chauhan 24:74f170dfd425 109 void green_led(){ //Set Only GREEN LED
lovkesh_chauhan 24:74f170dfd425 110 RED = 1; red_set = false;
lovkesh_chauhan 24:74f170dfd425 111 GREEN = 0; green_set = true;
lovkesh_chauhan 24:74f170dfd425 112 BLUE = 1; blue_set = false;
lovkesh_chauhan 24:74f170dfd425 113 }
lovkesh_chauhan 24:74f170dfd425 114 void blue_led(){//Set Only BLUE LED
lovkesh_chauhan 24:74f170dfd425 115 RED = 1; red_set = false;
lovkesh_chauhan 24:74f170dfd425 116 GREEN = 1; green_set = false;
lovkesh_chauhan 24:74f170dfd425 117 BLUE = 0; blue_set = true;
lovkesh_chauhan 24:74f170dfd425 118 }
lovkesh_chauhan 24:74f170dfd425 119 void amber_led(){//Set Only AMBER LED
lovkesh_chauhan 24:74f170dfd425 120 RED = 0; red_set = true;
lovkesh_chauhan 24:74f170dfd425 121 GREEN = 0; green_set = true;
lovkesh_chauhan 24:74f170dfd425 122 BLUE = 1; blue_set = false;
lovkesh_chauhan 24:74f170dfd425 123 }
lovkesh_chauhan 24:74f170dfd425 124 void pink_led(){//Set Only PINK LED
lovkesh_chauhan 24:74f170dfd425 125 RED = 0; red_set = true;
lovkesh_chauhan 24:74f170dfd425 126 GREEN = 1; green_set = false;
lovkesh_chauhan 24:74f170dfd425 127 BLUE = 0; blue_set = true;
lovkesh_chauhan 24:74f170dfd425 128 }
lovkesh_chauhan 24:74f170dfd425 129 void cyan_led(){//Set Only CYAN LED
lovkesh_chauhan 24:74f170dfd425 130 RED = 1; red_set = false;
lovkesh_chauhan 24:74f170dfd425 131 GREEN = 0; green_set = true;
lovkesh_chauhan 24:74f170dfd425 132 BLUE = 0; blue_set = true;
lovkesh_chauhan 24:74f170dfd425 133 }
lovkesh_chauhan 24:74f170dfd425 134 void white_led(){//Set Only WHITE LED
lovkesh_chauhan 24:74f170dfd425 135 RED = 0; red_set = true;
lovkesh_chauhan 24:74f170dfd425 136 GREEN = 0; green_set = true;
lovkesh_chauhan 24:74f170dfd425 137 BLUE = 0; blue_set = true;
lovkesh_chauhan 24:74f170dfd425 138 }
lovkesh_chauhan 24:74f170dfd425 139 void all_led_off(){//Set All LED OFF
lovkesh_chauhan 24:74f170dfd425 140 RED = 1; red_set = false;
lovkesh_chauhan 24:74f170dfd425 141 GREEN = 1; green_set = false;
lovkesh_chauhan 24:74f170dfd425 142 BLUE = 1; blue_set = false;
lovkesh_chauhan 24:74f170dfd425 143 }
lovkesh_chauhan 24:74f170dfd425 144 void start_blink(float blink_interval){ //Start blinking on current LED using tikcer to avoid code blocking
lovkesh_chauhan 24:74f170dfd425 145 blink.attach(callback(this, &indicator::blink_effect), blink_interval);
lovkesh_chauhan 24:74f170dfd425 146 }
lovkesh_chauhan 24:74f170dfd425 147 void stop_blink(){ //Stop blinkink. Detach Ticker function call
lovkesh_chauhan 24:74f170dfd425 148 blink.detach();
lovkesh_chauhan 24:74f170dfd425 149 if(red_set) //Set last LED being used as ON.
lovkesh_chauhan 24:74f170dfd425 150 RED = 0;
lovkesh_chauhan 24:74f170dfd425 151 if(green_set)
lovkesh_chauhan 24:74f170dfd425 152 GREEN = 0;
lovkesh_chauhan 24:74f170dfd425 153 if(blue_set)
lovkesh_chauhan 24:74f170dfd425 154 BLUE = 0;
lovkesh_chauhan 24:74f170dfd425 155 }
lovkesh_chauhan 24:74f170dfd425 156 };
lovkesh_chauhan 24:74f170dfd425 157
lovkesh_chauhan 24:74f170dfd425 158 indicator led_indicator(LED1, LED2, LED3); //Use K64f RGB LED for water pump
lovkesh_chauhan 24:74f170dfd425 159 indicator shield_led_indicator(D5, D9, D8); //Use Application Shield's RGB LED for MQ2 sensor
lovkesh_chauhan 24:74f170dfd425 160
lovkesh_chauhan 24:74f170dfd425 161 class ultrasonic{ //Ultrasonic sensot for measuring depth of water in tank
lovkesh_chauhan 24:74f170dfd425 162 private:
lovkesh_chauhan 24:74f170dfd425 163 DigitalOut trigger;//DigitalOut trigger; //trigger pin for UltarSonic sensor
lovkesh_chauhan 24:74f170dfd425 164 InterruptIn echo; //Echo pin for water_level measurement. Uses as Interrupt to avoid code execution blocking.
lovkesh_chauhan 24:74f170dfd425 165 Ticker measure; //Ticker to trigger water_level measurement periodically.
lovkesh_chauhan 24:74f170dfd425 166 Timeout trigger_low; //Timeout interrupt to set trigger pin low after 10us of HIGH.
lovkesh_chauhan 24:74f170dfd425 167 Timer echoTimer; //Timer to get the time elapsed between echo sent and echo received back
lovkesh_chauhan 24:74f170dfd425 168 int echo_time_us; //Timer taken by echo to resturn abck to module.
lovkesh_chauhan 24:74f170dfd425 169 float interval; //Interval after which periodic measurement should start
lovkesh_chauhan 24:74f170dfd425 170 volatile bool measuring;
lovkesh_chauhan 24:74f170dfd425 171 FP<void,int> fp;
lovkesh_chauhan 24:74f170dfd425 172
lovkesh_chauhan 24:74f170dfd425 173 void set_trigger_high(){ //Function to set Trigger Pin HIGH for 10us and set measuring = true
lovkesh_chauhan 24:74f170dfd425 174 measuring = true;
lovkesh_chauhan 24:74f170dfd425 175 trigger = ON;
lovkesh_chauhan 24:74f170dfd425 176 trigger_low.attach_us(callback(this, &ultrasonic::set_trigger_low), 10); //set trigger pin OFF after 10us
lovkesh_chauhan 24:74f170dfd425 177 }
lovkesh_chauhan 24:74f170dfd425 178
lovkesh_chauhan 24:74f170dfd425 179 void set_trigger_low(void){ //Function to set trigger pin LOW to fiish trigger after 10us. This will be called by low timer object.
lovkesh_chauhan 24:74f170dfd425 180 trigger = OFF;
lovkesh_chauhan 24:74f170dfd425 181 }
lovkesh_chauhan 24:74f170dfd425 182
lovkesh_chauhan 24:74f170dfd425 183 void start_echo(void){//Function to start listening for echo back. This will start timer echoTimer. Will be based on Echo Pin RISE(or HIGH)
lovkesh_chauhan 24:74f170dfd425 184 if(!measuring) return; //avoid fluctuations on echo pin if any. Continue echo calculation only if measuring is TRUE.
lovkesh_chauhan 24:74f170dfd425 185 echoTimer.reset();
lovkesh_chauhan 24:74f170dfd425 186 echoTimer.start(); //Timer start since Echo pin is HIGH
lovkesh_chauhan 24:74f170dfd425 187 }
lovkesh_chauhan 24:74f170dfd425 188 void stop_echo(void){//Function to stop echoTimer after echo received back. Will be based on Echo Pin FALL(or LOW).
lovkesh_chauhan 24:74f170dfd425 189 if (!measuring)return; //avoid fluctuations on echo pin if any
lovkesh_chauhan 24:74f170dfd425 190 echoTimer.stop(); //stop timer since echo pin back to LOW
lovkesh_chauhan 24:74f170dfd425 191 echo_time_us = echoTimer.read_us(); //Calculate time for ultrasonic wave RTT.
lovkesh_chauhan 24:74f170dfd425 192 if(echo_time_us > 2000 || echo_time_us < 125)return; //ignore reading in case abnormally high or low RTT time
lovkesh_chauhan 24:74f170dfd425 193 measuring = false; //Marke measurement completed
lovkesh_chauhan 24:74f170dfd425 194 fp(echo_time_us / (29 * 2));
lovkesh_chauhan 24:74f170dfd425 195 }
lovkesh_chauhan 24:74f170dfd425 196 public:
lovkesh_chauhan 24:74f170dfd425 197 ultrasonic(PinName _trigger, PinName _echo,float _interval):trigger(_trigger), echo(_echo){ //Constructor: initialize trigger(default value OFF) & echo pin
lovkesh_chauhan 24:74f170dfd425 198 interval = _interval;
lovkesh_chauhan 24:74f170dfd425 199 echo.rise(callback(this, &ultrasonic::start_echo)); //Attach function to Interrupt on Echo pin Rise
lovkesh_chauhan 24:74f170dfd425 200 echo.fall(callback(this, &ultrasonic::stop_echo)); //Attach function to Interrupt on Echo pin Fall
lovkesh_chauhan 24:74f170dfd425 201 measuring = false;
lovkesh_chauhan 24:74f170dfd425 202 }
lovkesh_chauhan 24:74f170dfd425 203
lovkesh_chauhan 24:74f170dfd425 204 void start_measure(){ //function to Set Trigger Pin high repeatedly every 'time' seconds
lovkesh_chauhan 24:74f170dfd425 205 measure.attach(callback(this, &ultrasonic::set_trigger_high),interval);//ticker to trigger measurement with callback function
lovkesh_chauhan 24:74f170dfd425 206 }
lovkesh_chauhan 24:74f170dfd425 207 void stop_measure(void){ //Dettach from Ticker and stop measurements
lovkesh_chauhan 24:74f170dfd425 208 measuring = false;
lovkesh_chauhan 24:74f170dfd425 209 measure.detach();
lovkesh_chauhan 24:74f170dfd425 210 }
lovkesh_chauhan 24:74f170dfd425 211 void req_callBack(FP<void, int> _fp){
lovkesh_chauhan 24:74f170dfd425 212 fp = _fp;
lovkesh_chauhan 24:74f170dfd425 213 }
lovkesh_chauhan 24:74f170dfd425 214 };
icraggs 0:0cae29831d01 215
lovkesh_chauhan 24:74f170dfd425 216 class pump{
lovkesh_chauhan 24:74f170dfd425 217 private:
lovkesh_chauhan 24:74f170dfd425 218 DigitalOut signal; //Signal for relay to set PUMP ON and OFF
lovkesh_chauhan 24:74f170dfd425 219 ultrasonic sensor; //Ultrasonic module objet for measuring depth of water
lovkesh_chauhan 24:74f170dfd425 220 volatile bool pump_on_request; //Bool to inform whether ita PUMP on request or only water depth meas.
lovkesh_chauhan 24:74f170dfd425 221 const int LEVEL_LOW = 4; //Low Water level defined as 4CM. Signifies Tank FULL.
lovkesh_chauhan 24:74f170dfd425 222 const int LEVEL_HIGH = 9; //High Water level defined as 11CM. Signifies Tank EMPTY.
lovkesh_chauhan 24:74f170dfd425 223 FP<void, int> fp; //Function pointer to receive call back with depth of water level in tank.
lovkesh_chauhan 24:74f170dfd425 224
lovkesh_chauhan 24:74f170dfd425 225 public:
lovkesh_chauhan 24:74f170dfd425 226 //Initialize Ultrasonic module with TRIG and ECHO pin along with measurement interval.
lovkesh_chauhan 24:74f170dfd425 227 pump(PinName _signal, PinName _trigger, PinName _echo, float _interval):signal(_signal), sensor(_trigger, _echo, _interval){
lovkesh_chauhan 24:74f170dfd425 228 pump_on_request = false;
lovkesh_chauhan 24:74f170dfd425 229 fp.attach(this, &pump::update_depth); //update_depth function will be called bu ultrasonic module when depth meas is completed.
lovkesh_chauhan 24:74f170dfd425 230 sensor.req_callBack(fp); //set call back.
lovkesh_chauhan 24:74f170dfd425 231 }
lovkesh_chauhan 24:74f170dfd425 232 void start_measurement(int interval){
lovkesh_chauhan 24:74f170dfd425 233 sensor.start_measure(); //Start depth measurements to calculate the water level.
lovkesh_chauhan 24:74f170dfd425 234 }
lovkesh_chauhan 24:74f170dfd425 235 void stop_measurement(){
lovkesh_chauhan 24:74f170dfd425 236 sensor.stop_measure(); //Start depth measurements to calculate the water level.
lovkesh_chauhan 24:74f170dfd425 237 }
lovkesh_chauhan 24:74f170dfd425 238
lovkesh_chauhan 24:74f170dfd425 239 void pump_on(){ //Pump On request by User.
lovkesh_chauhan 24:74f170dfd425 240 pump_on_request = true; //bool to confirm that Pump On request is raised
lovkesh_chauhan 24:74f170dfd425 241 //sensor.start_measure(); //Start depth measurements to calculate the water level.
lovkesh_chauhan 24:74f170dfd425 242 }
lovkesh_chauhan 24:74f170dfd425 243 void pump_off(){ //Switch OFF the pump
lovkesh_chauhan 24:74f170dfd425 244 pump_on_request = false; //Set User request to flase.
lovkesh_chauhan 24:74f170dfd425 245 //sensor.stop_measure(); //Stop the ultrasonic measurements
lovkesh_chauhan 24:74f170dfd425 246 signal = OFF; //Relay Signal for pump OFF.
lovkesh_chauhan 24:74f170dfd425 247 PUMP_STATE = OFF;
lovkesh_chauhan 24:74f170dfd425 248 }
lovkesh_chauhan 24:74f170dfd425 249
lovkesh_chauhan 24:74f170dfd425 250 void get_depth(){ //Only depth of water requried. no need to switch ON pump.
lovkesh_chauhan 24:74f170dfd425 251 if(signal == OFF) //If Already PUMP ONdont start measurement again.
lovkesh_chauhan 24:74f170dfd425 252 sensor.start_measure(); //Start Ultrasonic Measurements
lovkesh_chauhan 24:74f170dfd425 253 }
lovkesh_chauhan 24:74f170dfd425 254
lovkesh_chauhan 24:74f170dfd425 255 void update_depth(int depth){ //Depth from ultrasonic is available
lovkesh_chauhan 24:74f170dfd425 256 WATER_LEVEL = depth;
lovkesh_chauhan 24:74f170dfd425 257 if(depth <= LEVEL_LOW) // set GREEN LED to inidicate the tank is FULL
lovkesh_chauhan 24:74f170dfd425 258 led_indicator.green_led();
lovkesh_chauhan 24:74f170dfd425 259 else if(depth <= LEVEL_HIGH)// set Amber LED to inidicate the tank is more than HALF
lovkesh_chauhan 24:74f170dfd425 260 led_indicator.amber_led();
lovkesh_chauhan 24:74f170dfd425 261 else
lovkesh_chauhan 24:74f170dfd425 262 led_indicator.red_led();// set RED LED to inidicate the tank is less than HALF
lovkesh_chauhan 24:74f170dfd425 263
lovkesh_chauhan 24:74f170dfd425 264 if(depth <= LEVEL_LOW && signal == ON)//Tank FULL and PUMP ON. Switch OFF PUMP.
lovkesh_chauhan 24:74f170dfd425 265 pump_off();
lovkesh_chauhan 24:74f170dfd425 266 else if(signal == OFF && pump_on_request && depth > LEVEL_HIGH ) //Pump is OFF and User have requested to on PUMP
lovkesh_chauhan 24:74f170dfd425 267 signal = ON; //Pump ON
lovkesh_chauhan 24:74f170dfd425 268 else if(signal == ON && !pump_on_request) //If PUMP is ON but not requster by User.
lovkesh_chauhan 24:74f170dfd425 269 pump_off(); //Pump OFF
lovkesh_chauhan 24:74f170dfd425 270 else if(signal == OFF && !pump_on_request) // Only for depth measurement. Pump on is not requested by user.
lovkesh_chauhan 24:74f170dfd425 271 pump_off();
lovkesh_chauhan 24:74f170dfd425 272 PUMP_STATE = signal;
lovkesh_chauhan 24:74f170dfd425 273 }
lovkesh_chauhan 24:74f170dfd425 274 };
lovkesh_chauhan 24:74f170dfd425 275
lovkesh_chauhan 24:74f170dfd425 276 class control{ //Control object for fan and alarm. Only Switch ON and OFF requried.
lovkesh_chauhan 24:74f170dfd425 277 private:
lovkesh_chauhan 24:74f170dfd425 278 DigitalOut signal; //Signal pin for object
lovkesh_chauhan 24:74f170dfd425 279 public:
lovkesh_chauhan 24:74f170dfd425 280 control(PinName _signal, int state):signal(_signal,state){ //Initialize Signal PIN with default state(ON or OFF)
lovkesh_chauhan 24:74f170dfd425 281 }
lovkesh_chauhan 24:74f170dfd425 282
lovkesh_chauhan 24:74f170dfd425 283 void set_state(int state){ //Set the state of control
lovkesh_chauhan 24:74f170dfd425 284 if (state >= 1){ //Switch ON
lovkesh_chauhan 24:74f170dfd425 285 signal = ON;
lovkesh_chauhan 24:74f170dfd425 286 led_indicator.pink_led(); //Blink RED LED
lovkesh_chauhan 24:74f170dfd425 287 }
lovkesh_chauhan 24:74f170dfd425 288 else //Switch OFF
lovkesh_chauhan 24:74f170dfd425 289 signal = OFF; //Set Control OFF
lovkesh_chauhan 24:74f170dfd425 290 //publish data
lovkesh_chauhan 24:74f170dfd425 291 }
lovkesh_chauhan 24:74f170dfd425 292 int get_state(){ //Get the state of the control
lovkesh_chauhan 24:74f170dfd425 293 return signal.read();
lovkesh_chauhan 24:74f170dfd425 294 }
lovkesh_chauhan 24:74f170dfd425 295 };
lovkesh_chauhan 24:74f170dfd425 296
lovkesh_chauhan 24:74f170dfd425 297 class _temperature{ //Object for measuring current temperature and taking action accordingly
lovkesh_chauhan 24:74f170dfd425 298 private:
lovkesh_chauhan 24:74f170dfd425 299 LM75B temp_sensor; //LM75B temperature library used.
lovkesh_chauhan 24:74f170dfd425 300 float temperature_threshold_fan; // Threshold value for switching ON the FAN
lovkesh_chauhan 24:74f170dfd425 301 float temperature_threshold_alarm; // Threshold value for switching ON the ALARM
lovkesh_chauhan 24:74f170dfd425 302 Ticker periodic_meas; //Ticker for periodic measurement of the temperature
lovkesh_chauhan 24:74f170dfd425 303 control* alarm; //Poniter to control the alarm
lovkesh_chauhan 24:74f170dfd425 304 control* fan; //Poniter to control the fan
lovkesh_chauhan 24:74f170dfd425 305 void read_data(){ //Read the temperature from sensor
lovkesh_chauhan 24:74f170dfd425 306 TEMPERATURE = temp_sensor.temp(); //Use global variable TEMPERATURE which will be sent to ThingSpeak.
lovkesh_chauhan 24:74f170dfd425 307 if(TEMPERATURE >= temperature_threshold_alarm){ //Check if alarm threshold met.
lovkesh_chauhan 24:74f170dfd425 308 alarm->set_state(ON); //Set Alarm ON
lovkesh_chauhan 24:74f170dfd425 309 shield_led_indicator.red_led(); //Start RED LED Blinking
lovkesh_chauhan 24:74f170dfd425 310 }
lovkesh_chauhan 24:74f170dfd425 311 else if(TEMPERATURE >= temperature_threshold_fan){//Check if fan threshold met.
lovkesh_chauhan 24:74f170dfd425 312 shield_led_indicator.amber_led();//Set Amber LED Blinking
lovkesh_chauhan 24:74f170dfd425 313 fan->set_state(ON);//Set Alarm ON
lovkesh_chauhan 24:74f170dfd425 314 FAN_STATE = ON;
lovkesh_chauhan 24:74f170dfd425 315 }
lovkesh_chauhan 24:74f170dfd425 316 else
lovkesh_chauhan 24:74f170dfd425 317 shield_led_indicator.green_led();
lovkesh_chauhan 24:74f170dfd425 318 }
lovkesh_chauhan 24:74f170dfd425 319 public:
lovkesh_chauhan 24:74f170dfd425 320 _temperature(control* _alarm, control* _fan):temp_sensor(D14,D15){ //Initialize temp sensor with shield PINs D14, D15
lovkesh_chauhan 24:74f170dfd425 321 alarm = _alarm; //received object for alarm from Main function
lovkesh_chauhan 24:74f170dfd425 322 fan = _fan;//received object for fan from Main function
lovkesh_chauhan 24:74f170dfd425 323 temperature_threshold_fan = 30; //Fan threshold
lovkesh_chauhan 24:74f170dfd425 324 temperature_threshold_alarm = 40; //Alarm threshold
lovkesh_chauhan 24:74f170dfd425 325 }
lovkesh_chauhan 24:74f170dfd425 326 void start_measurement(int interval){
lovkesh_chauhan 24:74f170dfd425 327 periodic_meas.attach(callback(this, &_temperature::read_data), interval); //Start ticker based periodic temperature measurement
lovkesh_chauhan 24:74f170dfd425 328 }
lovkesh_chauhan 24:74f170dfd425 329 void stop_measurement(int interval){
lovkesh_chauhan 24:74f170dfd425 330 periodic_meas.detach(); //Stop ticker.
lovkesh_chauhan 24:74f170dfd425 331 }
lovkesh_chauhan 24:74f170dfd425 332 };
lovkesh_chauhan 24:74f170dfd425 333
lovkesh_chauhan 24:74f170dfd425 334 class _smoke_detector{ //Class to control the MQ2 Gas and Smoke detector
lovkesh_chauhan 24:74f170dfd425 335 private:
lovkesh_chauhan 24:74f170dfd425 336 MQ2 mq2; //Existing MQ2 library is used
lovkesh_chauhan 24:74f170dfd425 337 MQ2_data_t data; //data field to read the CO, SMOKE & GAS levels
lovkesh_chauhan 24:74f170dfd425 338 float CO_threshold_fan; //FAN Threshold for Carbon Monoxide
lovkesh_chauhan 24:74f170dfd425 339 float CO_threshold_alarm;//ALARM Threshold for Carbon Monoxide
lovkesh_chauhan 24:74f170dfd425 340 float GAS_threshold_fan; //FAN Threshold for combustible GASES
lovkesh_chauhan 24:74f170dfd425 341 float GAS_threshold_alarm; //ALARM Threshold for combustible GASES
lovkesh_chauhan 24:74f170dfd425 342 float SMOKE_threshold_fan; //FAN Threshold for SMOKE
lovkesh_chauhan 24:74f170dfd425 343 float SMOKE_threshold_alarm;//ALARM Threshold for SMOKE
lovkesh_chauhan 24:74f170dfd425 344 control* fan; //Poniter to control the fan
lovkesh_chauhan 24:74f170dfd425 345 control* alarm; //Poniter to control the alarm
lovkesh_chauhan 24:74f170dfd425 346 Ticker periodic_meas; //Ticker to complete periodic measurements
lovkesh_chauhan 24:74f170dfd425 347
lovkesh_chauhan 24:74f170dfd425 348 void read_data(){ //Read the GAS/CO/SMOKE level from sensor
lovkesh_chauhan 24:74f170dfd425 349 mq2.read(&data);
lovkesh_chauhan 24:74f170dfd425 350 CO_level = mq2.readCO(); //CO Level
lovkesh_chauhan 24:74f170dfd425 351 GAS_level = mq2.readLPG(); //GAS Level
lovkesh_chauhan 24:74f170dfd425 352 SMOKE_level = mq2.readSmoke(); //SMOKE Level
lovkesh_chauhan 24:74f170dfd425 353 //Check if alarm threhold is crossed for any measurements
lovkesh_chauhan 24:74f170dfd425 354 if(data.co >= CO_threshold_alarm || data.lpg >= GAS_threshold_alarm || data.smoke >= SMOKE_threshold_alarm){
lovkesh_chauhan 24:74f170dfd425 355 shield_led_indicator.red_led(); //Set RED Blinker
lovkesh_chauhan 24:74f170dfd425 356 alarm->set_state(ON); //Set alarm ON
lovkesh_chauhan 24:74f170dfd425 357 }
lovkesh_chauhan 24:74f170dfd425 358 //Check if fan threhold is crossed for any measurements
lovkesh_chauhan 24:74f170dfd425 359 else if(data.co >= CO_threshold_fan || data.lpg >= GAS_threshold_fan || data.smoke >= SMOKE_threshold_fan){
lovkesh_chauhan 24:74f170dfd425 360 shield_led_indicator.amber_led(); //Set AMBER LED
lovkesh_chauhan 24:74f170dfd425 361 fan->set_state(ON); //Set FAN ON
lovkesh_chauhan 24:74f170dfd425 362 }
lovkesh_chauhan 24:74f170dfd425 363 }
lovkesh_chauhan 24:74f170dfd425 364
lovkesh_chauhan 24:74f170dfd425 365 public:
lovkesh_chauhan 24:74f170dfd425 366 //Initialize MQ2 sensor using Analog pin.
lovkesh_chauhan 24:74f170dfd425 367 _smoke_detector(PinName _analog, control* _alarm, control* _fan):mq2(_analog){
lovkesh_chauhan 24:74f170dfd425 368 mq2.begin(); //mq2 begin method to Calibrate sensor
lovkesh_chauhan 24:74f170dfd425 369 fan = _fan; //Fan control object from Main function
lovkesh_chauhan 24:74f170dfd425 370 alarm = _alarm;//Alarm control object from Main function
lovkesh_chauhan 24:74f170dfd425 371 CO_threshold_alarm = 3;
lovkesh_chauhan 24:74f170dfd425 372 CO_threshold_fan = 2;
lovkesh_chauhan 24:74f170dfd425 373 GAS_threshold_fan = 2;
lovkesh_chauhan 24:74f170dfd425 374 GAS_threshold_alarm = 3;
lovkesh_chauhan 24:74f170dfd425 375 SMOKE_threshold_alarm = 3;
lovkesh_chauhan 24:74f170dfd425 376 SMOKE_threshold_fan = 2;
lovkesh_chauhan 24:74f170dfd425 377 }
lovkesh_chauhan 24:74f170dfd425 378
lovkesh_chauhan 24:74f170dfd425 379 void start_measurement(int interval){ //Start Ticker based periodic measurements
lovkesh_chauhan 24:74f170dfd425 380 periodic_meas.attach(callback(this, &_smoke_detector::read_data), interval);
lovkesh_chauhan 24:74f170dfd425 381 }
lovkesh_chauhan 24:74f170dfd425 382 void stop_measurement(){//Stop Ticker based periodic measurements
lovkesh_chauhan 24:74f170dfd425 383 periodic_meas.detach();
lovkesh_chauhan 24:74f170dfd425 384 }
lovkesh_chauhan 24:74f170dfd425 385 };
icraggs 2:638c854c0695 386
icraggs 2:638c854c0695 387 int arrivedcount = 0;
icraggs 2:638c854c0695 388
lovkesh_chauhan 24:74f170dfd425 389 pump water_pump(PUMP,TRIGGER, ECHO, 1); //Water Pump object
lovkesh_chauhan 24:74f170dfd425 390 control alarm(ALARM_SIG, ON); //Alarm Pump object
lovkesh_chauhan 24:74f170dfd425 391 control fan(FAN_SIG, OFF); //Fan object
lovkesh_chauhan 24:74f170dfd425 392 _smoke_detector smoke_detector(MQ2_SIG, &alarm, &fan); //Smoke Detector object
lovkesh_chauhan 24:74f170dfd425 393 _temperature temperature(&alarm, &fan);//Uses fixed PIN on shiled- D14, D15. So no need to pass from main
lovkesh_chauhan 24:74f170dfd425 394 NetworkInterface* network = easy_connect(true); //Network object. Creates ethernt conncetion
lovkesh_chauhan 24:74f170dfd425 395 MQTTNetwork mqttNetwork(network); //MQTTNetwork object. Takes Network object as input for connection over ethernet
lovkesh_chauhan 24:74f170dfd425 396 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); //MQTT Client for Connction with broker, Subscribe and Publsih service.
icraggs 8:a3e3113054a1 397
lovkesh_chauhan 24:74f170dfd425 398
lovkesh_chauhan 24:74f170dfd425 399 void field1_pump_state(MQTT::MessageData& md) //Callback functionf for Field1 from thingspeak.
lovkesh_chauhan 24:74f170dfd425 400 {
lovkesh_chauhan 24:74f170dfd425 401 MQTT::Message &message = md.message; //Message recived using MQTT
lovkesh_chauhan 24:74f170dfd425 402 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
lovkesh_chauhan 24:74f170dfd425 403 logMessage("Message Field1 %s\r\n", message.payload); //Message Payload
lovkesh_chauhan 24:74f170dfd425 404 if(strtol((char *)message.payload, '\0', 10) == 1){ //convert to int and check if power ON command
lovkesh_chauhan 24:74f170dfd425 405 logMessage("Water Pump ON\r\n");
lovkesh_chauhan 24:74f170dfd425 406 water_pump.stop_measurement();
lovkesh_chauhan 24:74f170dfd425 407 water_pump.start_measurement(2); //Start Water Pump measurements. At intervals of 2s. This is small value because for test purpose very small tank is being used. In real life this could be in mins.
lovkesh_chauhan 24:74f170dfd425 408 water_pump.pump_on(); //Start the pump for 1st time
lovkesh_chauhan 24:74f170dfd425 409 }
lovkesh_chauhan 24:74f170dfd425 410 else{
lovkesh_chauhan 24:74f170dfd425 411 logMessage("Water Pump OFF\r\n");
lovkesh_chauhan 24:74f170dfd425 412 water_pump.pump_off(); //Power off pump otherwise
lovkesh_chauhan 24:74f170dfd425 413 }
lovkesh_chauhan 24:74f170dfd425 414 }
lovkesh_chauhan 24:74f170dfd425 415
lovkesh_chauhan 24:74f170dfd425 416 void field2_fan_state(MQTT::MessageData& md)//Callback functionf for Field2 from thingspeak.
lovkesh_chauhan 24:74f170dfd425 417 {
lovkesh_chauhan 24:74f170dfd425 418 MQTT::Message &message = md.message; //Message recived using MQTT
lovkesh_chauhan 24:74f170dfd425 419 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
lovkesh_chauhan 24:74f170dfd425 420 logMessage("Message Field2 %s\r\n", message.payload);//Message Payload
lovkesh_chauhan 24:74f170dfd425 421 if(strtol((char *)message.payload, '\0', 10) == 1){//convert to int and check if power ON command
lovkesh_chauhan 24:74f170dfd425 422 logMessage("FAN ON\r\n");
lovkesh_chauhan 24:74f170dfd425 423 fan.set_state(OFF);//Power On FAN
lovkesh_chauhan 24:74f170dfd425 424 }
lovkesh_chauhan 24:74f170dfd425 425 else{
lovkesh_chauhan 24:74f170dfd425 426 logMessage("FAN OFF\r\n");
lovkesh_chauhan 24:74f170dfd425 427 fan.set_state(ON);//Power off fan otherwise
lovkesh_chauhan 24:74f170dfd425 428 }
lovkesh_chauhan 24:74f170dfd425 429 }
lovkesh_chauhan 24:74f170dfd425 430
lovkesh_chauhan 24:74f170dfd425 431 void field3_alarm_state(MQTT::MessageData& md)
icraggs 2:638c854c0695 432 {
icraggs 9:5beb8609e9f7 433 MQTT::Message &message = md.message;
Jan Jongboom 20:49c9daf2b0ff 434 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
lovkesh_chauhan 24:74f170dfd425 435 logMessage("Message Field3 %s\r\n", message.payload);
lovkesh_chauhan 24:74f170dfd425 436 // RED = strtol((char *)message.payload, '\0', 10);
lovkesh_chauhan 24:74f170dfd425 437 if(strtol((char *)message.payload, '\0', 10) == 1){//convert to int and check if power ON command
lovkesh_chauhan 24:74f170dfd425 438 logMessage("Alarm ON\r\n");;//Power On Alarm. This scenario should never occur,
lovkesh_chauhan 24:74f170dfd425 439 alarm.set_state(ON);
lovkesh_chauhan 24:74f170dfd425 440 }
lovkesh_chauhan 24:74f170dfd425 441 else{
lovkesh_chauhan 24:74f170dfd425 442 logMessage("Alarm OFF\r\n");
lovkesh_chauhan 24:74f170dfd425 443 alarm.set_state(OFF); //Power off Alarm otherwise
lovkesh_chauhan 24:74f170dfd425 444 }
icraggs 2:638c854c0695 445 ++arrivedcount;
icraggs 2:638c854c0695 446 }
icraggs 0:0cae29831d01 447
lovkesh_chauhan 24:74f170dfd425 448 void publish_field1(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 449 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 450 if(PUMP_STATE == last_update)
lovkesh_chauhan 24:74f170dfd425 451 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 452 last_update = PUMP_STATE;
lovkesh_chauhan 24:74f170dfd425 453 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 454 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 455 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 456 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 457 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 458 sprintf(buf, "field1=%d&status=MQTTPUBLISH",PUMP_STATE);
lovkesh_chauhan 24:74f170dfd425 459 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 460 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 461 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 462 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 463 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 464 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 465 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 466 }
lovkesh_chauhan 24:74f170dfd425 467
lovkesh_chauhan 24:74f170dfd425 468 void publish_field2(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 469 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 470 if(FAN_STATE == last_update)
lovkesh_chauhan 24:74f170dfd425 471 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 472 last_update = FAN_STATE;
lovkesh_chauhan 24:74f170dfd425 473 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 474 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 475 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 476 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 477 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 478 sprintf(buf, "field2=%d&status=MQTTPUBLISH",FAN_STATE);
lovkesh_chauhan 24:74f170dfd425 479 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 480 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 481 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 482 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 483 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 484 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 485 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 486 }
lovkesh_chauhan 24:74f170dfd425 487
lovkesh_chauhan 24:74f170dfd425 488 void publish_field3(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 489 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 490 if(ALARM_STATE == last_update)
lovkesh_chauhan 24:74f170dfd425 491 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 492 last_update = ALARM_STATE;
lovkesh_chauhan 24:74f170dfd425 493 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 494 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 495 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 496 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 497 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 498 sprintf(buf, "field3=%d&status=MQTTPUBLISH",ALARM_STATE);
lovkesh_chauhan 24:74f170dfd425 499 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 500 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 501 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 502 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 503 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 504 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 505 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 506 }
lovkesh_chauhan 24:74f170dfd425 507
lovkesh_chauhan 24:74f170dfd425 508 void publish_field4(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 509 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 510 last_update = WATER_LEVEL;
lovkesh_chauhan 24:74f170dfd425 511 //logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 512 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 513 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 514 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 515 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 516 //field5=%0.1f&field6=%0.1f&field7=%0.1f&field8=%0.1f&
lovkesh_chauhan 24:74f170dfd425 517 sprintf(buf, "field4=%d&field1=%d&field2=%d&field3=%d&status=MQTTPUBLISH",WATER_LEVEL, PUMP_STATE,FAN_STATE,ALARM_STATE);
lovkesh_chauhan 24:74f170dfd425 518 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 519 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 520 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 521 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 522 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 523 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 524 //logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 525 }
lovkesh_chauhan 24:74f170dfd425 526
lovkesh_chauhan 24:74f170dfd425 527 void publish_field5(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 528 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 529 last_update = GAS_level;
lovkesh_chauhan 24:74f170dfd425 530 //logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 531 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 532 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 533 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 534 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 535 sprintf(buf, "field5=%d&field6=%d&field7=%d&field8=%d&status=MQTTPUBLISH",GAS_level, CO_level, SMOKE_level, TEMPERATURE);
lovkesh_chauhan 24:74f170dfd425 536 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 537 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 538 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 539 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 540 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 541 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 542 //logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 543 }
lovkesh_chauhan 24:74f170dfd425 544
lovkesh_chauhan 24:74f170dfd425 545 void publish_field6(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 546 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 547 if(CO_level == last_update)
lovkesh_chauhan 24:74f170dfd425 548 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 549 last_update = CO_level;
lovkesh_chauhan 24:74f170dfd425 550 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 551 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 552 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 553 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 554 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 555 sprintf(buf, "field6=%d&status=MQTTPUBLISH",CO_level);
lovkesh_chauhan 24:74f170dfd425 556 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 557 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 558 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 559 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 560 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 561 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 562 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 563 }
lovkesh_chauhan 24:74f170dfd425 564
lovkesh_chauhan 24:74f170dfd425 565 void publish_field8(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 566 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 567 if(TEMPERATURE == last_update)
lovkesh_chauhan 24:74f170dfd425 568 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 569 last_update = TEMPERATURE;
lovkesh_chauhan 24:74f170dfd425 570 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 571 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 572 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 573 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 574 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 575 sprintf(buf, "field8=%d&status=MQTTPUBLISH",TEMPERATURE);
lovkesh_chauhan 24:74f170dfd425 576 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 577 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 578 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 579 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 580 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 581 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 582 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 583 }
lovkesh_chauhan 24:74f170dfd425 584
lovkesh_chauhan 24:74f170dfd425 585 void publish_field7(){ //Function to publish the values to ThingSpeak fields
lovkesh_chauhan 24:74f170dfd425 586 static int last_update = -1;
lovkesh_chauhan 24:74f170dfd425 587 if(SMOKE_level == last_update)
lovkesh_chauhan 24:74f170dfd425 588 return; //Ignore unncessary update in case no chnage in value.
lovkesh_chauhan 24:74f170dfd425 589 last_update = SMOKE_level;
lovkesh_chauhan 24:74f170dfd425 590 logMessage("Publish Routine\r\n");
lovkesh_chauhan 24:74f170dfd425 591 int rc = 0;
lovkesh_chauhan 24:74f170dfd425 592 MQTT::Message message;
lovkesh_chauhan 24:74f170dfd425 593 char* topic = "channels/1608275/publish";//Topic contaning Channel ID
lovkesh_chauhan 24:74f170dfd425 594 char buf[100]; //Buffer for storing data
lovkesh_chauhan 24:74f170dfd425 595 sprintf(buf, "field7=%d&status=MQTTPUBLISH",SMOKE_level);
lovkesh_chauhan 24:74f170dfd425 596 message.qos = MQTT::QOS0; //MQTT QOS 0 used. Best effort QOS
lovkesh_chauhan 24:74f170dfd425 597 message.retained = false;
lovkesh_chauhan 24:74f170dfd425 598 message.dup = false;
lovkesh_chauhan 24:74f170dfd425 599 message.payload = (void*)buf; //Copy buffer to Message Payload
lovkesh_chauhan 24:74f170dfd425 600 message.payloadlen = strlen(buf)+1;
lovkesh_chauhan 24:74f170dfd425 601 rc = client.publish(topic, message); //Publsih data using MQTT client
lovkesh_chauhan 24:74f170dfd425 602 logMessage("Publish rc: %d, Buf: %s\r\n", rc, buf);
lovkesh_chauhan 24:74f170dfd425 603 }
icraggs 2:638c854c0695 604
icraggs 2:638c854c0695 605 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 606 {
lovkesh_chauhan 24:74f170dfd425 607 smoke_detector.start_measurement(2); //Start periodic Smoke detector measurements at intervals of 10s
lovkesh_chauhan 24:74f170dfd425 608 temperature.start_measurement(20); //Start periodic Temperaturre readings. At interval of 20s
lovkesh_chauhan 24:74f170dfd425 609 water_pump.start_measurement(2); //Start Water Pump measurements. At intervals of 2s. This is small value because for test purpose very small tank is being used. In real life this could be in mins.
lovkesh_chauhan 24:74f170dfd425 610 water_pump.pump_on(); //Start the pump for 1st time
Jan Jongboom 20:49c9daf2b0ff 611 float version = 0.6;
Jan Jongboom 20:49c9daf2b0ff 612 logMessage("HelloMQTT: version is %.2f\r\n", version);
lovkesh_chauhan 24:74f170dfd425 613 int ret = 0;
lovkesh_chauhan 24:74f170dfd425 614
lovkesh_chauhan 24:74f170dfd425 615 if (!network) { //Check if ethernet connection successful
Jan Jongboom 20:49c9daf2b0ff 616 return -1;
Jan Jongboom 20:49c9daf2b0ff 617 }
Jan Jongboom 20:49c9daf2b0ff 618
lovkesh_chauhan 24:74f170dfd425 619 const char* hostname = "mqtt3.thingspeak.com"; //Thingspeak host name
lovkesh_chauhan 24:74f170dfd425 620 int port = 1883; //MQTT default port number
Jan Jongboom 20:49c9daf2b0ff 621 logMessage("Connecting to %s:%d\r\n", hostname, port);
lovkesh_chauhan 24:74f170dfd425 622 int rc = mqttNetwork.connect(hostname, port); //Attempt to connect to MQTT host
icraggs 6:e4c690c45021 623 if (rc != 0)
lovkesh_chauhan 24:74f170dfd425 624 logMessage("rc- TCP connect: %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 625
lovkesh_chauhan 24:74f170dfd425 626 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; //Build MQTT Client connection data
lovkesh_chauhan 24:74f170dfd425 627 data.MQTTVersion = 3; //MQTT Version
lovkesh_chauhan 24:74f170dfd425 628 data.clientID.cstring = "FA8KKzQ1CDY6EjENATYTESc"; //Thingspeak MQTT Client ID
lovkesh_chauhan 24:74f170dfd425 629 data.username.cstring = "FA8KKzQ1CDY6EjENATYTESc";//Thingspeak MQTT Username
lovkesh_chauhan 24:74f170dfd425 630 data.password.cstring = "vVdiiGscVIGLEgQIZ250Unxc";//Thingspeak MQTT Password
lovkesh_chauhan 24:74f170dfd425 631
lovkesh_chauhan 24:74f170dfd425 632
lovkesh_chauhan 24:74f170dfd425 633 if ((rc = client.connect(data)) != 0) //Connect Client
lovkesh_chauhan 24:74f170dfd425 634 logMessage("rc- connect: %d\r\n", rc);
lovkesh_chauhan 24:74f170dfd425 635
lovkesh_chauhan 24:74f170dfd425 636 char* topic = "channels/1608275/subscribe/fields/field1"; //Subscribe for field1
lovkesh_chauhan 24:74f170dfd425 637 if ((rc = client.subscribe(topic, MQTT::QOS0, field1_pump_state)) != 0)//Use field1_pump_state function pointer as call back function
lovkesh_chauhan 24:74f170dfd425 638 logMessage("rc1: %d\r\n", rc);
lovkesh_chauhan 24:74f170dfd425 639
lovkesh_chauhan 24:74f170dfd425 640 topic = "channels/1608275/subscribe/fields/field2";
lovkesh_chauhan 24:74f170dfd425 641 if ((rc = client.subscribe(topic, MQTT::QOS0, field2_fan_state)) != 0)//Use field2_fan_state function pointer as call back function
lovkesh_chauhan 24:74f170dfd425 642 logMessage("rc2: %d\r\n", rc);
icraggs 0:0cae29831d01 643
lovkesh_chauhan 24:74f170dfd425 644 topic = "channels/1608275/subscribe/fields/field3";
lovkesh_chauhan 24:74f170dfd425 645 if ((rc = client.subscribe(topic, MQTT::QOS0, field3_alarm_state)) != 0)//Use field3_alarm_state function pointer as call back function
lovkesh_chauhan 24:74f170dfd425 646 logMessage("rc: %d\r\n", rc);
lovkesh_chauhan 24:74f170dfd425 647
lovkesh_chauhan 24:74f170dfd425 648 while (1){
lovkesh_chauhan 24:74f170dfd425 649 logMessage("GAS: %d, CO: %d, Smoke: %d, TEMP = %0.1f, Water = %d", GAS_level, CO_level, SMOKE_level, TEMPERATURE, WATER_LEVEL);
lovkesh_chauhan 24:74f170dfd425 650 //publish_field1();
lovkesh_chauhan 24:74f170dfd425 651 //wait(1);
lovkesh_chauhan 24:74f170dfd425 652 //publish_field2();
lovkesh_chauhan 24:74f170dfd425 653 //wait(1);
lovkesh_chauhan 24:74f170dfd425 654 //publish_field3();
lovkesh_chauhan 24:74f170dfd425 655 //wait(1);
lovkesh_chauhan 24:74f170dfd425 656 //publish_field4();
lovkesh_chauhan 24:74f170dfd425 657 //wait(5);
lovkesh_chauhan 24:74f170dfd425 658 //publish_field5();
lovkesh_chauhan 24:74f170dfd425 659 //wait(5);
lovkesh_chauhan 24:74f170dfd425 660 //publish_field5();
lovkesh_chauhan 24:74f170dfd425 661 //wait(1);
lovkesh_chauhan 24:74f170dfd425 662 //publish_field6();
lovkesh_chauhan 24:74f170dfd425 663 //wait(1);
lovkesh_chauhan 24:74f170dfd425 664 //publish_field7();
lovkesh_chauhan 24:74f170dfd425 665 //wait(1);
lovkesh_chauhan 24:74f170dfd425 666 //publish_field8();
lovkesh_chauhan 24:74f170dfd425 667 client.yield(50);
lovkesh_chauhan 24:74f170dfd425 668 //client.connect(data);
lovkesh_chauhan 24:74f170dfd425 669 }
icraggs 17:0811bdbdd78a 670 if ((rc = client.unsubscribe(topic)) != 0)
Jan Jongboom 20:49c9daf2b0ff 671 logMessage("rc from unsubscribe was %d\r\n", rc);
icraggs 8:a3e3113054a1 672 if ((rc = client.disconnect()) != 0)
Jan Jongboom 20:49c9daf2b0ff 673 logMessage("rc from disconnect was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 674 mqttNetwork.disconnect();
Jan Jongboom 20:49c9daf2b0ff 675 logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
icraggs 0:0cae29831d01 676 return 0;
icraggs 0:0cae29831d01 677 }