BridgeClient.py doesn't full support messages

Hello all.

In the bridgeclient.py on OpenWrt there is only method mailbox(...) which are just sending message but there is no methods for recieving and check available messages.

How can I add some additional methods to the bridgeclient.py to make it similar to Mailbox class on Arduino side?

So, I need two additional classes for mailbox_read and mailbox_available...

Thank you in advance.

Mailbox Read Message REST style calls

Mailbox Class MailboxReadMessage PHP calls

Mailbox Class MailboxWriteMessage

Sonnyyu,

Thank you for replay. I saw your materials. I have long time experience with programming (avr and UNIX gcc).

In my project 32u4 controls water supply, send some alert messages to linino when something wrong happens and reading messages from linino some manual operating command.
On linino I have Python based daemon which reading messages from AVR and perform some special operations (storing day in SQLite, sending sms and etc.

So, when water leaking detected on 32u4, AVR should send such alert message to linino where daemon save events in SQLite and send sms and etc.

For easy programming, I prefer to use Python bridgeclient.py library but I need some routing for checking available messages, like:

avr = bridgeclient.begin()
While True:
If avr.messageavailable():
msg = avr.readmessage()
If msg = "leaking detected":
sensors = avr.get('SENSORS')
... Do something ...

But currently I have to develop own library based on sockets operation and manage communication with JSONServer listening on 5700 by own way.

So, I just what to know is any others developers met with it and already developed some additional library for Python or not?

Hello sonnyyu and all arduino funs.

I have modified bridgeclient.py to be able to read messages from AVR.
Here is my code. In the bridgeclient.py you should replace wait_response method to avoid unnessesary 100ms delay and add to the end additional method get_message:

  def wait_response(self, json, timeout):
    if timeout is None or timeout < 0:
      timeout = 0
    while timeout >= 0:
      r = json.recv()
      if not r is None:
        return r
      timeout -= 0.1
      if timeout >= 0:
        sleep(0.1)
    return None
...
  def get_message(self, timeout=0):
    json = self.socket_open()
    r = self.wait_response(json, timeout)
    m = None
    if not r is None:
      try:
        m = r['data']
      except:
        pass
    self.socket_close()
    return m

I have also renamed message method into send_message.
So now, the bridgeclient_message_example.py is:

#!/usr/bin/python
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge')

from bridgeclient import BridgeClient as bridgeclient

avr = bridgeclient()
avr.begin()
while True:
        msg = avr.getmessage()
        if not msg is None:
                print msg
avr.close()

on AVR side in arduino i have simple test script:

#include <Mailbox.h>
unsigned long timer, counter;
uint8_t cnt;

void setup()
{
	Bridge.begin();
	Mailbox.begin();
}

void loop()
{
	if (millis() - timer > 200) {
		timer = millis();
		Bridge.put("counter", String(counter++));
		Bridge.put("random", String(random(1, 100)));
		if (cnt == 100) {
			Mailbox.writeMessage("Test" + String(counter));
			cnt = 0;
		}
		cnt++;
	}
}

I have also test speed how many while loop iterations occur per second. And i got results:

iter=1904   <- while loop iterations per second
iter=1896
iter=1757
iter=2013
iter=1785
iter=1620
iter=1987
iter=1901
iter=1923
iter=2017
iter=1595
iter=1957
iter=1807
iter=2016
iter=2020
iter=1780
iter=2035
iter=1947
iter=1927
iter=1967
iter=1737
iter=1982
iter=1844
Test6501    <- Got message from AVR
iter=1659
iter=1952
iter=1820
iter=1987
iter=1909
iter=1940
iter=1968
iter=1871
iter=2043
iter=1933
iter=1641
iter=2059
iter=1867
iter=1790
iter=1914
iter=1839
iter=1977
iter=1859
iter=1805
iter=2028
iter=1998
iter=1869
iter=1941
iter=1542
Test6601    <- Got message from AVR
iter=1897

I hope it will helps and arduino developers will include it into next release of bridge.

I've not tried it, but thanks for posting your code. The Mailbox class has always shown a lot of potential, but as you discovered it is incomplete. This should help the situation.