IoT_LAB
IoT_LAB
F − Serial monitor used to receive serial data from the board and send the serial data to the
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
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():
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
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
// 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
***************************************/
void setup() {
Serial.begin(9600);
delay(10);
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());
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();
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();
}
*/
}
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
Start sampling