Beaglebone black–esp8266 UDP Service

Source: Internet
Author: User

NODEMCU's documentation finally found that ESP8266 's GPIO 2 is indeed a PIN 4,gpio 0 is pin 3.

Https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn

In other words, this API is not for the Earth people to see. According to the instructions, Net.createserver returns the Server, in fact, if using UDP, according to the code inside you do not work out. Finally found the answer in the Www.esp8266.com forum, the content of this article is to describe the UDP in the NODEMCU inside the correct posture.

Wiring, BBB open UART, and minicom related, please see a few. First I go directly to the code.

LUA Code Init.lua

Print (' Init.lua ver 0.9 ')
Wifi.setmode (WiFi. Station)
Print (' Set mode=station (mode= '): Wifi.getmode (): ')
Print (' MAC: ', Wifi.sta.getmac ())
Print (' Chip: ', Node.chipid ())
Print (' heap: ', Node.heap ())
Wifi.sta.config (' SSID ', ' PWD ')
Dofile (' Main.lua ')

Init.lua is the user program code that executes after startup, WiFi is set to Station mode (also can be AP mode), then set SSID and password, if you want to try, the code inside the Wifi.sta.config need to change the correct value. The last sentence of the code is to have it execute another LUA code file.

Main.lua

Print (' Connecting ... ')
Tmr.alarm (0, 1, function ()
If Wifi.sta.getip () ~= Nil Then
Print (' IP: ', Wifi.sta.getip ())
Tmr.stop (0)
Dofile (' Udp.lua ')
End
End

This is nothing, see once every second whether it is connected to the line, if connected, execute another code file. (another file?) It's a little extra, it's my personal hobby, I test each individually.

Udp.lua

Sv=net.createserver (NET. udp,0)
Sv:on (' Receive ', Function (C,PL)
Print (PL)
R=cjson.decode (PL)
if r.cmd = = ' 0 ' Then
Print (' I got a ZERO cmd. ')
C:send (' {\ ' "cmd\": \ "2\", \ "guid\": \ "24f92\", \ "dtype\": \ "powerplug\"} ')
End
End
Sv:listen (4000)
Print (' Server started ')

The key is this on method. You see the official Api,on is the socket method, not the server. Net.createserver, by official instructions, is returned to the server. That is, if you follow the official instructions, my code is wrong. I saw http://www.esp8266.com/viewtopic.php?f=24&t=645 here to know this "wrong" approach ....

Upload Code

My code is written in the BBB with Vim, saved in BBB, and then written with Luatool. Https://github.com/4refr0nt/luatool. Python, git clone down can be used, there is a Telnet code demonstration. This luatool actually helps me manipulate the NODEMCU command and convert the code. Run-time plus –v parameter you'll know what it's for.

Usage is Python luatool.py–p/dev/ttyo2–b 9600–f xxx.lua–v

-P is the serial port,-B is the baud,-f is the local code file,-V is the display process. It's obvious that it just calls Nodemcu's LUA command, and the worst part of it is that it waits for the NODEMCU response to return a character to determine if the operation has been performed smoothly. If you're like me, there's something running in there, there's print, and there's a mistake, because print is mixed up with the luatool response.

Effect

Without BBB, from Windows, via a no-line, send UDP directly to ESP8266, with the BBB minicom looking at these debug (er, print) messages. I first sent the cmd:1 and 2 characters in the past, the reaction is normal, also correctly print out. Then cmd:0, it correctly returned their identity, PowerPlug. Yes, I'm going to play smart sockets.

Broadcast like no problem:

So far today.

Beaglebone black–esp8266 UDP Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.