-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Controls over MQTT Protocol and Added Required Micropython File…
…s for ESP32
- Loading branch information
1 parent
068d602
commit 260341b
Showing
21 changed files
with
809 additions
and
519 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import network | ||
import ure | ||
import time | ||
from machine import Pin, PWM | ||
import ubinascii | ||
import machine | ||
import micropython | ||
import umqtt_simple | ||
from umqtt.simple import MQTTClient | ||
|
||
#connecting to hotspot | ||
ssid = "whotspot" | ||
ssid_password = "manumanu" | ||
wlan = network.WLAN(network.STA_IF) | ||
wlan.active(True) | ||
wlan.connect(ssid,ssid_password) | ||
|
||
# Direction Pin of Motor1 assuming value=1 as forward | ||
m1_dir = Pin(2, Pin.OUT, value=1) | ||
|
||
# MQTT Broker IP | ||
server="192.168.137.160" | ||
|
||
# Received messages from subscriptions will be delivered to this callback | ||
def sub_cb(topic, msg): | ||
#print((topic, msg)) | ||
msgDecoded=msg.decode("utf-8") #Converts Byte Data into UTF-8 Strings | ||
#Using Micropython RegEXP lib to seperate data | ||
#where data[0] denotes Direction and data[1] denotes Speed | ||
sep = ure.compile("-") # Delimiter is "-" | ||
data = sep.split(msgDecoded) | ||
print("Direction:",data[0],"\t Speed:",data[1],"\t State:",data[2]) | ||
if data[0] == "w": | ||
#m1_dir.value(1) | ||
motor(1,int(data[1]),int(data[2])) | ||
elif data[0] == "s": | ||
#m1_dir.value(0) | ||
motor(0,int(data[1]),int(data[2])) | ||
else: | ||
#m1_dir.value(0) | ||
motor(0,0) #Frequency Becomes zero so motor stops | ||
|
||
|
||
def pircontrol(server="192.168.137.160"): | ||
c = MQTTClient("esp32_pir_mqttClient", server) | ||
c.set_callback(sub_cb) | ||
c.connect() | ||
c.subscribe(b"esp32/control") | ||
while True: | ||
if True: | ||
# Blocking wait for message | ||
c.wait_msg() | ||
else: | ||
# Non-blocking wait for message | ||
x=c.check_msg() | ||
|
||
# Then need to sleep to avoid 100% CPU usage (in a real | ||
# app other useful actions would be performed instead) | ||
time.sleep(1) | ||
c.disconnect() | ||
|
||
def motor(dir,speed,state,duty_cycle=750): | ||
stepper1 = PWM(Pin(2), freq=speed, duty=duty_cycle) | ||
if state==0: | ||
stepper1.deinit() | ||
else: | ||
stepper1 = PWM(Pin(2), freq=speed, duty=duty_cycle) | ||
direction1 = Pin(4, Pin.OUT, value=dir) | ||
# if speed==0: | ||
# stepper1.deinit() | ||
# direction2 = Pin(4, Pin.OUT, value=dir) | ||
# direction3 = Pin(4, Pin.OUT, value=dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
pircontrol() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def connect(ssid=None,ssid_password=None): | ||
import network | ||
if ssid is None: | ||
ssid = "whotspot" | ||
print('Error!!: Invalid Credentials Provided') | ||
print('Using Default SSID') | ||
if ssid_password is None: | ||
ssid_password = "manumanu" | ||
print('Using Default WIFI Authentication Password...') | ||
wlan = network.WLAN(network.STA_IF) | ||
wlan.active(True) | ||
if not wlan.isconnected(): | ||
print('Connecting to network...') | ||
wlan.connect(ssid,ssid_password) | ||
while not wlan.isconnected(): | ||
pass | ||
print("\n****Connection Successful****") | ||
print('Network config:', wlan.ifconfig()) | ||
|
||
def disconnect(): | ||
import network | ||
print('Disconnecting...') | ||
wlan = network.WLAN(network.STA_IF) | ||
if not wlan.isconnected(): | ||
print('Warning: Already Disconnected!') | ||
else: | ||
wlan.disconnect() | ||
print('\n****Disconnected Successfully****') | ||
|
||
#def update(pkg=None): | ||
# import network, upip | ||
# if not network.WLAN(network.STA_IF).isconnected(): | ||
# print('Error!!: Connect to Internet') | ||
# else: | ||
# if pkg is None: | ||
# pkg=["micropython-uasyncio","micropython-pkg_resources","picoweb","utemplate","micropython-ulogging"] | ||
# print("Warning!:No Library name provided.") | ||
# i=0 | ||
# while i<len(pkg): | ||
# print("\n"+"Updating Default Library: "+pkg[i]) | ||
# upip.install(pkg[i]) | ||
# i+=1 | ||
# print("\n****Library update successful****") | ||
# else: | ||
# print("Updating Library: "+pkg) | ||
# upip.install(pkg) | ||
|
||
|
||
|
||
|
2 changes: 1 addition & 1 deletion
2
platforms/android/app/build/intermediates/incremental/mergeDebugAssets/merger.xml
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file modified
BIN
+13.5 KB
(100%)
platforms/android/app/build/outputs/apk/debug/app-debug.apk
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
platforms/android/app/src/main/assets/www/img/broken-link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.92 KB
platforms/android/app/src/main/assets/www/img/solid-green-blink-light.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.