NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

BLINK ALL LEDS
import sys
import time
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python-Code- legacy/Adafruit_MCP230XX’)
from Adafruit_MCP230XX import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)

mcp.config(0,mcp.OUTPUT)
mcp.config(1,mcp.OUTPUT)
mcp.config(2,mcp.OUTPUT)
mcp.config(3,mcp.OUTPUT)
mcp.config(4,mcp.OUTPUT)
mcp.config(5,mcp.OUTPUT)
mcp.config(6,mcp.OUTPUT)
mcp.config(7,mcp.OUTPUT)
mcp.config(8,mcp.OUTPUT)

while True:
mcp.output(0,1)
time.sleep(1)
mcp.output(0,0)
time.sleep(1)
mcp.output(4,1)
time.sleep(1)
mcp.output(4,0)
time.sleep(1)
mcp.output(8,1)
time.sleep(1)
mcp.output(8,0)
time.sleep(1)
-----------------------------------------------------------------------------------------------------
TURN ON/OFF PUSH buttons
import sys
import time
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python-Code- legacy/Adafruit_MCP230XX’)
from Adafruit_MCP230XX import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)

mcp.config(0,mcp.OUTPUT)
mcp.config(1,mcp.OUTPUT)
mcp.config(2,mcp.OUTPUT)
mcp.config(3,mcp.OUTPUT)
mcp.config(4,mcp.OUTPUT)
mcp.config(5,mcp.OUTPUT)
mcp.config(6,mcp.OUTPUT)
mcp.config(7,mcp.OUTPUT)
mcp.config(8,mcp.OUTPUT)
mcp.config(9,mcp.INPUT)
mcp.pullup(9,0)
mcp.config(10,mcp.INPUT)
mcp.pullup(10,0)
i=0
while i!=4:
print(“Pin-9=”,mcp.input(9))
print(“Pin-10=”,mcp.input(10))
time.sleep(2)
i+=1
------------------------------------------------------------------------------------------------------
Program to glow LED1/LED2 when push button 1 or 2 is pressed else glow LED3
import sys
import time
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python-Code- legacy/Adafruit_MCP230XX’)
from Adafruit_MCP230XX import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)

mcp.config(0,mcp.OUTPUT)
mcp.config(1,mcp.OUTPUT)
mcp.config(2,mcp.OUTPUT)
mcp.config(3,mcp.OUTPUT)
mcp.config(4,mcp.OUTPUT)
mcp.config(5,mcp.OUTPUT)
mcp.config(6,mcp.OUTPUT)
mcp.config(7,mcp.OUTPUT)
mcp.config(8,mcp.OUTPUT)
mcp.config(9,mcp.INPUT)
mcp.pullup(9,0)
mcp.config(10,mcp.INPUT)
mcp.pullup(10,0)

while True:
if mcp.input(9):
mcp.output(0,1)
if mcp.input(10):
mcp.output(4,1)
else:
mcp.output(8,1)
-----------------------------------------------------------------------------------------
Program to turn on buzzer.
CODE:
import sys
import time
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python-Code- legacy/Adafruit_MCP230XX’)
from Adafruit_MCP230XX import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)

mcp.config(0,mcp.OUTPUT)
mcp.config(1,mcp.OUTPUT)
mcp.config(2,mcp.OUTPUT)
mcp.config(3,mcp.OUTPUT)
mcp.config(4,mcp.OUTPUT)
mcp.config(5,mcp.OUTPUT)
mcp.config(6,mcp.OUTPUT)
mcp.config(7,mcp.OUTPUT)
mcp.config(8,mcp.OUTPUT)
mcp.config(9,mcp.INPUT)
mcp.pullup(9,0)
mcp.config(10,mcp.INPUT)
mcp.pullup(10,0)
mcp.config(11,mcp.OUTPUT)
while True:
if mcp.input(9):
mcp.output(11,1)
if mcp.input(10):
mcp.output(11,0)
----------------------------------------------------------------------------------------------
Write a program to sense the weather using BME280 sensor.

