{"id":4628,"date":"2015-02-20T01:10:08","date_gmt":"2015-02-20T01:10:08","guid":{"rendered":"http:\/\/www.softov.com.br\/blog\/?p=4628"},"modified":"2019-12-23T13:47:40","modified_gmt":"2019-12-23T13:47:40","slug":"huawei-as-sms-gateway-on-freebsd","status":"publish","type":"post","link":"https:\/\/softov.com.br\/blog\/2015\/02\/20\/huawei-as-sms-gateway-on-freebsd\/","title":{"rendered":"Huawei as SMS gateway on FreeBSD"},"content":{"rendered":"<p>Have you tried to send SMS using U3G modens? In this topic I will show a example how to send SMS using U3G modens in FreeBSD.<\/p>\n<p><!--more--><\/p>\n<p>FreeBSD has built in support for this device, just add<\/p>\n<blockquote><p>u3g_load=&#8221;YES&#8221;<\/p><\/blockquote>\n<p>to your \/boot\/loader.conf and reboot or issue:<\/p>\n<blockquote><p>kldload u3g<\/p><\/blockquote>\n<p>as root.<\/p>\n<p>Here&#8217;s sample Python script to read SMS data. It uses pySerial.<\/p>\n<p>[code language=&#8221;python&#8221;]<\/p>\n<p># encoding: utf-8<br \/>\nimport serial<br \/>\nimport time<br \/>\nimport types<\/p>\n<p># converts UCS2 to UNICODE<br \/>\ndef ucs(t):<br \/>\nw = &quot;&quot;<br \/>\nfor i in range(len(t)\/4):<br \/>\nw += unichr(int(t[i*4:i*4+4],16))<br \/>\nreturn w<\/p>\n<p># sends AT command to modem and receives result<br \/>\ndef at(k):<br \/>\nglobal ser<\/p>\n<p>ser.write(k+&quot;\\r&quot;)<br \/>\nbuf = &quot;&quot;<br \/>\nwhile buf[-6:] != &quot;\\r\\nOK\\r\\n&quot;:<br \/>\nbuf += ser.read(1)<br \/>\nbuf = buf[len(k)+3:].split(&quot;\\r\\n&quot;)[:-2]<br \/>\nreturn buf<\/p>\n<p># reads SMSes from modem and prints it on screen<br \/>\ndef getSMS():<br \/>\nsms = at(&quot;AT+CMGL=\\&quot;ALL\\&quot;&quot;)<br \/>\nwhile len(sms) &gt;= 2:<br \/>\nmeta = sms[0].split(&quot;,&quot;)<br \/>\nidx = meta[0][6:].strip()<br \/>\nsender = ucs(meta[2][1:-1])<br \/>\nw = &quot;&quot;<br \/>\ndata = ucs(sms[1])<br \/>\nsms = sms[2:]<br \/>\nprint &quot;From: %s\\nContent:\\n%s\\n&#8212;&quot; % (sender,data)<br \/>\nkomenda(&quot;AT+CMGD=&quot;+idx)<\/p>\n<p># change your port below<br \/>\nser = serial.Serial(&quot;\/dev\/ttyU0.0&quot;,115200,timeout=5,rtscts=True)<br \/>\nser.open()<br \/>\nat(&quot;AT&quot;)<br \/>\n# at(&quot;AT+CPIN=****&quot;) # enter PIN if you have one<br \/>\nat(&quot;AT+CMGF=1&quot;) # puts modem in text mode<br \/>\nat(&quot;AT+CPMS=\\&quot;SM\\&quot;&quot;) # change SMS storage<br \/>\nat(&quot;AT+CSCS=\\&quot;UCS2\\&quot;&quot;) # change encoding<br \/>\nwhile True:<br \/>\ngetSMS()<br \/>\ntime.sleep(0.5)<\/p>\n<p>[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you tried to send SMS using U3G modens? In this topic I will show a example how to send SMS using U3G modens in FreeBSD.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94],"tags":[],"_links":{"self":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/4628"}],"collection":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4628"}],"version-history":[{"count":3,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/4628\/revisions"}],"predecessor-version":[{"id":4700,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/posts\/4628\/revisions\/4700"}],"wp:attachment":[{"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softov.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}