<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lesson 22. Modules. SIM800L]]></title><description><![CDATA[<h3>The purpose of this lesson</h3>
<p dir="auto">Hi! Today we will get acquainted with the excellent module SIM800L and learn how to receive SMS-messages (figure 1).</p>
<p dir="auto"><img src="https://pp.userapi.com/c851128/v851128058/9bcac/Gb95FpczwxA.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 1</p>
<p dir="auto">This lesson will teach: to exchange UART data with the module; to initialize and configure the module; to receive and read text SMS.</p>
<h3>Short help</h3>
<ul>
<li>Purpose: apply for receiving cellular services</li>
<li>Scope: agriculture, smart home and others</li>
<li>Connection interface: UART2 (MBUS)</li>
<li>Supply voltage: 3.4 to 4.4 V</li>
<li>Compatibility: M5STACK and M5FIRE (with GO module removed)</li>
<li>Form factor: STACK</li>
</ul>
<h3>Brief overview</h3>
<p dir="auto">SIM800L - one of the large family of additional modules (MODULES), designed primarily for M5STASK. This module is supplied in a plastic case (figure 2).</p>
<p dir="auto"><img src="https://pp.userapi.com/c851128/v851128058/9bceb/Kw1q5RT6M6o.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 2</p>
<p dir="auto">On the front side of the module there is a MBUS bus plug, a slot for MicroSIM, a groove for the antenna-spring (figure 3).</p>
<blockquote>
<p dir="auto">Note: contacts: G5 - &gt; RST, G16 - &gt; TXD, G17 - &gt; RXD (Serial2).</p>
</blockquote>
<p dir="auto"><img src="https://pp.userapi.com/c846219/v846219151/18078e/TBocCGixxkU.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 3</p>
<blockquote>
<p dir="auto">Note: if the module is connected to the M5, the SIM card cannot be inserted or removed.</p>
</blockquote>
<p dir="auto">On the reverse side of the module there is an MBUS bus socket, SIM800L module, a spring antenna for GSM-band, a connector (3.5 mm mini-jack) for headphones and a built-in microphone (figure 4).</p>
<p dir="auto"><img src="https://pp.userapi.com/c844616/v844616151/18a9cd/W4pffgehXtM.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 4</p>
<h2>Let's start!</h2>
<h3>Blockly (<a href="http://flow.m5stack.com" target="_blank" rel="noopener noreferrer nofollow ugc">UI Flow</a>)</h3>
<p dir="auto">In order to start using our module, it is necessary to initialize it (figure 5). It will take some time (about 1 minute). At this time, you can not disturb the modem and perform any operations with it, so we temporarily to light the LED BAR red. With the <strong>InitSIM800L</strong> function, we will inform the modem of some parameters that are interesting to us in the future. Note on the variable <strong>isMessage</strong> that takes the value <strong>0</strong>. For what it is necessary - consider below.</p>
<p dir="auto"><img src="https://pp.userapi.com/c847219/v847219020/183ab1/JLe3YmCNveg.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5</p>
<p dir="auto">It takes approximately 10 seconds for the modem to respond to external commands after power is supplied. Next, we need to initialize the UART interface on the corresponding contacts of the device using the <strong>set UART</strong> block.</p>
<blockquote>
<p dir="auto">Please note: TX 17, RX 16</p>
</blockquote>
<p dir="auto">Then, using the <strong>read UART available</strong> block in the loop, make sure that there is some garbage in the UART buffer and consider it symbolically using the <strong>read UART</strong> block. Great! The port is ready for operation. Now restart the module by sending the command <strong>AT+CFUN=1,1\r</strong> using the block <strong>write</strong>. Let's give the modem a normal run of 45 seconds. Turn off the ECHO using <strong>ATE0\r</strong>. Set the text mode GSM encoding to <strong>AT+CSCS="GSM"\r</strong>. Turn on text mode <strong>AT+CMGF=1\r</strong>. Clear the message memory <strong>AT+CMGD=1.4\r</strong>. Ask the module to read incoming messages when receiving <strong>AT+CNMI=1,2,2,1,0\r</strong>. Again, clear the buffer of possible debris. That's all, the module is ready to work (figure 5.1).</p>
<p dir="auto"><img src="https://pp.userapi.com/c847219/v847219020/183a92/0D6mKv0zwi0.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5.1</p>
<p dir="auto">Function <strong>LiveSIM800L</strong> will always be called from a loop <strong>Loop</strong> (figure 5.2). In it we will look if there is incoming data from the module. If so, we consider and bring them to the string form. Now let's see if there is a flag <strong>CMT:</strong>, if there is, then this incoming message put the flag <strong>isMessage</strong> in the state <strong>1</strong>, we need this in order to read the text of the message. Remember the function <strong>parseString</strong>, which we have written in previous tutorials for the Arduino IDE? We're gonna need it here. I translated it to MicroPython, and You just need to copy and paste it into your project ;)</p>
<pre><code>def parseString(idSeparator, separator, str2):
    global parseStringGlobalVar1
    parseStringGlobalVar1 = "
    separatorCount = 0
    for i in range(len(str2)):
        if str2[i] = = separator:
            separatorCount += 1
        else:
            if separatorCount == idSeparator:
                parseStringGlobalVar1 += str2[i]
            elif separatorCount &gt; idSeparator:
                break
    return parseStringGlobalVar1
</code></pre>
<p dir="auto">For those who do not remember or have not read past lessons - the <strong>parseString</strong> function takes three arguments and returns a line segment located between the delimiters: <strong>idSeparator</strong> (int), <strong>separator</strong> (char), <strong>str2</strong> (String). Where <strong>idSeparator</strong> - separator sequence number; <strong>separator</strong> - separator; <strong>str2</strong> - string from which we extract the substring.</p>
<p dir="auto">For example:</p>
<pre><code>cmtStr = '+CMT: "+7XXXXXX0001","","01/01/01,09:30:00+12"\r\n';
print(parseStr(1,'"', cmtStr))
</code></pre>
<p dir="auto">Will be displayed:</p>
<pre><code>+7XXXXXX0001
</code></pre>
<p dir="auto">And if you do so:</p>
<pre><code>print(parseStr(3,'"', cmtStr))
</code></pre>
<p dir="auto">That will be displayed:</p>
<pre><code>01/01/01, 09: 30: 00+12
</code></pre>
<p dir="auto"><img src="https://pp.userapi.com/c847219/v847219020/183a9b/Tnr6hMcwV-s.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5.2</p>
<p dir="auto">Unfortunately Blockly does not allow us to implement our function normally today, but it's okay - there is a workaround to make MicroPython-insert (figure 5.3 - 5.3.1).</p>
<p dir="auto"><img src="https://pp.userapi.com/c847219/v847219020/183aaa/U9EkGRkVE3I.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5.3</p>
<p dir="auto"><img src="https://pp.userapi.com/c845522/v845522129/187801/MIb1oyIIkCw.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5.3.1</p>
<p dir="auto">When will come SMS to call feature-event <strong>IncomingSms</strong>. And the sender number and message text will be in the global variables IncomingSmsPhone and IncomingSmsMessage, respectively.</p>
<p dir="auto"><img src="https://pp.userapi.com/c847219/v847219020/183aa2/2SptatHsF98.jpg" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">Figure 5.4</p>
<p dir="auto">The lesson is finished! :)</p>
<h3>MicroPython (<a href="http://flow.m5stack.com" target="_blank" rel="noopener noreferrer nofollow ugc">UI Flow</a>)</h3>
<pre><code>from m5stack import *
from m5ui import *
import machine
clear_bg(0xffffff)


