0% found this document useful (0 votes)
2 views21 pages

IoT_LAB

The document outlines a series of experiments using Arduino and Raspberry Pi, detailing software setup, code examples, and circuit connections for various projects. Each experiment includes specific code snippets for controlling hardware components, reading sensors, and connecting to networks. The document serves as a practical guide for beginners to learn about programming and interfacing with microcontrollers.

Uploaded by

JARVIS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views21 pages

IoT_LAB

The document outlines a series of experiments using Arduino and Raspberry Pi, detailing software setup, code examples, and circuit connections for various projects. Each experiment includes specific code snippets for controlling hardware components, reading sensors, and connecting to networks. The document serves as a practical guide for beginners to learn about programming and interfacing with microcontrollers.

Uploaded by

JARVIS
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

EXPERIMENT – 1

Download Arduino IDE Software.

Launch Arduino IDE and Open your first project.

A − Used to check if there is any compilation error.


B − Used to upload a program to the Arduino board.

C − Shortcut used to create a new sketch.

D − Used to directly open one of the example sketch.

E − Used to save your sketch.

F − Serial monitor used to receive serial data from the board and send the serial data to the
board.

Select your Arduino board.

Select your serial port and Upload the program to your board
EXPERIMENT -2

Code
void setup() {

pinMode(D0,OUTPUT

);

void loop() {

digitalWrite(D0,HIG

H);delay(2000);

digitalWrite(D0,LOW

); delay(2000);

}
EXPERIMENT – 3

Code

void setup() {
pinMode(12,OUTPUT);
//R
pinMode(11,OUTPUT);
//G
pinMode(10,OUTPUT);
//B
}

void loop() {
digitalWrite(12,HIG
H);delay(500);
digitalWrite(12,LOW
);
digitalWrite(11,HIG
H);delay(500);
digitalWrite(11,LOW
);
digitalWrite(10,HIG
H);delay(500);
digitalWrite(12,LOW
);
digitalWrite(11,LOW
);
digitalWrite(10,LOW
);delay(2000);}
EXPERIMENT -4

Code

int outputpin =

A0;void setup()
{
Serial.begin(9600);
pinMode(D1,OUTP
UT);
}
void loop() //main loop
{
int analogValue = analogRead(outputpin);
float millivolts = (analogValue/1024.0) * 3300; //3300 is the voltage provided by
NodeMCU float celsius = (millivolts/10)*5;
Serial.print("in DegreeC=
");Serial.println(celsius);
delay(1000);
if(celsius>=10)
{
digitalWrite(D1,HIGH);
}
else{ digitalWrite(D1,LOW);}}
EXPERIMENT – 5
Circuit connection:

Code:-

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
String tagId = "None";
byte nuidPICC[4];

void setup(void)
{
Serial.begin(115200);
Serial.println("System initialized");
nfc.begin();
}

void loop()
{
readNFC();
}

void readNFC()
{
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
tag.print();
tagId = tag.getUidString();
}
delay(5000);
}

OBSERVATION
EXPERIMENT - 6

Following is the configuration tool in PIXEL desktop −


EXPERIMENT – 7

Circuit connection:
Code :
#!/usr/bin/env python
import RPi.GPIO as GPIO # RPi.GPIO can be referred as GPIO from now
import time

ledPin = 22 # pin22

def setup():
GPIO.setmode(GPIO.BOARD) # GPIO Numbering of Pins
GPIO.setup(ledPin, GPIO.OUT) # Set ledPin as output
GPIO.output(ledPin, GPIO.LOW) # Set ledPin to LOW to turn Off the LED

def loop():
while True:
print 'LED on'
GPIO.output(ledPin, GPIO.HIGH) # LED On
time.sleep(1.0) # wait 1 sec
print 'LED off'
GPIO.output(ledPin, GPIO.LOW) # LED Off
time.sleep(1.0) # wait 1 sec
def endprogram():

GPIO.output(ledPin, GPIO.LOW) # LED Off


GPIO.cleanup() # Release resources

if __name__ == '__main__': # Program starts from here


setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the destroy() will be executed.
endprogram().
EXPERIMENT – 8

Circuit connection:

STEP 1
STEP 2

STEP 3
Code:
int led = 13;

int received = 0;
int i;

void setup() {

Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop() {
if (Serial.available() > 0) {

received = Serial.read();
if (received == 'a'){

digitalWrite(led, HIGH);
delay(2000);
digitalWrite(led, LOW);
}
else if (received == 'b'){

for(i=0;i<5;i++){

digitalWrite(led, HIGH);
delay(1000);

digitalWrite(led, LOW);
delay(1000);
}

}
EXPERIMENT – 9

Circuit connection:

1. Now edit the wifi and Adafruit io credentials with correct information of example as shown
in below image.
Code :
/***************************************************
Adafruit MQTT Library ESP8266 Example

Must use ESP8266 Arduino from:


https://github.com/esp8266/Arduino

Works great with Adafruit's Huzzah ESP board & Feather


----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Tony DiCola for Adafruit Industries.


MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point


*********************************/

#define WLAN_SSID "...your SSID..."


#define WLAN_PASS "...your password..."

/************************* Adafruit.io Setup


*********************************/

#define AIO_SERVER "io.adafruit.com"


#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see
https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."

/************ Global State (you don't need to change this!)


******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.


WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT
server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,
AIO_USERNAME, AIO_KEY);

/****************************** Feeds
***************************************/

// Setup a feed called 'potValue' for publishing.


// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish potValue = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/potValue");

// Setup a feed called 'ledBrightness' for subscribing to changes.


Adafruit_MQTT_Subscribe ledBrightness = Adafruit_MQTT_Subscribe(&mqtt,
AIO_USERNAME "/feeds/ledBrightness");

/*************************** Sketch Code


************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function


declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

uint8_t ledPin = D6;


uint16_t potAdcValue = 0;
uint16_t ledBrightValue = 0;

void setup() {
Serial.begin(9600);
delay(10);

Serial.println(F("Adafruit MQTT demo"));

// Connect to WiFi access point.


Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();

Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());

// Setup MQTT subscription for ledBrightness feed.


mqtt.subscribe(&ledBrightness);
}

void loop() {
// Ensure the connection to the MQTT server is alive (this will make the
first
// connection and automatically reconnect when disconnected). See the
MQTT_connect
// function definition further below.
MQTT_connect();

// this is our 'wait for incoming subscription packets' busy subloop


// try to spend your time here

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(200))) {
if (subscription == &ledBrightness) {
Serial.print(F("Got LED Brightness : "));
ledBrightValue = atoi((char *)ledBrightness.lastread);
Serial.println(ledBrightValue);
analogWrite(ledPin, ledBrightValue);
}
}
// Now we can publish stuff!
uint16_t AdcValue = analogRead(A0);
if((AdcValue > (potAdcValue + 7)) || (AdcValue < (potAdcValue - 7))){
potAdcValue = AdcValue;
Serial.print(F("Sending pot val "));
Serial.print(potAdcValue);
Serial.print("...");
if (! potValue.publish(potAdcValue)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}

// Function to connect and reconnect as necessary to the MQTT server.


// Should be called in the loop function and it will take care if
connecting.
void MQTT_connect() {
int8_t ret;

// Stop if already connected.


if (mqtt.connected()) {
return;
}

Serial.print("Connecting to MQTT... ");

uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for
connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
EXPERIMENT - 10

Circuit connection:

Code:
13 #include <ESP8266WiFi.h>
14 #include <ArduinoJson.h>
15 #include "coap_client.h"
16
17 //instance for coapclient
18 coapClient coap;
19
20 //WiFi connection info
21 const char* ssid = "your-wifi-ssid";
22 const char* password = "your-wifi-password";
23 String DEVICE_SECRET_KEY = "your-device_secret_key";
24
25 //ip address and default port of coap server in which your
interested in
26 IPAddress ip(52, 17, 209, 228);
27 int port = 5683;
28 char* path = "events";
29
30 StaticJsonBuffer<200> jsonBuffer;
31 JsonObject& root = jsonBuffer.createObject();
32
33 // coap client response callback
34 void callback_response(coapPacket &packet, IPAddress ip, int port) {
35 char p[packet.payloadlen + 1];
36 memcpy(p, packet.payload, packet.payloadlen);
37 p[packet.payloadlen] = NULL;
38
39 //response from coap server
40 if (packet.type == 3 && packet.code == 0) {
41 Serial.println("ping ok");
42 }
43
44 Serial.println(p);
45 }
46
47 void setup() {
48 Serial.begin(115200);
49
50 WiFi.begin(ssid, password);
51 Serial.println(" ");
52
53 // Connection info to WiFi network
54 Serial.println();
55 Serial.println();
56 Serial.print("Connecting to ");
57 Serial.println(ssid);
58 WiFi.begin(ssid, password);
59 while (WiFi.status() != WL_CONNECTED) {
60 yield();
61 Serial.print(".");
62 }
63 Serial.println("");
64 Serial.println("WiFi connected");
65 // Print the IP address of client
66 Serial.println(WiFi.localIP());
67
68 // client response callback.
69 // this endpoint is single callback.
70 coap.response(callback_response);
71
72 // start coap client
73 coap.start();
74 }
75
76 void loop() {
77 root["name"] = "temperature";
78 root["data"] = 21.5;
79 root["accessToken"] = DEVICE_SECRET_KEY;
80
81 String data;
82 root.printTo(data);
83 char dataChar[data.length() + 1];
84 data.toCharArray(dataChar, data.length() + 1);
85 bool state;
86
87
88 //post request
89 //arguments server ip address,default port,resource name,
payload,payloadlength
90 int msgid = coap.post(ip, port, path, dataChar, data.length());
91
92 state = coap.loop();
93
94 delay(5000);
95 }
EXPERIMENT – 11

Setting up Edge Impulse

Create an Edge Impulse account through the Arduino platform


EXPERIMENT – 12

Collecting your first data

Start sampling

You might also like