CODE:

import sys
sys.path.append('/home/pi/ETS IOT KIT demo/DemoCode/BME288)
import EME280lib as bme
import time
while True:
temperature, pressure, humidity=bme.readBME280A11()
print ("Temperature : , temperature, "c")
print ("Pressure : ", pressure, "hPa")
print ("Humidity: humidity, "%")
time.sleep (2)
----------------------------------------------------------------------------------------------
Write a program to calculate the distance using ULTRASONIC SENSOR.

CODE:
import sys
import time
Import RPi.GPIO as GPIO
GPIO.setwarnings (False)
GPIO.setmode (GPIO.BOARD)|
GPIO_TRIGGER=16
GPIO_ECHO=18
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
GPIO.output(GPIO_TRIGGER, False))
time.sleep(1)
while True:
GPIO.output(GPIO_TRIGGER, True)
time.sleep(1)
GPIO.output(GPIO_TRIGGER, False)
while GPIO.input(GPIO_ECHO)==0:
start=time.time()
while GPIO. input(GPIO_ECHO)==1:
stop=time.time()

elapsed=stop-start
distance=elapsed*34300
distance=distance/2
print("Distance :",distance)
time.sleep(1)

--------------------------------------------------------------------------------------
PIR Sensor

import sys
import time
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python-Code- legacy/Adafruit_MCP230XX’)
from Adafruit_MCP230XX import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)
mcp.config(0,mcp.INPUT)
mcp.pullup(0,1)
while True:
i=mcp.input(0)
time.sleep(1)
if i==1:
print("DEtect")
time.sleep(1)
if i==0:
print("Not Detect")
time.sleep(1)
----------------------------------------------------------------------------------------
Program to sense the moisture level in ground soil using soil moisture sensor.

import sys
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

SPI_PORT =0
SPI_DEVICE =0

mcp=Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT,SPI_DEVICE))
while True:
t=mcp.read_adc(0)

p=(100-(t/1023.0)*100)
print('moisture content is',p)
time.sleep(3)
-------------------------------------------------------------------------------------------
Upload the weather information on to the open-source cloud “ThingSpeak”

import sys
import time
sys.path.append(“/home/pi/ETS_IoT KIT demo/DemoCode/BME280”)
import BME280lib as bme
import urllib.request as ur

while(True):
t,p,h=bme.readBME280All()
st=’https://api.thingspeak.com/update?api_key=8NRQFDOVGWPUL669&field1=’str(t)+’&field2=’
+str(p)+’&field3=’str(h)
f=ur.urlopen(st)
time.sleep(2)

----------------------------------------------------------------------------------------
OLED – Organic Light Emitting Diode

import time
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import Image Font

RST=24
disp=Adafruit_SSD1306.SSD1306_128_64(rst=RST,i2c_address=0x3c)
disp.begin()
disp.clear()
disp.display()
width=disp.width
height=disp.height
image=Image.new('1' ,(width,height))
draw=ImageDraw.Draw(image)
draw.rectangle((0,0,width, height),outline=0,fill=0)
padding=2
top=padding
x=padding
font=ImageFont.load_default()
draw.text((x,top),’HELLO...’,font=font,fill=255)
draw.text((x,top+20),’Vasavi!’,font=font,fill=255)
disp.image(image)
disp.display()
time.sleep(5)

EXECUTE this infront on Bluetooth in command prompt

1.sudo bluetoothctl
agent on
default-agent
scan on
scan off
devices
pair CB:AA: 0B: BF :08: CD connect CB: AA:0B:BF:08: CD
trust CB:AA:0B: BF: 08: CD

2.On your Raspberry Pi:
1. Type bluetoothctl and press Enter to open
Bluetooth control
2. At the [bluetooth]#
prompt enter the
following commands:
discoverable on
pairable on
agent on
default-agent
scan on
3. Wait for a message to appear showing the Android phone has been found:

