Code Monkey home page Code Monkey logo

drogue-esp8266's Introduction

Drogue IoT ESP8266

crates.io docs.rs Matrix

A network driver for an ESP8266 attached via a USART. See esp8266-at-driver for a driver that works with async Rust.

Currently requires the ESP to be flashed with a 1.7.0.x version of the AT firmware provided by Espressif.

To use, you must configure your USART as 115,200 bps and 8-N-1, along with selecting the enable and reset connections to the board.

By using the initialize(...) function, you will get a 2-tuple back, container the Adapter and an Ingress object:

static mut RESPONSE_QUEUE: Queue<Response, U2> = Queue(i::Queue::new());
static mut NOTIFICATION_QUEUE: Queue<Response, U16> = Queue(i::Queue::new());

let (adapter, ingress) = esp8266::initialize(
    tx, rx,
    &mut en, &mut reset,
    unsafe { &mut RESPONSE_QUEUE },
    unsafe { &mut NOTIFICATION_QUEUE },
).unwrap();

In an RTIC app, this would occur during the init phase of the app, and both pieces would be placed into the shared resources.

The Ingress should be wired up to the USART interrupt in order to receive octets from the serial port:

#[task(binds = USART6, priority = 10, resources = [ingress])]
fn usart(ctx: usart::Context) {
    if let Err(b) = ctx.resources.ingress.isr() {
        info!("failed to ingress {}", b as char);
    }
}

Additionally, the Ingress should be attached to a timer loop in order to process all received octets in a timely fashion. The cycle speed is left as an exercise for the reader:

#[task(schedule = [digest], priority = 2, resources = [ingress])]
fn digest(mut ctx: digest::Context) {
    ctx.resources.ingress.lock(|ingress| ingress.digest());
    ctx.schedule.digest(ctx.scheduled + (DIGEST_DELAY * 100_000).cycles())
        .unwrap();
}

Once all iterrupts/tasks are enabled, the adapter may then be used in order to join a Wifi access point:

let result = adapter.join("myaccesspoint", "thepassword");

After successfully joining, the adapter may be convereted into a TCPNetworkStack:

 let network = adapter.into_network_stack();

 let socket = network.open(Mode::Blocking).unwrap();

 let socket_addr = SocketAddr::new(
     IpAddr::from_str("192.168.1.245").unwrap(),
     80,
 );

 let mut socket = network.connect(socket, socket_addr).unwrap();
 let result = network.write(&mut socket, b"GET / HTTP/1.1\r\nhost:192.168.1.245\r\n\r\n").unwrap();

 loop {
     let mut buffer = [0; 128];
     let result = network.read(&mut socket, &mut buffer);
     match result {
         Ok(len) => {
             if len > 0 {
                 let s = core::str::from_utf8(&buffer[0..len]);
                 match s {
                     Ok(s) => {
                         info!("recv: {} ", s);
                     }
                     Err(_) => {
                         info!("recv: {} bytes (not utf8)", len);
                     }
                 }
             }
         }
         Err(e) => {
             info!("ERR: {:?}", e);
             break;
         }
     }
 }

drogue-esp8266's People

Contributors

bobmcwhirter avatar ctron avatar lulf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

drogue-esp8266's Issues

Closed sockets should still be read til the end of data

When the ESP sends a close notification for a socket, that socket may still have data in its buffer.

However, when reading, we check for the "close" state first. So you are unable to read the remaining data.

To my understanding it may also be the case that the ESP sends out a close notification, still holding data for that socket. So we may have the additional issue of reading data from a socket we know that is closed, but need to drain the ESP's socket buffer.

Does not compile

Hi. I tried to compile this project:

$ rustc --version
rustc 1.45.0 (5c1f21c3b 2020-07-13)
$ cargo build
   Compiling drogue-esp8266 v0.1.0 (/mnt/data/Projects/drogue-esp8266)
error[E0599]: no method named `moveslice` found for array `[u8; 4096]` in the current scope
  --> src/buffer.rs:47:29
   |
47 |                 self.buffer.moveslice(start..start + len, 0);
   |                             ^^^^^^^^^ method not found in `[u8; 4096]`
   |
   = note: the method `moveslice` exists but the following trait bounds were not satisfied:
           `[u8; 4096]: core::convert::AsMut<[_]>`
           which is required by `[u8; 4096]: moveslice::Moveslice<_, _>`
           `[u8]: core::marker::Sized`
           which is required by `[u8]: moveslice::Moveslice<_, _>`

