Okay. I know we talked earlier about doing a very simple protocol for my project that uses rs485 to communicate.. For easy debugging, after all, I decided to go with the original plan - which was an AT-command set designed for project. This way it is way easier, as I don't have my "master" device or relay board yet done (and actually I am still waiting for some parts).. And I have a usb-serial->rs-485 adapter in my computer and a serial session through that.. And therefore, I am doing by hand the work of master device..
Project has grown - it no longer with to maple mini r2's flash, ofcourse, I maybe could minimize it a bit.. Bit I think tweaking comes when I have the system working, so it's atleast usable. Filetransfer hasn't been implemented yet, code has place for it.. But I think, I'l go with the filetransfer when I have my protocol ready first including everything I am needing..
This isn't so much a opensource project - it's more like a experiment - but when I was playing with Arduinos, I was happy that there was so much source code available for the project, ofcourse, usually code wasn't usable for me, but it gave me a great starting point.. So I decided to share my work done so far..
It's available here: http://pastebin.com/M0dhRqWt
Oh - the code starts with #include <itoa.h>
I got itoa.h from Arduino Due's IDE (Arduino Beta), copied itoa.h and itoa.c/cpp (can't remember which they had used) to libraries/itoa and it worked out of the box.
commandBegins was first done with strncmp, but for unknown reason - it started failing, so I implemented my own.. Maybe the reason that it was failing for was that at that time I was still running from RAM and.. Propably my project eated up all the remaining RAM.
---
This can be tested also with serial without rs485, just connect your terminal to serial1. Little help is documented into the source code.
Here's how I thought this could work; devices are 1xRaspberry PI connected to rs485, 1xMaple with 16(or at max. 32) relays. I call this the relayboard. And other devices: various maple or arduinos or similars connected to rs485 using this protocol, having various sensors connected and sensor data is acquired over this protocol with rs485.
When system starts, only thing starting is relayboard. Relayboard has a watchdog enabled, so if it hangs - it automaticly reboots. When it starts, it checks that all relays are open - and then it closes relay which powers raspberry pi and gives it enough time to boot. After this it enables internal timer that resets every time data moves on rs485. If there has not been data in certain time, it's propably because master(raspberry pi) has hanged, this is the time when we close all relays and restart. A external hardware watchdog for raspberry.
Raspberry boots and calls to device 32, which is the only static phone number that we aren't going to change and belongs to relayboard. Telling it to power up relay1, which therefore powers up device1. Initially all devices have phone number 99 and raspberry calls to that number after 2 seconds which is enough for boot. If device does not answer, there is no device 1. Raspberry makes a note of this. If device answers, raspberry asks the device to change it's phone number from 99 to same number as relay is, in this case: 1. This is done to all relays/devices of the system, so we can map amount and locations of the devices. There is also going to be command that asks device to report it's capabilities (sensors) to raspberry so raspberry knows what it can do with the device.
On the normal loop, raspberry queries mapped devices one by one asking for values from sensors, and ofcourse, checks that devices answer. If device doesn't answer, it has propably hanged - raspberry then asks relayboard to recycle the power of device and then contact the device again, to give it the phonenumber accordingly to it's relay.
command +++ATZZ# returns always OK. This can be used to test if we lost connection to device, or if it hanged because of what we asked it to do, or something. Forgot to add it to the help in source.
If raspberry hangs, everything must be restarted, because raspberry maps devices after boot. I could make it remember the devices, but then the system would need changes if I add a device or remove one. Idea is that I can just throw in more devices if I feel it's needed and this way system becomes modular.
Yeah, and I know my code ain't perfect. But what a heck- it works. If you can do it better, feel free..