Project description
Code
Arduino Based Smart IoT Food Quality Monitoring System
c_cpp
1//Program to 2 3#include <SoftwareSerial.h> 4 5#include <LiquidCrystal.h> 6 7#include <dht.h> // include dht sensor library 8 9LiquidCrystal lcd(10, 9, 5, 4, 3, 2); 10 11dht DHT; 12 13float t=0; 14 15float h=0; 16 17#define DHT11_PIN 8 18 19#define LDR_PIN A1 20 21#define MQ3_PIN A0 22 23char data = 0; 24 25int ldr_read = 0; 26 27int gas_read = 0; 28 29// replace with your channel's thingspeak API key 30 31String apiKey = "[API KEY FROM THINGSPEAK]"; 32 33// connect 10 to TX of Serial USB 34 35// connect 11 to RX of serial USB 36 37SoftwareSerial ser(12,13); // RX, TX 38 39void setup() 40 41{ 42 43 // enable software serial 44 45 ser.begin(9600); 46 47 lcd.begin(16,2); 48 49 50 51 52 // pinMode(LDR_PIN, INPUT); 53 54 //pinMode(MQ3_PIN, INPUT); 55 56 57 58 59 lcd.setCursor(1,0); 60 61 lcd.print("**SmarT FooD**"); 62 63 lcd.setCursor(3,1); 64 65 lcd.print("MonitorinG"); 66 67 Serial.begin(9600); 68 69 70 71 72 // reset ESP8266 WiFi connection AT+CIPMUX=1 AT+CWJAP 73 74 ser.println("AT"); 75 76 delay(1000); 77 78 ser.println("AT+GMR"); 79 80 delay(1000); 81 82 ser.println("AT+CWMODE=3"); 83 84 delay(1000); 85 86 ser.println("AT+RST"); 87 88 delay(5000); 89 90 ser.println("AT+CIPMUX=1"); 91 92 delay(1000); 93 94 String cmd="AT+CWJAP=\\"[YOUR SSID]\\",\\"[PASSWORD]\\""; 95 96 ser.println(cmd); 97 98 delay(1000); 99 100 ser.println("AT+CIFSR"); 101 102 delay(1000); 103 104 } 105 106// the loop 107 108void loop() 109 110{ 111 112 lcd.clear(); 113 114 ldr_read = analogRead(LDR_PIN); 115 116 lcd.setCursor(0,0); 117 118 lcd.print("LDR-"); 119 120 lcd.setCursor(4,0); 121 122 lcd.print(ldr_read); 123 124 125 126 127 lcd.setCursor(0,1); 128 129 lcd.print("MQ3-"); 130 131 gas_read = analogRead(MQ3_PIN); 132 133 lcd.setCursor(4,1); 134 135 lcd.print(gas_read); 136 137 138 139 140 int chk = DHT.read11(DHT11_PIN); 141 142 Serial.print("Temperature = "); 143 144 t = DHT.temperature; 145 146 147 148 149 //lcd.clear(); 150 151 lcd.setCursor(8,0); 152 153 lcd.print("Tem-"); 154 155 156 157 158 lcd.setCursor(12,0); 159 160 lcd.print(t); 161 162 Serial.println(t); 163 164 165 166 167 Serial.print("Humidity = "); 168 169 h = DHT.humidity; 170 171 lcd.setCursor(8,1); 172 173 lcd.print("Hum-"); 174 175 lcd.setCursor(12,1); 176 177 lcd.print(h); 178 179 Serial.println(h); 180 181 182 183 184 esp_8266(); 185 186} 187 188void esp_8266() 189 190{ 191 192 // TCP connection AT+CIPSTART=4,"TCP","184.106.153.149",80 193 194 String cmd = "AT+CIPSTART=4,\\"TCP\\",\\""; 195 196 cmd += "184.106.153.149"; // api.thingspeak.com 197 198 cmd += "\\",80"; 199 200 ser.println(cmd); 201 202 Serial.println(cmd); 203 204 if(ser.find("Error")) 205 206 { 207 208 Serial.println("AT+CIPSTART error"); 209 210 return; 211 212 } 213 214 215 216 217 // prepare GET string GET https://api.thingspeak.com/update?api_key=LHAG4NSIYJ5UWS6U&field1=0\ \ 218\ \ 219 220 221 String getStr = "GET /update?api_key="; 222 223 getStr += apiKey; 224 225 getStr +="&field1="; 226 227 getStr +=String(t); 228 229 getStr +="&field2="; 230 231 getStr +=String(h); 232 233 getStr +="&field3="; 234 235 getStr +=String(ldr_read); 236 237 getStr +="&field4="; 238 239 getStr +=String(gas_read); 240 241 getStr += "\ \ 242\ \ 243"; 244 245 // send data length 246 247 cmd = "AT+CIPSEND=4,"; 248 249 cmd += String(getStr.length()); 250 251 ser.println(cmd); 252 253 Serial.println(cmd); 254 255 delay(1000); 256 257 ser.print(getStr); 258 259 Serial.println(getStr); 260 261 262 263 264 // thingspeak needs 15 sec delay between updates 265 266 delay(3000); 267 268} 269 270
Downloadable files
Arduino-IoT-Food-Quality-Monitor
Arduino-IoT-Food-Quality-Monitor
Arduino-IoT-Food-Quality-Monitor
Arduino-IoT-Food-Quality-Monitor
Comments
Only logged in users can leave comments
TechnicalEngineer
0 Followers
•0 Projects
Table of contents
Intro
0
0