error[E0277]: arrays only have std trait implementations for lengths 0..=32
  --> src/protocol.rs:90:18
   |
90 |     DataReceived([u8; 128], usize),
   |                  ^^^^^^^^^ the trait `core::array::LengthAtMost32` is not implemented for `[u8; 128]`
   |
   = note: required because of the requirements on the impl of `core::fmt::Debug` for `[u8; 128]`
   = note: required because of the requirements on the impl of `core::fmt::Debug` for `&[u8; 128]`
   = note: required for the cast to the object type `dyn core::fmt::Debug`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused import: `moveslice::Moveslice`
 --> src/buffer.rs:3:5
  |
3 | use moveslice::Moveslice;
  |     ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `drogue-esp8266`.

To learn more, run the command again with --verbose.

Any ideas? I also tried with nightly, but it does not work either.

Handle unknown responses gracefully

Currently the parser always tried to parse from the beginning of the buffer. This has the disadvantage, that it cannot handle unknown responses. Or handle a response which e.g. misses a character due to transmission errors.

For example:

08:45:03.936 INFO - parsing 31 [DHCP TIMEOUT                                                                                                         
08:45:03.936 WIFI DISCONNECT                                                                                 
08:45:03.936 ]                                                                                                                                              
08:45:06.437 INFO - parsing 47 [DHCP TIMEOUT                                                                 
08:45:06.437 WIFI DISCONNECT                                                                                                                                
08:45:06.437 WIFI CONNECTED                                                                                  
08:45:06.437 ]                                                                                                                                              
08:45:10.939 INFO - parsing 66 [DHCP TIMEOUT                                                                 
08:45:10.939 WIFI DISCONNECT                                                                                                                                
08:45:10.939 WIFI CONNECTED                                                                                                                          
08:45:10.939 DNS Fail                                                                                                                             
08:45:10.939                                                                                                                                         
08:45:10.939 ERROR                                                                                                                                          
08:45:10.939 ]                                                                                                                                              
08:45:43.950 INFO - parsing 97 [DHCP TIMEOUT                                                                                                      
08:45:43.950 WIFI DISCONNECT                                                                                                                                
08:45:43.950 WIFI CONNECTED                                                                                                      
08:45:43.950 DNS Fail                                                                                                                             
08:45:43.950                                                                                                        
08:45:43.950 ERROR                                                                                                                                   
08:45:43.950 DHCP TIMEOUT                                                                                                        
08:45:43.950 WIFI DISCONNECT                                                                                                                 
08:45:43.950 ]                                                                                                                   
08:45:46.951 INFO - parsing 113 [DHCP TIMEOUT                                                                                                               
08:45:46.951 WIFI DISCONNECT                                                                                                     
08:45:46.951 WIFI CONNECTED                                                                                                                          
08:45:46.951 DNS Fail                                                                                        
08:45:46.951                                                                                                                                 
08:45:46.951 ERROR                                                                                           
08:45:46.951 DHCP TIMEOUT                                                                                                                                   
08:45:46.951 WIFI DISCONNECT                                                                                                     
08:45:46.951 WIFI CONNECTED                                                                                                                       
08:45:46.951 ]                                                                                                                   
08:45:47.451 INFO - parsing 126 [DHCP TIMEOUT                                                                       
08:45:47.451 WIFI DISCONNECT                                                                                                                         
08:45:47.451 WIFI CONNECTED                                                                                  
08:45:47.451 DNS Fail                                                                                                                        
08:45:47.451                                                                                                 
08:45:47.451 ERROR                                                                                                                                          
08:45:47.451 DHCP TIMEOUT                                                                                           
08:45:47.451 WIFI DISCONNECT                                                                                                                      
08:45:47.451 WIFI CONNECTED                                                        
08:45:47.451 WIFI GOT IP                                                                                                         
08:45:47.451 ]    

You can see that the internal buffer is growing, because the first message DHCP TIMEOUT is unknown to the parser, and blocks the rest of the buffer.

We should find a way to handle situations like this gracefully. Maybe doing more line based parsing, and dropping unknown responses, could be a solution to the issue.

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.