0% found this document useful (0 votes)
93 views6 pages

IOT Lab Part

The document describes 5 programs: 1. Transmits strings over UART serial communication 2. Implements point-to-point communication between two ESP8266 modules over WiFi using UDP protocol 3. Extends program 2 to enable multi-point to single-point communication over a WiFi network 4. Studies I2C protocol by reading accelerometer sensor data over I2C 5. Reads temperature and humidity from DHT11 sensor and transmits values over serial.

Uploaded by

Pavan K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views6 pages

IOT Lab Part

The document describes 5 programs: 1. Transmits strings over UART serial communication 2. Implements point-to-point communication between two ESP8266 modules over WiFi using UDP protocol 3. Extends program 2 to enable multi-point to single-point communication over a WiFi network 4. Studies I2C protocol by reading accelerometer sensor data over I2C 5. Reads temperature and humidity from DHT11 sensor and transmits values over serial.

Uploaded by

Pavan K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Program 1 : Transmit a string using UART

String str1 = "Initializing UART";


String str2 = "IoT lab - BITM";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(str1);
Serial.println(str2);
}

void loop() {
// put your main code here, to run repeatedly:
long time = millis();
Serial.println("Runtime is:" + String(time));
delay(1000);
}
Program 2 : Point-to-Point communication of two Motes over the radio frequency.

// Server Side

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "harivanam";


const char *pass = "14081987";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);

// A UDP instance to let us send and receive packets over UDP


WiFiUDP udp;

char packetBuffer[9]; //Where we get the UDP data


//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(9600);
Serial.println();
WiFi.softAP(ssid, pass); //Create Access point

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb)
{
//If serial data is recived send it to UDP
if(Serial.available()>0)
{
udp.beginPacket(ClientIP, 2000);
//Send UDP requests are to port 2000
char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte
Serial.print(packetBuffer);
delay(20);
}
}

//Client Side

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "harivanam";


const char *pass = "14081987";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192,168,4,1);
IPAddress ClientIP(192,168,4,2);

// A UDP instance to let us send and receive packets over UDP


WiFiUDP udp;

char packetBuffer[9]; //Where we get the UDP data


//======================================================================
// Setup
//======================================================================
void setup()
{
Serial.begin(9600);
Serial.println();

WiFi.begin(ssid, pass); //Connect to access point

Serial.println("");

// Wait for connection


while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb)
{
//If serial data is recived send it to UDP
if(Serial.available()>0)
{
udp.beginPacket(ServerIP, 2000); //Send Data to Master unit
//Send UDP requests are to port 2000

char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
udp.read(packetBuffer, 1); // read the packet into the buffer, we are reading only one byte
Serial.print(packetBuffer);
delay(20);
}
}

Program 3 : Multi-point to single point communication of Motes over the radio frequency.LAN (Subnetting).

Same as 2 program consists of Server and 2 clients


Program 4. I2C protocol study

#include <Wire.h>
#include <ADXL345.h>
ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
void setup()
{
Serial.begin(9600);
adxl.powerOn();
}
void loop(){
//Boring accelerometer stuff
int x,y,z;
adxl.readXYZ(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z
// Output x,y,z values
Serial.print("values of X , Y , Z: ");
Serial.print(x);
Serial.print(" , ");
Serial.print(y);
Serial.print(" , ");
Serial.println(z);
double xyz[3];
double ax,ay,az;
adxl.getAcceleration(xyz);
ax = xyz[0];
ay = xyz[1];
az = xyz[2];
Serial.print("X=");
Serial.print(ax);
Serial.println(" g");
Serial.print("Y=");
Serial.print(ay);
Serial.println(" g");
Serial.print("Z=");
Serial.println(az);
Serial.println(" g");
Serial.println("**********************");
delay(500);
}
Program 5. Reading Temperature and Relative Humidity value from the sensor.

#include <DHT.h>
DHT dht(2, DHT11 );
float hum;
float temp;
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
hum = dht.readHumidity();
temp= dht.readTemperature();
Serial.print("Humidity: " + String(hum));
Serial.print(" %, Temp: " + String(temp));
Serial.println(" Celsius");
delay(2000);
}

You might also like