[NEW] Device 12:23:34:45:56:67 devicename
4. Type pair with the mac address of your Android phone:

pair 12:23:34:45:56:67
-------------------------------------------------------------------------------------- Bluetooth Program using normal server and client.

Server Code

import bluetooth
import time
server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port=1
server_sock.bind((“ “,port))
server_sock.listen(1)
client_sock,address=server_sock.accept()
print(“accepted connection from”,address)
while True:
data=client_sock.recv(1024)
print(“received from client”+data)
server_sock.close()

Client code
import bluetooth import time
bd_addr=”B8:27:EB:5A:15:EF”
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while True:
text=raw_input(“enter your message”)
sock.send(text)
time.sleep(1)
sock.close()
---------------------------------------------------------------------------------------
Program to turn on LED and Buzzer remotely Server Code

import bluetooth
import time
import sys
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python.Codelegacy/ Adafruit_MCP230xx’)
from Adafruit_MCP230xx import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1,address=0x20,num_gpios=16)
mcp.config(0,mcp.OUTPUT)
mcp.config(11,mcp.OUTPUT)
server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port=1
server_sock.bind((==,port))
server_sock.listen(1)
client_sock,address=server_sock.accept()
print(“accepted connection from”,address)
while True:
data=client_sock.recv(1024)
print(“received from client”+data)
if(data==”on”):
mcp.output(0,1)
mcp.output(11,1)
time.sleep(1)
else:
mcp.output(0,0)
mcp.output(11,0)
time.sleep(1)
server_sock.close()

Client code:
import bluetooth import time
bd_addr=”B8:27:EB:5A:15:EF”
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while True:
text=raw_input(“enter your message”)
sock.send(text)
time.sleep(1)
sock.close()

------------------------------------------------------------------------------------------ Program to turn on LED2 in Blue color and RElay-1(ON)

Server Code

import bluetooth
import time
import RPi.GPIO as GPIO
import sys
sys.path.append(‘/home/pi/Adafruit-Raspberry-Pi-Python.Codelegacy/ Adafruit_MCP230xx’)
from Adafruit_MCP230xx import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1,address=0x20,num_gpios=16)
mcp.config(3,mcp.OUTPUT)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36,GPIO.OUT)
server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port=1
server_sock.bind((“”,port))
server_sock.listen(1)
client_sock,address=server_sock.accept()
print(“accepted connection from”,address)
while True:
data=client_sock.recv(1024)
print(“received from client”+data)
if(data==”on”):
mcp.output(3,1)
GPIO.output(36,1)
time.sleep(1)
else:
mcp.output(3,0)
GPIO.output(36,0)
time.sleep(1)
server_sock.close()

Client Code

import bluetooth
import time
bd_addr=”B8:27:EB:5A:15:EF”
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while True:
text=raw_input(“enter your message”)
sock.send(text)
time.sleep(1)
sock.close()
---------------------------------------------------------------------------------------
ZIGBEE end device.

Sourcecode:
import time
import serial
ser=serial.Serial(port=’/dev/ttyUSB0’,baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS,timeout=1)
while True:
x=raw_input(‘enter text to send to coordinator’)
ser.write(x)
time.sleep(1)

ZIGBEE router device.
Sourcecode:

