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:
- 2020-04-20
- Revision:
- 24:80345e511574
- Parent:
- 20:36931ee9af85
File content as of revision 24:80345e511574:
/** \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; // read until it sends \r while (c != '\r') { if (serial_xbee.readable()) { c = serial_xbee.getc(); msg[i++] = c; } } } bool XBeeModule::executeWithOk (const char* cmd) { char msg[5]; // send command serial_xbee.printf("%s", cmd); // read response readResponse(msg); // check response if (strncmp(msg, "OK\r", 3) != 0) return false; return true; } void XBeeModule::executeWithRes (const char* cmd, char* res) { // send command serial_xbee.printf("%s", cmd); // read response readResponse(res); } bool XBeeModule::initSequence() { // wait 1 second before starting ThisThread::sleep_for(1000); // send start command if (!executeWithOk("+++")) return false; // wait 1 more second after start command ThisThread::sleep_for(1000); return true; } XBeeModule::XBeeModule (PinName tx, PinName rx, int panID, int channel) : serial_xbee(tx, rx, 9600) { status = _getLocalAddr() && _setChannel(channel) && _setPanID(panID); } bool XBeeModule::getDstAddress (XBeeAddress &addr) { char tmp[10]; std::stringstream s1, s2; uint32_t high, low; // start communication with transceiver if (!initSequence()) return false; // read high address executeWithRes("ATDH \r", tmp); s1<<std::hex<<tmp; s1>>high; // read low address executeWithRes("ATDL \r", tmp); s2<<std::hex<<tmp; s2>>low; // terminate if (!executeWithOk("ATCN \r")) return false; addr.setAddress(low, high); return true; } bool XBeeModule::setDstAddress (XBeeAddress addr) { char s[10]; string low, high; // start communication with transceiver if (!initSequence()) return false; // send high address sprintf(s, "ATDH%X \r", addr.getHighAddr()); if (!executeWithOk(s)) return false; // send low address sprintf(s, "ATDL%X \r", addr.getLowAddr()); if (!executeWithOk(s)) return false; // terminate if (!executeWithOk("ATCN \r")) return false; ThisThread::sleep_for(1000); return true; } bool XBeeModule::_getLocalAddr () { char tmp[10]; uint32_t high, low; std::stringstream s1, s2; // start communication with transceiver if (!initSequence()) return false; // read high address executeWithRes("ATSH \r", tmp); s1<<std::hex<<tmp; s1>>high; // read low address executeWithRes("ATSL \r", tmp); s2<<std::hex<<tmp; s2>>low; // terminate if (!executeWithOk("ATCN \r")) return false; // save address local = XBeeAddress(low, high); return true; } bool XBeeModule::_setChannel (int channel) { char s[10]; // start communication with transceiver if (!initSequence()) return false; 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]; // start communication with transceiver if (!initSequence()) return false; executeWithRes("ATCH \r", s); channel = atoi(s); if (!executeWithOk("ATCN \r")) return -1; ThisThread::sleep_for(1000); return channel; } bool XBeeModule::_setPanID (int panID) { char s[10]; // start communication with transceiver if (!initSequence()) 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]; // start communication with transceiver if (!initSequence()) return false; executeWithRes("ATID \r", s); id = atoi(s); if (!executeWithOk("ATCN \r")) return -1; return id; } int XBeeModule::read (char* msg) { int i = 0; while (serial_xbee.readable()) msg[i++] = serial_xbee.getc(); return i; } void XBeeModule::write (const char* msg, int n) { for (int i=0; i<n; i++) { while(!serial_xbee.writeable()); serial_xbee.putc(msg[i]); } }