rgb = RGB_Bar()
btnA = M5Button(name="ButtonA", text="ButtonA", visibility=False)
btnB = M5Button(name="ButtonB", text="ButtonB", visibility=False)
btnC = M5Button(name="ButtonC", text="ButtonC", visibility=False)
label1 = M5TextBox(18, 90, "...", lcd.FONT_DejaVu24, 0x000000)
label2 = M5TextBox(22, 192, "...", lcd.FONT_DejaVu18, 0x000000)
title0 = M5Title(title="SIM800L Module", fgcolor=0xFFFFFF, bgcolor=0x000000)
label4 = M5TextBox(24, 48, "Phone number:", lcd.FONT_DejaVu24, 0x000000)
label3 = M5TextBox(48, 145, "Message:", lcd.FONT_DejaVu24, 0x000000)

from numbers import Number

idSeparator = None
separator = None
str2 = None
parseStringGlobalVar1 = None
IncomingSmsPhone = None
uart = None
buf = None
IncomingSmsMessage = None
isMessage = None

def parseString(idSeparator, separator, str2):
    global parseStringGlobalVar1
    parseStringGlobalVar1 = ''
    separatorCount = 0
    for i in range(len(str2)):
        if str2[i] == separator:
            separatorCount += 1
        else:
            if separatorCount == idSeparator:
                parseStringGlobalVar1 += str2[i]
            elif separatorCount &gt; idSeparator:
                break
    return parseStringGlobalVar1

