· 4 years ago · Jul 18, 2021, 01:30 PM
1Week-2
2AIM: To interface LED/Buzzer with Arduino/Raspberry Pi and write a program to turn ON LED for 1 sec after every 2 seconds
3
4Source Code:
5int led=13;
6int buzzer=11;
7void setup() {
8pinMode(13, OUTPUT);
9pinMode(11, OUTPUT);
10
11}
12void loop() {
13digitalWrite(13, HIGH);
14digitalWrite(11, HIGH);
15delay(1000);
16digitalWrite(13, LOW);
17digitalWrite(11, LOW);
18delay(2000);
19}
20-------------------------------------------------------------------------------------------------
21WEEK-3
22Aim: To interface Push button/Digital sensor (IR/LDR) with Arduino/Raspberry Pi and write a program to turn ON LED when push button is pressed or at sensor detection.
23
24Source code:
25int IRPin = 3;
26int led=13;
27int value;
28void setup(){
29pinMode(IRPin, INPUT);
30Serial.begin(9600);
31pinMode(13,OUTPUT);
32
33}
34void loop(){
35 value = digitalRead(IRPin);
36Serial.println(value);
37 if(digitalRead(IRPin)==1)
38 digitalWrite(led,HIGH);
39 else
40 digitalWrite(led,LOW);
41}
42-------------------------------------------------------------------------------------------------
43WEEK-4
44AIM: To interface DHT11 sensor with Arduino/Raspberry Pi and write a program to print temperature and humidity readings.
45Source Code:
46
47#include "DHT.h"
48#define DHTPIN 2
49//#define DHTTYPE DHT11 // DHT 11
50#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
51//#define DHTTYPE DHT21 // DHT 21 (AM2301)
52DHT dht(DHTPIN, DHTTYPE);
53void setup() {
54 Serial.begin(9600);
55Serial.println(F("DHTxx test!"));
56dht.begin();
57}
58void loop() {
59 delay(2000);
60 float h = dht.readHumidity();
61 // Read temperature as Celsius (the default)
62 float t = dht.readTemperature();
63 // Read temperature as Fahrenheit (isFahrenheit = true)
64 float f = dht.readTemperature(true);
65 if (isnan(h) || isnan(t) || isnan(f)) {
66 Serial.println(F("Failed to read from DHT sensor!"));
67 return;
68 }
69 float hif = dht.computeHeatIndex(f, h);
70 float hic = dht.computeHeatIndex(t, h, false);
71 Serial.print(F("Humidity: "));
72 Serial.print(h);
73 Serial.print(F("% Temperature: "));
74 Serial.print(t);
75 Serial.print(F("°C "));
76 Serial.print(f);
77 Serial.print(F("°F Heat index: "));
78 Serial.print(hic);
79 Serial.print(F("°C "));
80 Serial.print(hif);
81 Serial.println(F("°F"));
82}
83-------------------------------------------------------------------------------------------------
84WEEK-5
85Aim: To interface Bluetooth with Arduino/Raspberry Pi and write a program to send sensor data to smart phone using Bluetooth.
86
87Source Code:
88#include <SoftwareSerial.h>
89SoftwareSerial BTserial(10, 11); // RX | TX
90void setup() {
91BTserial.begin(9600); }
92void loop() {
93BTserial.print("hai");
94BTserial.print("@@");
95BTserial.print("W@LCOM@ TO iot LAB");
96BTserial.print(";");
97delay(3000);
98}
99-------------------------------------------------------------------------------------------------
100Week-6
101AIM: To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED ON/OFF when ‘1’/’0’ is received from smart phone using Bluetooth.
102SOURCE CODE:
103
104#include <SoftwareSerial.h>
105SoftwareSerial Bluetooth(10, 9); // RX, TX
106int LED = 13; // the on-board LED
107int Data; // the data received
108
109
110void setup() {
111 Bluetooth.begin(9600);
112 Serial.begin(9600);
113 Serial.println("Waiting for command...");
114 Bluetooth.println("Send 1 to turn on the LED. Send 0 to turn Off");
115 pinMode(LED,OUTPUT);
116
117}
118
119void loop() {
120 if (Bluetooth.available()){ //wait for data received
121 Data=Bluetooth.read();
122 if(Data=='1'){
123 digitalWrite(LED,1);
124 Serial.println("LED On!");
125 Bluetooth.println("LED On!");
126 }
127 else if(Data=='0'){
128 digitalWrite(LED,0);
129 Serial.println("LED Off!");
130 Bluetooth.println("LED On D13 Off ! ");
131 }
132 else{;}
133 }
134delay(100);
135}
136-------------------------------------------------------------------------------------------------
137WEEK-7
138Aim: Write a program on Arduino/Raspberry Pi to upload temperature and humidity data to cloud.
139SOURCE CODE:
140#include <DHT.h>
141#include <DHT_U.h>
142#include <DHT.h>
143#include <ESP8266WiFi.h>
144
145String apiKey = "WAHKMAHIYUR1SEFU";
146const char *ssid = "chitti";
147const char *pass = "sari123#";
148const char* server = "api.thingspeak.com";
149
150#define DHTPIN 0
151
152DHT dht(DHTPIN, DHT11);
153
154WiFiClient client;
155 void setup()
156{
157 Serial.begin(115200);
158delay(10);
159dht.begin();
160Serial.println("Connecting to ");
161Serial.println(ssid);
162WiFi.begin(ssid, pass);
163while (WiFi.status() != WL_CONNECTED)
164{
165delay(500);
166Serial.print(".");
167}
168Serial.println("");
169Serial.println("WiFi connected");
170
171}
172void loop()
173{
174
175float h = dht.readHumidity();
176float t = dht.readTemperature();
177
178if (isnan(h) || isnan(t))
179{
180Serial.println("Failed to read from DHT sensor!");
181return;
182}
183
184if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
185{
186
187String postStr = apiKey;
188postStr +="&field1=";
189postStr += String(t);
190postStr +="&field2=";
191postStr += String(h);
192postStr += "\r\n\r\n";
193
194client.print("POST /update HTTP/1.1\n");
195client.print("Host: api.thingspeak.com\n");
196client.print("Connection: close\n");
197client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
198client.print("Content-Type: application/x-www-form-urlencoded\n");
199client.print("Content-Length: ");
200client.print(postStr.length());
201client.print("\n\n");
202client.print(postStr);
203
204Serial.print("Temperature: ");
205Serial.print(t);
206Serial.print(" degrees Celcius, Humidity: ");
207Serial.print(h);
208Serial.println("%. Send to Thingspeak.");
209}
210client.stop();
211
212Serial.println("Waiting...");
213
214delay(10000);
215}
216-------------------------------------------------------------------------------------------------
217WEEK-8
218
219Aim: Write a program on Arduino/Raspberry Pi to retrieve temperature and humidity data from cloud.
220Source Code:
221#include <DHT.h>
222#include <DHT_U.h>
223
224// Robo India Tutorial
225// Simple code upload the tempeature and humidity data using thingspeak.com
226// Hardware: NodeMCU,DHT22
227
228#include <DHT.h> // Including library for dht
229
230#include <ESP8266WiFi.h>
231
232String apiKey = "6YL93NXXHFZ964NA"; // Enter your Write API key from ThingSpeak
233const char *ssid = "chitti"; // replace with your wifi ssid and wpa2 key
234const char *pass = "sari123#";
235const char* server = "api.thingspeak.com";
236
237#define DHTPIN 0 //pin where the dht11 is connected
238
239DHT dht(DHTPIN, DHT11);
240
241WiFiClient client;
242
243void setup()
244{
245 Serial.begin(115200);
246 delay(10);
247 dht.begin();
248
249 Serial.println("Connecting to ");
250 Serial.println(ssid);
251
252
253WiFi.begin(ssid, pass);
254
255while (WiFi.status() != WL_CONNECTED)
256{
257delay(500);
258Serial.print(".");
259}
260Serial.println("");
261Serial.println("WiFi connected");
262
263}
264
265void loop()
266{
267
268float h = dht.readHumidity();
269float t = dht.readTemperature();
270
271if (isnan(h) || isnan(t))
272{
273Serial.println("Failed to read from DHT sensor!");
274return;
275}
276
277if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
278{
279
280String postStr = apiKey;
281postStr +="&field1=";
282postStr += String(t);
283postStr +="&field2=";
284postStr += String(h);
285postStr += "\r\n\r\n";
286
287client.print("POST /update HTTP/1.1\n");
288client.print("Host: api.thingspeak.com\n");
289client.print("Connection: close\n");
290client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
291client.print("Content-Type: application/x-www-form-urlencoded\n");
292client.print("Content-Length: ");
293client.print(postStr.length());
294client.print("\n\n");
295client.print(postStr);
296
297Serial.print("Temperature: ");
298Serial.print(t);
299Serial.print(" degrees Celcius, Humidity: ");
300Serial.print(h);
301Serial.println("%. Send to Thingspeak.");
302}
303client.stop();
304
305Serial.println("Waiting...");
306
307// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
308delay(10000);
309}
310-------------------------------------------------------------------------------------------------
311Aim:IoT Project using Ultrasonic Sensor HC-SR04 and Arduino to distance calculation using Processing App
312#include <Mouse.h>
313
314const int trigpin= 8;
315const int echopin= 7;
316long duration;
317int distance;
318void setup(){
319 pinMode(trigpin,OUTPUT);
320 pinMode(echopin,INPUT);
321 Serial.begin(9600);
322}
323
324void loop(){
325 digitalWrite(trigpin,HIGH);
326 delayMicroseconds(10);
327 digitalWrite(trigpin,LOW);
328 duration=pulseIn(echopin,HIGH);
329 distance = duration*0.034/2;
330 Serial.println(distance);
331}
332
333
334
335
336