<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>deep sleep Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/deep-sleep/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/deep-sleep/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Thu, 09 Oct 2025 14:50:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Mailbox IoT ESP 32 Project</title>
		<link>https://creatronix.de/firebeetle-project/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 02 Apr 2020 14:52:49 +0000</pubDate>
				<category><![CDATA[Electronics & IOT]]></category>
		<category><![CDATA[deep sleep]]></category>
		<category><![CDATA[esp32]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[micropython]]></category>
		<category><![CDATA[wifi]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3147</guid>

					<description><![CDATA[<p>Motivation Out of curiosity I bought a ESP32 board to tinker with, but for almost half a year hadn&#8217;t got an idea what to do with it&#8230; But then! Project Idea The idea: Every time I get mail into my physical snail mail mailbox, the mailbox shall send a message via Telegram to my smartphone:&#8230;</p>
<p>The post <a href="https://creatronix.de/firebeetle-project/">Mailbox IoT ESP 32 Project</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Motivation</h2>
<p>Out of curiosity I bought a ESP32 board to tinker with, but for almost half a year hadn&#8217;t got an idea what to do with it&#8230; But then!</p>
<h2>Project Idea</h2>
<p><img fetchpriority="high" decoding="async" class="alignnone size-large wp-image-3243" src="https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-1024x922.jpg" alt="" width="525" height="473" srcset="https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-1024x922.jpg 1024w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-300x270.jpg 300w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-768x692.jpg 768w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-1536x1383.jpg 1536w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8859-2048x1844.jpg 2048w" sizes="(max-width: 525px) 100vw, 525px" /></p>
<p>The idea: Every time I get mail into my physical snail mail mailbox, the mailbox shall send a message via Telegram to my smartphone:</p>
<p><img decoding="async" class="alignnone size-full wp-image-3241" src="https://creatronix.de/wp-content/uploads/2020/04/telegram_mailbox_bot_esp32.png" alt="" width="600" height="311" srcset="https://creatronix.de/wp-content/uploads/2020/04/telegram_mailbox_bot_esp32.png 600w, https://creatronix.de/wp-content/uploads/2020/04/telegram_mailbox_bot_esp32-300x156.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></p>
<p>But with many ideas the execution can become time consuming. Well, let&#8217;s see 🙂</p>
<h2>MicroPython</h2>
<p>As a Python afficionado I was relieved as I found out, that you can use MicroPython on the ESP32 board. I wrote a separate article as a prerequisite:</p>
<p><a href="https://creatronix.de/using-micropython-on-firebeetle-esp32/">Using MicroPython on FireBeetle ESP32</a></p>
<p>From now on I assume, that You have a MicroPython version running on the ESP32.</p>
<h2>Buttons</h2>
<p>When the lid of the mailbox is opened, a button shall trigger the action, so we need to configure a GPIO as an input.</p>
<p>This is done via the machine package.</p>
<p>When we don&#8217;t want to have external pullups we can enable the internal pullup resistor. Beware: the logic becomes inverted, because the pin is high in idle we pull it down with the button to 0 volts, hence button pressed equals value zero</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import machine 
button = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) 
while True: 
    if not button.value(): 
        print('Button pressed!')</code></pre>
</div>
<h2>Deep Sleep</h2>
<p>Due to the fact that the device will be battery powered and will just need to send a message once or twice a day, we will need the deepsleep capability.</p>
<p>The machine package gives you access to the deepsleep function as well.</p>
<p>A bit counterintuitive but you also need the esp32 package for defining a wake up cause. One caveat: not every GPIO is able to wake up the ESP32 from deepsleep but GPIO 4 will do it.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import machine 
import esp32 
from time import sleep 

wake1 = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) 
esp32.wake_on_ext0(pin=wake1, level=esp32.WAKEUP_ALL_LOW) 
print('Im awake. Going to sleep in 10 seconds') 
sleep(10) 
print('Going to sleep now') 

machine.deepsleep()</code></pre>
</div>
<h2>Setting up Wifi in MicroPython</h2>
<p>To be able to send messages we need a WiFi connection. In MicroPython this is handled by the network package.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python">import network
</pre>
</div>
<p>The ESP32 can either work as a station or as an access point. We need the first option so we use the STA_IF configuration.<br />
sta_if = network.WLAN(network.STA_IF)<br />
the interface has to be activated via</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>sta_if.active(True)</code></pre>
</div>
<p>Now we can connect to our Wi-Fi</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>sta_if.connect(ssid,  password)</code></pre>
</div>
<p>You can check the status of the connection via:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>sta_if.isconnected()</code></pre>
</div>
<h2>Writing a Telegram Bot</h2>
<h3>Create TG bot</h3>
<ol>
<li>Open Telegram app on your phone</li>
<li>Search for @botfather</li>
<li>Start chat</li>
<li>Send /newbot</li>
<li>Pick a name</li>
<li>Pick a username (must be unique)</li>
<li>Receive credentials</li>
</ol>
<h3>Chat with your bot</h3>
<p>Now you can search for your bot&#8217;s name and send a message</p>
<h3>Python test Program</h3>
<p>Telegram has a nice python package which you can pip install</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>pip install python-telegram-bot --upgrade</code></pre>
</div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import telegram 

bot = telegram.Bot(token='&lt;your_token&gt;') 

if __name__ == '__main__': 
    print(bot.get_me()) 
    bot.send_message(chat_id="&lt;your_chat_id&gt;", text="Post ist da")</code></pre>
</div>
<p>This program is just used to test the connectivity of your bot.</p>
<h3>urequests</h3>
<p>Because we don&#8217;t have access to the telegram package inside the ESP / MicroPython environment we use <a href="https://pypi.org/project/urequests/">urequests</a>.</p>
<p>urequests is similar to the requests package and lets you send http messages.</p>
<p>Sending messages looks like the following</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import urequests 

requests.get("https://api.telegram.org/bot&lt;your_token&gt;/sendMessage?text=hallo&amp;chat_id=&lt;your_chat_id&gt;")</code></pre>
</div>
<h2>The complete Code</h2>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python">import network
import urequests
import machine
import esp32
from time import sleep

SSID = &lt;your_ssid&gt;
WIFI_PW = &lt;your_wifi_pw&gt;
TOKEN = &lt;your_tg_token&gt;
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
CHAT_ID = &lt;your_chat_id&gt;


def connect_to_wifi(ssid, password):
    print("MBB: connect_to_wifi")
    sta_if = network.WLAN(network.STA_IF)

    if sta_if.isconnected():
        print("MBB: already connected")
        return

    if not sta_if.active():
        print("MBB: activate network adapter")
        sta_if.active(True)
        print("MBB: network adapter activated")

    if not sta_if.isconnected():
        print("MBB: trying to connect")
        sta_if.connect('%s' % ssid, '%s' % password)

        while not sta_if.isconnected():
            pass
        print("MBB: connected successfully")
    print(sta_if.ifconfig())


def get_url(url):
    response = urequests.get(url)
    content = response.text
    return content


def send_message(text, chat_id):
    url = URL + "sendMessage?text={}&amp;chat_id={}".format(text, chat_id)
    get_url(url)


if __name__ == '__main__':
    print("MBB: start")
    #disable LED on firebeetle board
    p2 = machine.Pin(2, machine.Pin.OUT)
    p2.value(0)
    wake1 = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP) # pin 4 is able to wake
    esp32.wake_on_ext0(pin=wake1, level=esp32.WAKEUP_ALL_LOW)
    print('MBB: Im awake. Going to sleep in 10 seconds')
    connect_to_wifi(SSID, WIFI_PW)
    print("MBB: sending message")
    send_message("Post ist endlich da", CHAT_ID)
    print("MBB: finished")
    sleep(10)
    print('MBB: Going to sleep now')
    machine.deepsleep()

</pre>
</div>
<h2>Testing</h2>
<p><img decoding="async" class="alignnone size-large wp-image-3250" src="https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-1024x768.jpg" alt="" width="525" height="394" srcset="https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-1024x768.jpg 1024w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-300x225.jpg 300w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-768x576.jpg 768w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-1536x1152.jpg 1536w, https://creatronix.de/wp-content/uploads/2020/04/IMG_8888-2048x1536.jpg 2048w" sizes="(max-width: 525px) 100vw, 525px" /></p>
<p>After charging the battery we can use the board independently from the computer</p>
<h2>Sources</h2>
<p>https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf</p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/firebeetle-project/">Mailbox IoT ESP 32 Project</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
