GithubHelp home page GithubHelp logo

matiasinsaurralde / go-mcu Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 4.0 45 KB

Alternative CLI tool and Go package for NodeMCU-based modules.

License: MIT License

Go 91.92% Python 8.08%
esp8266 nodemcu nodemcu-esp8266 golang lua cli

go-mcu's Introduction

go-mcu

GoDoc License: MIT

go-mcu provides an alternative way to work with NodeMCU-based modules like the ESP8266. Inspired by NodeMCU-Tool and nodemcu-uploader. It can be used as a Go package but also as a standalone CLI tool.

One of the goals is to take advantage of Go's cross compilation capability while keeping a minimal set of runtime dependencies.

Getting started

To download the package use:

go get -u github.com/matiasinsaurralde/go-mcu

The CLI tool should be available afterwards:

$ go-mcu
NAME:
   go-mcu - NodeMCU tool (in Golang)

USAGE:
   go-mcu [global options] command [command options] [arguments...]

COMMANDS:
   hwinfo   retrieves hardware info
   upload   upload a file
   run      invoke a script
   restart  trigger node restart
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --port value  Serial port device
   --baud value  Baud rate (default: 115200)
   --help, -h    show help (default: false)

Binaries are available in the releases page.

Supported/tested platforms

  • Mac
  • Windows
  • Linux

Sample code

Full sample here.

package main

import (
	"fmt"
	"log"
	"os"

	nodemcu "github.com/matiasinsaurralde/go-mcu/nodemcu"
)

const (
	logPrefix = "go-mcu "
)

func main() {
	// Setup the port
	node, err := nodemcu.NewNodeMCU("/dev/cu.usbserial-1410", 115200)
	if err != nil {
		panic(err)
	}

	// Set a custom logger
	l := log.New(os.Stdout, logPrefix, log.LstdFlags)
	node.SetLogger(l)

	// Initialization
	err = node.Sync()
	if err != nil {
		panic(err)
	}

	// Upload a file
	err = node.SendFile("test.lua")
	if err != nil {
		panic(err)
	}

	// List files
	files, err := node.ListFiles()
	if err != nil {
		panic(err)
	}
	fmt.Printf("Found %d files\n", len(files))
	for i, file := range files {
		fmt.Printf("File #%d, '%s' (%d bytes)\n", i, file.Name, file.Size)
	}

	// Invoke a file
	err = node.Run("blink.lua")
	if err != nil {
		panic(err)
	}

	// Retrieve hardware info
	hwInfo, err := node.HardwareInfo()
	if err != nil {
		panic(err)
	}
	fmt.Println(hwInfo)
}

Additional features

GPIO module

node.GPIO.Mode(4, nodemcu.GPIO_OUTPUT)
time.Sleep(1 * time.Second)
node.GPIO.Mode(4, nodemcu.GPIO_HIGH)
time.Sleep(1 * time.Second)
node.GPIO.Mode(4, nodemcu.GPIO_LOW)

License

MIT

go-mcu's People

Contributors

matiasinsaurralde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-mcu's Issues

Support multiple log levels

The current implementation uses log in a very simple way.

Requirements:

  • Support multiple log levels.
  • The user should be able to change the log level by using a CLI flag and/or an environment variable.

Implement serial-browser bridge sample

The main idea behind this is to implement a sample with the following characteristics:

  • Main program would implement an HTTP server with Websockets support as well as a very simple HTML page for the client.
  • It should only support a single client.
  • When this client connects, go-mcu (running on the server side) starts a discovery process and finds the NodeMCU-enabled device.
  • If the initial synchronization step (Sync) succeeds on the server side, a device_connected event is sent to the client.
  • The client is now able to interact with the device through WS, e.g. query hwinfo, etc.

Most of the experimentation will be done here until it lands on the main go-mcu repo.

Implement integrity check

When using the upload command, go-mcu should be able to verify the integrity of the transferred files.

Requirements:

  • Take in account scenarios experienced by other tools like this one.
  • Ideally a call to a helper method like shafile should be implemented.
  • It should be possible to enable/disable the integrity check by using the CLI flags.

Cannot upload any type of file on windows

Hi, at first - thank you for doing good work. I've tried to write small file into device running latest (for the time of writing) dev version using windows and got an error:

\go-mcu-1.0.0-windows-amd64.exe --port com9 --baud 460800 upload test.lua
go-mcu 2021/01/13 21:57:56 Sync is called
go-mcu 2021/01/13 21:57:57 Sync ok
go-mcu 2021/01/13 21:57:57 SendFile is called
go-mcu 2021/01/13 21:57:57 File opened 'test.lua'
go-mcu 2021/01/13 21:57:57 SendFile is called, loading recv code
go-mcu 2021/01/13 21:57:58 Calling recv()
go-mcu 2021/01/13 21:57:59 ReadyToRecv is called
2021/01/13 21:57:59 Device isn't ready to receive data

I'm using 460800 baud on all of my devices. And I can upload code with ESPlorer (and other tools). Besides, info read working okay:

go-mcu 2021/01/13 22:06:40 Sync is called
go-mcu 2021/01/13 22:06:40 Sync ok
go-mcu 2021/01/13 22:06:40 HardwareInfo is called
{"ChipID":12135951,"FlashSize":4096,"FlashMode":0,"FlashSpeed":40000000,"FlashID":1458208}

I will definitely try MacOS tomorrow, but still, windows is important for some users of nodemcu. How could I help to find out the reason?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.