0% found this document useful (0 votes)
238 views5 pages

EXP-9 Communicate Between Arduino and Raspberry PI Using Any Wireless Medium

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

EXP-9 Communicate Between Arduino and Raspberry PI Using Any Wireless Medium

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

EXPT.

NO 09 COMMUNICATE BETWEEN ARDUINO AND RASPBERRY PI USING


DATE: ANY WIRELESS MEDIUM

AIM: To Communicate between Arduino and Raspberry PI using wireless medium-ESP01(WIFI Module)

HARDWARE / SOFTWARE REQUIREMENTS:


1. Raspberry PI
2. ESP01
3. Arduino UNO

PROCEDURE:

1. Connect pin TX of ESP0I to RX (set as 3) of Arduino UNO.


2. Connect pin RX of ESP0I to TX (set as 2) of Arduino UNO.
3. Connect pin ENABLE of ESP01 to 3.3v(high).
4. Connect pin GND of ESP01 to GND in Arduino UNO.
5. Connect VCC of ESP01 to 3.3v in Arduino UNO.
6. Create a Wifi hotspot to get data connectivity for ESP01.
7. Connect Raspberry pi and Arduino to PC using USB-serial communication cable.
8. Type and run the program.
9. After execution copy the IP address from Thonny IDE (Raspberry pi environment) to Arduino IDE
program.
10. Run the Arduino program.
11. Check the output received in Thonny IDE (Raspberry pi environment) for random number generated using
Arduino program.

PROGRAM:

1. Program to send data from Arduino

#include <SoftwareSerial.h>
#define RX 3
#define TX 2
String AP = "A54"; // AP NAME
String PASS = "12345678"; // AP PASSWORD
String HOST = "[Link]";
String PORT = "1234";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);

void setup() {
[Link](9600);
[Link](115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
/* String ip = "AT+CIFSR";
sendCommand(ip,5,"OK");
while([Link]())
{
// The esp has data so display its output to the serial window
char c = [Link](); // read the next character.
[Link](c);
break;
}*/
}

void loop() {
valSensor = getSensorData();
String getData = String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"UDP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String([Link]()),4,">");
[Link](getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}

int getSensorData(){
return random(1000); // Replace with your own sensor code
}

void sendCommand(String command, int maxTime, char readReplay[]) {


[Link](countTrueCommand);
[Link](". at command => ");
[Link](command);
[Link](" ");
while(countTimeCommand < (maxTime*1))
{
[Link](command);//at+cipsend
if([Link](readReplay))//ok
{
found = true;
break;
}

countTimeCommand++;
}

if(found == true)
{
[Link]("OYI");
countTrueCommand++;
countTimeCommand = 0;
}

if(found == false)
{
[Link]("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}

2. Program to receive data using Raspberry Pi:

import network
import socket
import time

wlan = [Link](network.STA_IF)
[Link](True)
[Link]("A54","12345678")

# rgb led
led1=[Link](2,[Link])
led2=[Link](3,[Link])

# Wait for connect or fail


wait = 10
while wait > 0:
if [Link]() < 0 or [Link]() >= 3:
break
wait -= 1
print('waiting for connection...')
[Link](1)

# Handle connection error


if [Link]() != 3:
raise RuntimeError('wifi connection failed')
else:
print('connected')
ip=[Link]()[0]
print('IP: ', ip)

localIP = [Link]()[0]
localPort = 1234
bufferSize = 1024

# Create a datagram socket


UDPServerSocket = [Link](socket.AF_INET,socket.SOCK_DGRAM)

# Bind to address and ip


[Link]((localIP, localPort))
print('waiting....')

while True:
bytesAddressPair = [Link](bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
m=str([Link]())
m=[Link]()
print(m)
OUTPUT:

RESULT:

Thus the communication between Arduino and Raspberry PI using wireless medium-ESP01(WIFI Module) has
been done.

You might also like