def IncomingSms():
  global idSeparator, separator, str2, parseStringGlobalVar1, IncomingSmsPhone, uart, buf, IncomingSmsMessage, isMessage
  label1.setText(str(IncomingSmsPhone))
  label2.setText(str(IncomingSmsMessage))
  speaker.volume(1)
  for count in range(4):
    rgb.set_all(0x00cccc)
    speaker.sing(659, 1/8)
    wait(0.125)
    rgb.set_all(0x000000)
    wait(0.125)

def InitSIM800L():
  global idSeparator, separator, str2, parseStringGlobalVar1, IncomingSmsPhone, uart, buf, IncomingSmsMessage, isMessage
  wait(10)
  uart = machine.UART(1, tx=17, rx=16)
  uart.init(115200, bits=8, parity=None, stop=1)
  while uart.any():
    if uart.read(1):
      pass
  uart.write('AT+CFUN=1,1\r')
  wait(45)
  uart.write('ATE0\r')
  wait(1)
  uart.write('AT+CSCS="GSM"\r')
  wait(1)
  uart.write('AT+CMGF=1\r')
  wait(1)
  uart.write('AT+CMGD=1,4\r')
  wait(1)
  uart.write('AT+CNMI=1,2,2,1,0\r')
  wait(1)
  while uart.any():
    if uart.read(1):
      pass

def LiveSIM800L():
  global idSeparator, separator, str2, parseStringGlobalVar1, IncomingSmsPhone, uart, buf, IncomingSmsMessage, isMessage
  if uart.any():
    buf = str((uart.readline()))
    if isMessage == 0:
      if buf.count('CMT:') == 1:
        isMessage = (isMessage if isinstance(isMessage, Number) else 0) + 1
        IncomingSmsPhone = parseString(1, '"', buf)
    else:
      if buf.count('\\x') == 0:
        isMessage = (isMessage if isinstance(isMessage, Number) else 0) + 0
        buf = buf[2:-5]
        if buf.count('CMT:') == 0 and len(buf) != 0:
          IncomingSmsMessage = buf
          IncomingSms()
    wait(1)



rgb.set_all(0xff0000)
InitSIM800L()
rgb.set_all(0x000000)
isMessage = (isMessage if isinstance(isMessage, Number) else 0) + 0
while True:
  LiveSIM800L()
  wait(0.001)
</code></pre>
<h3>C &amp; C++ (<a href="https://www.arduino.cc/en/Main/Software" target="_blank" rel="noopener noreferrer nofollow ugc">Arduino IDE</a>)</h3>
<p dir="auto">Example not yet written ^_^</p>
<h3>Downloads</h3>
<p dir="auto">Alas, there is nothing here today :)</p>
]]></description><link>https://community.m5stack.com/topic/539/lesson-22-modules-sim800l</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Mar 2026 11:02:01 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/539.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 25 Jan 2019 06:14:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Lesson 22. Modules. SIM800L on Wed, 31 Jul 2019 13:22:09 GMT]]></title><description><![CDATA[<p dir="auto">Is there Arduino code available? I have some problems with the examples added to the library.</p>
]]></description><link>https://community.m5stack.com/post/4925</link><guid isPermaLink="true">https://community.m5stack.com/post/4925</guid><dc:creator><![CDATA[ckuehnel]]></dc:creator><pubDate>Wed, 31 Jul 2019 13:22:09 GMT</pubDate></item></channel></rss>