# Function:
# Solar soil moisture sensor with 18650 LI battery with charger and power supply 3.3V
# For power save, the microcontroller shall enter the deep sleep and switch of the power supplier of the soil moisture sensor via transistor or (GPIO Output Pin=Pull-UP)
# Subfunction: Battery voltage sensor for battery state monitoring
# Connect to the ioBroker via MQTT and send values: soil moisture sensor and Battery voltage as raw value. Further function via node-red
# Function Sequence:
# 1.) Load required modules for the simple work
# 2.) Configure required HW pin, LED, Transistor etc.
# 3.) Connect WLAN
# 4.) Connect MQTT
# 5.) LED blink for visual indication that the system is alive
# 6.) Deep sleep for 1 hour (or 2 hour -> soil moisture don't change the value in a fast manner)
# Reqeat the sequence until we all getting old :-)
#
# Further Information
# Deep Sleep is working with full reset
# Several example don't work
# Don't work:
# https://wokwi.com/projects/389514828405174273
# Scheduler and deep sleep combination are not working
# https://wokwi.com/projects/389622799443898369
# rtc.irq is not supported
# Further references for this code
# https://docs.micropython.org/en/latest/esp32/quickref.html#deep-sleep-mode
# *****************************************************************************************
# ******************** Import libraries ***************************************************
# *****************************************************************************************
import machine
from machine import Pin
from time import sleep
import esp32
import network
# Start Program code
print("")
print("Start program....")
# *****************************************************************************************
# ******************** HW Pin configuration ***********************************************
# *****************************************************************************************
led = Pin (2, Pin.OUT) #GPIO2 as output for LED
opin = Pin(19, Pin.OUT, value=1, hold=True) # hold output level
ipin = Pin(21, Pin.IN, Pin.PULL_UP, hold=True) # hold pull-up
print("HW Pin Configuration: Done")
# enable pad hold in deep-sleep for non-RTC GPIO
esp32.gpio_deep_sleep_hold(True)
# *****************************************************************************************
# ******************** WLAN ***************************************************************
# *****************************************************************************************
# Connection Info
SSID="Wokwi-GUEST"
WIFIPASS=""
print("Connect to Network", end="") # end="" --> Keine neue Zeile anfangen
wlan = network.WLAN(network.STA_IF)
wlan.active(True) # WLAN-Adapter switch on
wlan.connect(SSID, WIFIPASS) # Connect with network
while not wlan.isconnected():
print(".", end="")
sleep(0.1)
print(" WLAN connected !")
# *****************************************************************************************
# ******************** MQTT ***************************************************************
# *****************************************************************************************
from umqtt.simple import MQTTClient
MQTT_CLIENT_ID = "richards-publisher"
MQTT_BROKER = "mqtt.chaos4all.de"
MQTT_USER = "student"
MQTT_PW = "Pa55w.rd"
MQTT_TOPIC = "lf07/richard"
# mqtt-Verbindungsparameter
print("Verbinde mit MQTT-Broker ............ ", end="")
mqtt = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PW)
# Verbindung herstellen
mqtt.connect()
print(" MQTT Verbunden")
# *****************************************************************************************
# ******************** Hello world blink **************************************************
# *****************************************************************************************
print("LED Blink: Start")
led.value(1) #LED is ON
sleep(1) #delay of 1 second
led.value(0) #LED is OFF
sleep(1) #delay of 1 second
print("LED Blink: Done")
# wait 5 seconds so that you can catch the ESP awake to establish a serial communication later
# you should remove this sleep line in your final script
sleep(5) #delay of 10 seconds
# *****************************************************************************************
# ******************** Deep Sleep *********************************************************
# *****************************************************************************************
print("Going into Deep Sleep Mode")
machine.deepsleep(10000) # 10000ms sleep time
# *****************************************************************************************