A simple wireless protocol to let my examples communicate each other. ssWi stands for Shared Slotted Wireless protocol
Dependents: rover_car rover_pc supervisor watering_unit ... more
This library aims at implementing a simple communication protocol among nodes, abstracting from the hardware. The name ssWi stands for Shared Slotted Wireless. Wireless is part of the name, even though the library abstracts from the hardware, as the first version was entirely focused on the XBee modules and then the name has not been changed.
The communication channel is represented by ssWiChannel, an abstract class which models the channel that the transceivers access to. The concrete classes must implement the functions: init, read and write. The protocol automatically sends and receives data through the selected channel, exploiting the operting system timers. Addresses are not considered as the communication lays on broadcast transmissions.
The protocol provides the ssWiPort abstraction which is like memory areas shared among all the connected nodes. Reading from one port lets the node retrieve the last written value from the other nodes. Writing on one port means sending such value to other nodes.
Objects instantiated from ssWiSocket are the interface for allowing nodes to access the protocol ports.
TODO:
- improve the parsing of the received messages
- communication tests with many nodes (so far, only 2 nodes have been tested)
xbee/xbee.cpp
- Committer:
- mariob
- Date:
- 2017-01-18
- Revision:
- 17:8ba1b278b407
- Parent:
- 15:fb0f6cbc0ed5
- Child:
- 18:133e42dc82b0
File content as of revision 17:8ba1b278b407:
/** \file xbee.cpp * \brief implementation of the ssWi channel using the XBee module * */ #include "xbee.hpp" #include <sstream> void XBeeModule::readResponse (char* msg) { char c = 0; int i = 0; while (c!='\r') if (xbee.readable()) { c = xbee.getc(); msg[i++] = c; } } bool XBeeModule::executeWithOk (const char* cmd) { char msg[5]; xbee.printf("%s", cmd); readResponse(msg); //printf("%s -> %s\n\r", cmd, msg); if (strncmp(msg, "OK\r", 3)!=0) return false; return true; } void XBeeModule::executeWithRes (const char* cmd, char* res) { xbee.printf("%s", cmd); readResponse(res); //printf("\n\r%s -> %s\n\r", cmd, res); } XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : xbee(tx, rx, 64) { status = false; wait(1); if (!_getLocalAddr()) return; wait(1); if (!_setChannel(channel)) return; wait(1); if (!_setPanID(panID)) return; wait(1); status = true; } XBeeAddress XBeeModule::getDstAddress () { char tmp[10]; std::stringstream s1, s2; string high, low; XBeeAddress addr; wait(1); if (!executeWithOk("+++")) return addr; wait(1); executeWithRes("ATDH \r", tmp); s1<<std::hex<<tmp; s1>>high; executeWithRes("ATDL \r", tmp); s2<<std::hex<<tmp; s2>>low; if (!executeWithOk("ATCN \r")) return addr; wait(1); return XBeeAddress(low, high); } bool XBeeModule::setDstAddress (XBeeAddress addr) { char s[10]; string low, high; wait(1); if (!executeWithOk("+++")) return false; wait(1); sprintf(s, "ATDH%s \r", addr.getHighAddr().c_str()); //printf("%s\n\r", addr.getHighAddr().c_str()); if (!executeWithOk(s)) return false; sprintf(s, "ATDL%s \r", addr.getLowAddr().c_str()); //printf("%s\n\r", addr.getLowAddr().c_str()); if (!executeWithOk(s)) return false; if (!executeWithOk("ATCN \r")) return false; return true; } bool XBeeModule::_getLocalAddr () { char tmp[10]; string high, low; std::stringstream s1, s2; if (!executeWithOk("+++")) return false; executeWithRes("ATSH \r", tmp); s1<<std::hex<<tmp; s1>>high; executeWithRes("ATSL \r", tmp); s2<<std::hex<<tmp; s2>>low; if (!executeWithOk("ATCN \r")) return false; local = XBeeAddress(low, high); return true; } bool XBeeModule::_setChannel (int channel) { char s[10]; if (!executeWithOk("+++")) return false; wait(1); sprintf(s, "ATCH%d \r", channel); if (!executeWithOk(s)) return false; if (!executeWithOk("ATCN \r")) return false; return true; } int XBeeModule::getChannel () { int channel; char s[10]; if (!executeWithOk("+++")) return -1; wait(1); executeWithRes("ATCH \r", s); channel = atoi(s); if (!executeWithOk("ATCN \r")) return -1; wait(1); return channel; } bool XBeeModule::_setPanID (int panID) { char s[10]; if (!executeWithOk("+++")) return false; sprintf(s, "ATID%d \r", panID); if (!executeWithOk(s)) return false; if (!executeWithOk("ATCN \r")) return false; return true; } int XBeeModule::getPanID () { int id; char s[10]; if (!executeWithOk("+++")) return -1; executeWithRes("ATID \r", s); id = atoi(s); if (!executeWithOk("ATCN \r")) return -1; wait(1); return id; } int XBeeModule::read (char* msg) { int i = 0; while (xbee.readable()) msg[i++] = xbee.getc(); return i; } void XBeeModule::write (const char* msg, int n) { for (int i=0; i<n; i++) { while(!xbee.writeable()); xbee.putc(msg[i]); } }