import time
import serial
ser=serial.Serial(port=’/dev/ttyUSB0’,baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
while True:
x=’blank’
x=ser.readline()
print ‘message from EN: %s’ %X
ser.write(‘message from R: %s’ %X)
time.sleep(1)

ZIGBEE Coordinator device.
Sourcecode:
import time
import serial
ser=serial.Serial(port=’/dev/ttyUSB0’,baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
while 1:
msg=s.readline()
print(msg)

-----------------------------------------------------------------------------------------
Installation on command prompt

sudo apt install -y mosquitto
sudo apt install -y mosquitto-clients

To check status
sudo systemctl status mosquitto.service

MQTT Subscriber

import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print(“Connected with MQTT Server “+str(rc))
client.subscribe(“iot/home”)
def on_message(client, userdata, msg):
print(msg.topic+“ ”+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(“test.mosquitto.org”, 1883,60)
client.loop_forever()

MQTT Publisher

import paho.mqtt.client as mqtt
publish.single(“iot/home”, “temperature”, hostname=”test.mosquitto.org”)
print(“Done")

-------------------------------------------------------------------------------------------

Write a Program to upload the weather information on to the open-source cloud “ThingSpeak”.

Sourcecode:
import sys
import time
sys.path.append(‘/home/pi/ETS_IoT KIT demo/DemoCode/BME280’)
import BME280lib as bme
import urllib.request as ur
while True:
t,p,h=bme.readBME280All( )
print(‘Temperature :’,t)
print(‘Pressure:’,p)
print(‘Humidity :’,h)
st=’https:// api.thingspeak.com/update?api_key=FBMA7CJW92DJJ1S&field1=’+ str(t)+
’&field2=’+str(p)+’&field3=’+str(h)
f=ur.urlopen(st)
time.sleep(2)

-------------------------------------------------------------------------------------------


be
//led code//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import sys
import time
sys.path.append("/home/pi/1602-21-733/Adafruit_MCP230xx")
from Adafruit_MCP230xx import Adafruit_MCP230XX
mcp=Adafruit_MCP230XX(busnum=1, address=0x20,num_gpios=16)
mcp.config(0,mcp.OUTPUT)
mcp.config(1,mcp.OUTPUT)
mcp.config(2,mcp.OUTPUT)
mcp.config(3,mcp.OUTPUT)
mcp.config(4,mcp.OUTPUT)
mcp.config(5,mcp.OUTPUT)
mcp.config(6,mcp.OUTPUT)
mcp.config(7,mcp.OUTPUT)
mcp.config(8,mcp.OUTPUT)
mcp.config(9,mcp.INPUT)
mcp.config(10,mcp.INPUT)

while True:
mcp.output(2,0)
if mcp.input(9) == 512 :
mcp.output(2,1)
if mcp.input(10) == 0 :
mcp.output(2,0)
mcp.output(3,1)
time.sleep(5)


//Ultrasonic sensor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import sys
import time
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO_TRIGGER = 16
GPIO_ECHO = 18
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO.ECHO, GPIO.IN)
GPIO.output(GPIO_TRIGGER, False)
time.sleep(1)

while(True):
GPIO.output(GPIO_TRIGGER, True)
time.sleep(1)
GPIO.output(GPIO_TRIGGER, False)
while(GPIO.input(GPIO_ECHO) == 0):
start = time.time()
while(GPIO.input(GPIO_ECHO) == 1):
stop = time.time()
distance = ((stop-start)*34300)/2
print("distance :%.2f" % distance)

#Pins Connection
Vcc=5V Output
Trig=RPI16 //This is gpresent on kit left side on board
ECHO=RPI18
Gnd=GND



//PIR//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import sys
sys.path.append('/home/pi/Adafruit-Raspberry-Pi-Python-Code-legacy/Adafruit_MCP230xx')
from Adafruit_MCP230xx import Adafruit_MCP230XX
import time
mcp=Adafruit_MCP230XX(busnum=1,address=0x21,num_gpios=16)
mcp.config(0,mcp.INPUT)
mcp.pullup(0,1)
while True:
i=mcp.input(0)
time.sleep(1)
if i==1:
print "person detect"
time.sleep(1)
if i==0:
print("person not detect")
time.sleep(1)

#Pins Connection
1pin-GND
2pin-GPA0//left side of board
3pin-VCC 5v


//soil moisture ////////////////////////////////////////////////////////////////////////////////////////////////////

import time

import Adafruit_GPIO.SPI as SPI

import Adafruit_ MCP3008

SPI_PORT=0

SPI _DEVICE=0

mcp=Adafruit_MCP3008.MCP3008(spi = SPI. SpiDev(SPI_ PORT,SPI_DEVICE))

while True:

t=mcp. read_adc(0)

p= (100- (t/1023. 0) *100)

print( ‘moisture content is', p)

time. sleep (3)

#Pins connections
VCC-VCC 5v
GND-GND
A0-A0




#servo motor////////////////////////////////////////////////////////////////////////////////////////////////////

import RPi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT)

pwm=GPIO.PWM(22,100)
pwm.start(5)

angle1=10
duty1= float(angle1)/10 + 2.5
angle2=160
duty2= float(angle2)/10 + 2.5
ck=0
while ck<=5:
pwm.ChangeDutyCycle(duty1)
time.sleep(0.8)
pwm.ChangeDutyCycle(duty2)
time.sleep(0.8)
ck=ck+1
time.sleep(1)
GPIO.cleanup()

#Pins connection
ASH color- GND
RED-VCC
Yellow-RPI 22



//Thingspeak the data will be uploaded to cloud////////////////////////////////////////////////////////////////////////////////////////////////////
c
#To see the output
Go to thingspeak go to apikeys then copy the link of read feed channel and open it



//Bluetooth////////////////////////////////////////////////////////////////////////////////////////////////////
Server program(Server.py)
import time
import sys
import bluetooth
server_sock=bluetooth.BluetoothSocket(bluetooth.RFC)
port=1
server_sock.bind(("",port))
server_sock.listen(1)
client_sock, address=server_sock.accept()
print("Accepted connection from ",address)
while(True):
data=client_sock.recv(1024)
print("received [%s]"% data)
text=raw_input("ENTER YOUR MESSAGE:")
client sock.send(text)
client sock.close()
server_sock.close()


Client program(Client.py)
import time
import bluetooth
bd_addr="88:27:EB:5A:15:EF"// this you will get from command prompt when you type all those commands
port=1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while True:
text=raw_input("ENTER YOUR MESSAGE:")
sock.send(text)
data=sock.recv(1024)
print("received[%s] %data)
time.sleep(1)
sock.close()

Type this in command pompt
sudo apt-get install bluetooth
sudo apt-get install bluez
sudo apt-get install python-bluez
sudo bluetoothctl
power on
agent on
default-agent
discoverable on
scan on

#first run server.py
#then client.py

Mqtt /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
mqtt.py
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with MQTT Server" + str(rc))
client.subscribe(iot/vce")

def on_message(client, userdata, msg):
print(msg.topic+str(msg.payload))

client=mqtt.Client()
client.on_connect = on_connect
client.on_message=on_message
client.connect("test mosquito.org", 1863, 60)
client.loop_forever()


subs.py
import paho.mqtt.publish as publish
import BME280lib as b
t,p,h=b.readBME280All()
publish.single('iot/vce","temperature %s"%t, hostname="test.mosquito.org")
print("Done")

#first run subs.py
#then run mqtt.py
///////////////////////////////////////////////////////////////////////////////////////
import RPi.GPIO as GPIO
import time

# Set GPIO mode
GPIO.setmode(GPIO.BCM)

# Define GPIO pins for LED, servo motor, and buzzer
LED_PIN = 18
SERVO_PIN = 17
BUZZER_PIN = 22

GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(SERVO_PIN, GPIO.OUT)
GPIO.setup(BUZZER_PIN, GPIO.OUT)

def read_moisture():
import random
return random.randint(0, 100)

def turn_on_led():
GPIO.output(LED_PIN, GPIO.HIGH)

def turn_off_led():
GPIO.output(LED_PIN, GPIO.LOW)

def turn_servo():
pwm = GPIO.PWM(SERVO_PIN, 50)
pwm.start(0)
duty = 90 / 18 + 2
GPIO.output(SERVO_PIN, True)
pwm.ChangeDutyCycle(duty)
time.sleep(1)
GPIO.output(SERVO_PIN, False)
pwm.ChangeDutyCycle(0)
pwm.stop()

def beep_buzzer():
GPIO.output(BUZZER_PIN, GPIO.HIGH)
time.sleep(3)
GPIO.output(BUZZER_PIN, GPIO.LOW)

try:
while True:
moisture_level = read_moisture()
print("Moisture level:", moisture_level)

if moisture_level < 20:
turn_servo()
beep_buzzer()
elif moisture_level < 50:
turn_on_led()
else:
turn_off_led()

time.sleep(5) # Adjust the interval as needed

except KeyboardInterrupt:
GPIO.cleanup()





node red installation/////////////////////////////////

npm install -g --unsafe-perm node-red
node-red go to the server running

there will inject node you need to select the topic set the topic and connect to mqttout
in mqtt set the server as 127.0.0.1 and retain as true and assign qos as 0 and change the name to publisher

next take mqtt in(subscriber) and debug component and connect both of them and you need to
enter the data in the debug and write the topic which was used earlier while setting inject node

and mqtt in topic name which was given previous and in debug component







zigbee ///////

#FOR coordinator node

import time
import serial
s=serial.Serial(port='/dev/ttyUSB0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1)
while(1):
x=s.readline().strip()
print(x)

#For END DEVICE node :

import time
import serial
ser=serial.Serial(port='/dev/ttyUSB0;, baudrate=9600,parity=serial.PARITY_NONE, stopbits =serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS, timeout=1)
c=0
while True:
x=raw input('Enter text to send to coordinator:')
ser.write(x)
time.sleep(1)


#For ROUTER node:

import time
import serial
ser=serial.Serial(port='/dev/ttyUSB0;, baudrate=9600,parity=serial. PARITY_NONE, stopbits =serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
c=0
while True:
x=raw_input('Enter text to send to coordinator:')
ser.write(x)








//////////////////////////////////////// Zigbee config /////////////////////////////////////////////////////


https://drive.google.com/file/d/1rb40WD0BkLNn-GlIblOHmnXokuG3SX2X/view?usp=sharing







//////8th ANS

import time
import serial
import sys
import BME280lib as BME # Ensure BME280lib is in the correct path or installed

# Initialize the serial connection for Zigbee communication
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1)

def read_bme280():
temperature, pressure, humidity = BME.readBME280A11()
return temperature, pressure, humidity

def send_data_to_coordinator(temperature, pressure):
message = f"Temp: {temperature:.2f} C, Pressure: {pressure:.2f} hPa"
ser.write(message.encode())
print(f"Sent to coordinator: {message}")

def main():
while True:
try:
temperature, pressure, humidity = read_bme280()
print(f"Temperature: {temperature:.2f} C")
print(f"Pressure: {pressure:.2f} hPa")
print(f"Humidity: {humidity:.2f} %")

send_data_to_coordinator(temperature, pressure)
time.sleep(3) # Adjust the sleep time as needed

except Exception as e:
print(f"Error: {e}")
time.sleep(3)

if __name__ == "__main__":
main()





import time
import serial

# Initialize the serial connection for Zigbee communication
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1)

def main():
while True:
try:
message = ser.readline().decode().strip()
if message:
print(f"Received from end device: {message}")

except Exception as e:
print(f"Error: {e}")
time.sleep(1)

if __name__ == "__main__":
main()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3rd
#include <SoftwareSerial.h>

const int trigPin = 9;
const int echoPin = 10;
const float speedOfSound = 34300; // in cm/s
SoftwareSerial xbeeSerial(0, 1); // RX, TX

void setup() {
Serial.begin(9600);
xbeeSerial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(1000);
}

void loop() {
long duration = measureDistance();
float distance = calculateDistance(duration);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
sendData(distance);
delay(1000);
}

long measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration;
}

float calculateDistance(long duration) {
float distance = (duration * speedOfSound) / 2 / 10000; // cm
return distance;
}

void sendData(float distance) {
String distanceStr = String(distance);
xbeeSerial.print(distanceStr);
xbeeSerial.print("n");
}







     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.