import machine
import utime
import network
#import radio
#=====================THIS NEED TO BE PULL OUT FOR A SEPERATE INCLUDE FILE=========================
#conditonal variable
intr_check = 0;
UNIX_TIME = 0;
UNIX_TIME_MS = 0;
red_pin = 12
blue_pin = 14
green_pin = 13
buzz_pin = 4
intr_pin = 2
CS = 5
CE_PIN = 32
CSN_PIN = 33
ssid = ""
pass1 = ""
radio_on = 0
j = 0 #for wifi j == 6 --> WIFI Connected
# j == 3 --> WIFI NOT Connected
ERR_CODE = 0 # 0 = ALL OKEY
# 1 = SENSOR DATA ERROR
# 2 = SENSOR TIME OUT
# 3 = SD CARD FAILED
# 4 = RTC ERROR
SF_time_now = 0 #Switch Functiom
ST_TO_MEGA_time_now = 0 #Status Send
ST_TO_CLOUD_time_now = 0 #Status to cloud
DIS_MILLIS_time_now = 0 #Display
SEN_DATA_time_now = 0 #Sensor Time Out
BUZZ_previousMillis = 0 #will store last time LED was updated
WIFI_CONNECT_time_now = 0 #wifi connected time
#status
ST_S1 = 0 #Irr_1
ST_S2 = 0 #Irr_2
ST_S3 = 0 #Irr_3
ST_S4 = 0 #Irr_4
ST_S5 = 0 #ACF
ST_S6 = 0 #Fogger
ST_S7 = 0 #Ex_Fan
ST_S8 = 0 #Pad
ST_S9 = 0 #Vent
ST_S10 = 0 #Curtain
#----------------Intrupt - 1 - To Configuration Mode---------------------------
def CONFIG_INTR():
intr_check = 1
def DATE_TIME():
#TODO: Print Date & Time
#ref:https://stackoverflow.com/questions/57154794/micropython-and-epoch
#populate the global var
UNIX_TIME = utime.time() - 19800 #converting to IST
UNIX_TIME_MS = UNIX_TIME * 1000;
#TODO: need to implement this
def ASSIGN_CRED():
raise NotImplementedError("ASSIGN_CRED() not implemented")
#TODO: need to implement this
def ASSIGN_VAR():
raise NotImplementedError("ASSIGN_VAR() not implemented")
#TODO: need to implement this
def RECONNECT():
#ref:https://docs.micropython.org/en/latest/library/network.WLAN.html
if(network.WLAN.status() == STAT_IDLE):
WIFI_CONNECT_time_now = utime.ticks_ms()
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect(ssid, pass1)
#TODO: need to implement this
def CONFIG_MODE():
raise NotImplementedError("CONFIG_MODE() not implemented")
#TODO: need to implement this
def FW_VERSION_CHECK():
raise NotImplementedError("FW_VERSION_CHECK() not implemented")
#TODO: need to implement this
def DEVICE_DETAILS_TO_CLOUD():
raise NotImplementedError("DEVICE_DETAILS_TO_CLOUD() not implemented")
#TODO: need to implement this
def ENSENSE_TO_ESP():
raise NotImplementedError("ENSENSE_TO_ESP() not implemented")
#TODO: need to implement this
def ENSENSE_TO_CLOUD():
raise NotImplementedError("ENSENSE_TO_CLOUD() not implemented")
#TODO: need to implement this
def STATUS_TO_CLOUD():
raise NotImplementedError("STATUS_TO_CLOUD() not implemented")
#==================================================================================================
red_pin = 12
blue_pin = 14
green_pin = 13
buzz_pin = 4
#==================================================================================================
print("Hello, ESP32! - 1")
#--------1. Basic Initilisation --------------------
#ref:https://www.engineersgarage.com/micropython-esp8266-esp32-uart/
Serial = machine.UART(0, baudrate=9600)
Serial2 = machine.UART(0, baudrate=9600, tx=16, rx=17, bits=8, parity=None, stop=1)
#EEPROM in micropython can be implement uisng simple file, and there we can use read() & write()
#--------2. Radio Initialisation --------------------
#ref:https://microbit-micropython.readthedocs.io/en/v1.0.1/radio.html
#radio.on()
#radio.config(data_rate=radio.RATE_1MBIT)
#radio_on = 1
#--------3. RGB Initilisation --------------------
#ref:https://docs.micropython.org/en/latest/library/machine.Pin.html
RED_PIN = machine.Pin(red_pin, machine.Pin.OUT)
GREEN_PIN = machine.Pin(green_pin, machine.Pin.OUT)
BLUE_PIN = machine.Pin(blue_pin, machine.Pin.OUT)
BUZZ_PIN = machine.Pin(buzz_pin, machine.Pin.OUT)
#------ 4.Defining the interrupt-------------------
#IRQ setted for Rising edge can be ORed for falling edge also if required
#ref:https://docs.micropython.org/en/v1.8/esp8266/esp8266/tutorial/pins.html
INTR_PIN = machine.Pin(intr_pin, machine.Pin.IN)
INTR_PIN.irq(trigger=machine.Pin.IRQ_RISING, handler=CONFIG_INTR)
#----- 5. Interrupt Calling for Config Mode------------
if intr_check == 1:
intr_check = 0
utime.sleep_ms(3000)
#interupt raised, we will do config
if intr_pin.value() == 1:
CONFIG_MODE()
ASSIGN_CRED()
ASSIGN_VAR()
Serial.write('_ALL SET UP -> [DONE]_')
RECONNECT()
# for OTA
if ((j == 6) and FW_VERSION_CHECK()):
Serial.write('Executing Over the air Updates')
elif j != 6:
Serial.write('Could not Get Wi-Fi -> Skipped OTA Update')
#ref:https://docs.micropython.org/en/v1.15/library/utime.html
SF_time_now = utime.ticks_ms() #Switch Functiom
ST_TO_MEGA_time_now = utime.ticks_ms() #Status Send
ST_TO_CLOUD_time_now = utime.ticks_ms() #Status to cloud
DIS_MILLIS_time_now = utime.ticks_ms() #Display
SEN_DATA_time_now = utime.ticks_ms() #Sensor Time Out
BUZZ_previousMillis = utime.ticks_ms() #will store last time LED was updated
Serial.write(" ")
Serial.write("IoT Hub by City Greens Starting")
Serial.write("Hardware Version : ")
Serial.write(HardwareVer)
Serial.write(" || ")
Serial.write("Firmware Version : ")
Serial.write(FirmwareVer)
DATE_TIME()
DEVICE_DETAILS_TO_CLOUD() #TODO: need to check how we can send telemetry
# data to the ThingsBoard in MicroPython
#TODO: Need to check how we can keep WiFi alive
if (utime.ticks_ms() > (SF_time_now + 2000)):
DATE_TIME()
SWITCHING_LOOP()
AUTO_HVAC()
AUTO_VENT()
SW_TO_MEGA()
SWST_TO_CLOUD()
SF_time_now = utime.ticks_ms()
if radio_on: # Mircopython ensure if Radio is turned ON then it will keep alive, using some flag to check the same
ENSENSE_TO_ESP()
ERR_CODE = 0
SEN_DATA_time_now = utime.ticks_ms()
ENSENSE_TO_CLOUD()
if intr_check == 1:
intr_check = 0
utime.sleep_ms(3000)
if intr_pin.value() == 1:
CONFIG_MODE()
if (utime.ticks_ms() - (SEN_DATA_time_now > SEN_DATA_time_out)):
ERR_CODE = 2;
Serial.write("Sensor Data Time Out -> Turing off HVAC | FOG | VENT | CUR ")
ST_S6 = 0;
ST_S7 = 0;
ST_S8 = 0;
ST_S9 = 0;
ST_S10 = 0;
utime.sleep_ms(3000)
SW_TO_MEGA()
utime.sleep_ms(3000)
#------------------- Status to Cloud ---------------------------------
if (time.ticks_ms() > (ST_TO_CLOUD_time_now + 3000)):
STATUS_TO_CLOUD()
ST_TO_CLOUD_time_now = time.ticks_ms()