I have added the Agrinet.cpp
and modified a bit. It compiles fine:
#include "mbed.h"
#include <string.h>
#include <ctype.h>
//#include "C12832.h"
#include <string>
#include <sstream>
//PAN ID: 416772694E6574
// Configuration Section
// ---------------------------------------------------------------------------------
//#define DEBUG
#define BaseStation
//#define SensorNode
//#define Display
#define max_msg 128 //const unsigned int
// ----------------------------------------------------------------------------------
#ifdef SensorNode
#ifdef BaseStation
#undef BaseStation
#endif
#endif
#ifdef DEBUG
Serial usb(USBTX, USBRX);
#endif
//#ifdef Display
//C12832 lcd(p5, p7, p6, p8, p11);
//#endif
class AgriNet :
public Serial
{
public:
AgriNet(PinName tx, PinName rx);
struct agriProtocol
{ //Structure for storing AgriNet commands
uint16_t UUID;
uint32_t UTCS;
#ifdef BaseStation
bool LBAT;
uint8_t PWRS;
float STMP;
float ATMP;
float LIGT;
float COND;
float MOIS;
float HUMD;
float PHLV;
#endif
#ifdef SensorNode
bool PDAT;
bool WAKE;
bool LPMD;
bool PALL;
bool PING;
bool BTRS;
bool BRST;
#endif
//To be added
};
enum agriData
{ //Enumeration of AgriNet Data options
ERRR,
UUID,
UTCS,
#ifdef BaseStation
LBAT,
PWRS,
STMP,
ATMP,
MOIS,
LIGT,
PHLV,
COND,
HUMD,
GPSC,
#endif
#ifdef SensorNode
PING,
BTRS,
PDAT,
PALL,
LPMD,
WAKE,
BRST,
#endif
};
agriProtocol data;
void receive(void);
#ifdef BaseStation
void transmit(char msg[], int node);
#endif
#ifdef SensorNode
void transmit(char msg[]);
#endif
template<typename T>
void add_msg(char type[], T data, bool end);
void clear_msg(void);
agriData resolveString(char* s);
static unsigned char transmit_msg[64];
private:
/* enum agriData { //Enumeration of AgriNet Data options
ERRR,
UUID,
UTCS,
#ifdef BaseStation
LBAT,
PWRS,
STMP,
ATMP,
MOIS,
LIGT,
PHLV,
COND,
HUMD,
GPSC,
#endif
#ifdef SensorNode
PING,
BTRS,
PDAT,
PALL,
LPMD,
WAKE,
BRST,
#endif
}; */
char* command_ptr;
char* val_ptr;
void reset(void);
char* translate(char* p);
void parse(char* p);
//agriData resolveString(char *s);
//Serial xbee;
#ifdef SensorNode
static unsigned char msg_frame[5];
static unsigned int chksm_1;
#endif
#ifdef BaseStation
static unsigned char msg_frame[5];
static unsigned char node_1[10];
static unsigned char node_2[10];
static unsigned int chksm_1;
#endif
static unsigned int chksm;
std::string packet;
};
unsigned char AgriNet::transmit_msg[] = { 0 };
#ifdef SensorNode
unsigned char AgriNet::msg_frame[] = { 0x7E, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00 };
unsigned int AgriNet::chksm_1 = 0x20D;
#endif
#ifdef BaseStation
unsigned char AgriNet::msg_frame[] = { 0x7E, 0x10, 0x00, 0x00, 0x00 };
unsigned char AgriNet::node_1[] = { 0x00, 0x13, 0xA2, 0x00, 0x41, 0xC0, 0x2D, 0x7C, 0x23, 0xFF };
unsigned char AgriNet::node_2[] = { 0, 0x13, 0xA2, 0x00, 0x41, 0xC0, 0x29, 0x31, 0x84, 0x19 };
unsigned int AgriNet::chksm_1 = 0x10;
#endif
unsigned int AgriNet::chksm = 0x00;
/**
* @brief
* @note
* @param
* @retval
*/
AgriNet::AgriNet(PinName tx, PinName rx) :
Serial(tx, rx)
{}
/**
* @brief
* @note
* @param
* @retval
*/
AgriNet::agriData AgriNet::resolveString(char* s)
{
if (strcmp(s, "UUID") == 0) {
return UUID;
}
if (strcmp(s, "UTCS") == 0) {
return UTCS;
}
#ifdef BaseStation
if (strcmp(s, "PWRS") == 0) {
return PWRS;
}
if (strcmp(s, "LBAT") == 0) {
return LBAT;
}
if (strcmp(s, "STMP") == 0) {
return STMP;
}
if (strcmp(s, "ATMP") == 0) {
return ATMP;
}
if (strcmp(s, "MOIS") == 0) {
return MOIS;
}
if (strcmp(s, "LIGT") == 0) {
return LIGT;
}
if (strcmp(s, "PHLV") == 0) {
return PHLV;
}
if (strcmp(s, "COND") == 0) {
return COND;
}
if (strcmp(s, "HUMD") == 0) {
return HUMD;
}
if (strcmp(s, "GPSC") == 0) {
return GPSC;
}
#endif
#ifdef SensorNode
if (strcmp(s, "PING") == 0) {
return PING;
}
if (strcmp(s, "PDAT") == 0) {
return PDAT;
}
if (strcmp(s, "BTRS") == 0) {
return BTRS;
}
if (strcmp(s, "PALL") == 0) {
return PALL;
}
if (strcmp(s, "LPMD") == 0) {
return LPMD;
}
if (strcmp(s, "WAKE") == 0) {
return WAKE;
}
#endif
//to be added
return ERRR;
}
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::reset(void)
{
command_ptr = NULL;
val_ptr = NULL;
#ifdef Display
lcd.cls();
#endif
}
/**
* @brief
* @note
* @param
* @retval
*/
char* AgriNet::translate(char* p)
{
static char message[max_msg];
memset(message, 0, max_msg);
memcpy(message, (p + 16), max_msg); //+
message[max_msg - 1] = '\0';
return message;
}
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::parse(char* p)
{
char command[5];
#ifdef DEBUG
usb.printf("Parsing\r\n");
#endif
reset();
for (unsigned int i = 0; i < strlen(p); i++) {
p[i] = toupper(p[i]); //Make all CAPS for consistency
}
//First extraction
command_ptr = strtok(p, " ");
#ifdef Display
lcd.cls();
int i = 0;
int j = 5;
#endif
while (command_ptr != NULL)
{
#ifdef DEBUG
int pcount = 1;
#endif
strncpy(command, command_ptr, 4); //extract 4 digit command
command[4] = '\0'; //append charcter
switch (resolveString(command)) {
case UUID:
val_ptr = command_ptr + 4;
data.UUID = atol(val_ptr);
#ifdef DEBUG
usb.printf("UUID: %u \r\n", data.UUID);
#endif
break;
case UTCS:
val_ptr = command_ptr + 4;
data.UTCS = atol(val_ptr);
#ifdef DEBUG
usb.printf("UTCS: %u \r\n", data.UTCS);
#endif
break;
#ifdef BaseStation
case STMP:
val_ptr = command_ptr + 4;
data.STMP = atof(val_ptr);
#ifdef DEBUG
usb.printf("SOILTEMP: %4.2f \r\n", data.STMP);
#endif
break;
case ATMP:
val_ptr = command_ptr + 4;
data.ATMP = atof(val_ptr);
#ifdef DEBUG
usb.printf("AIRTEMP: %4.2f \r\n", data.ATMP);
#endif
break;
case MOIS:
val_ptr = command_ptr + 4;
data.MOIS = atof(val_ptr);
#ifdef DEBUG
usb.printf("SOILMOIS: %4.2f \r\n", data.MOIS);
#endif
break;
case LIGT:
val_ptr = command_ptr + 4;
data.LIGT = atof(val_ptr);
#ifdef DEBUG
usb.printf("LIGHT: %4.2f \r\n", data.LIGT);
#endif
break;
case COND:
val_ptr = command_ptr + 4;
data.COND = atof(val_ptr);
#ifdef DEBUG
usb.printf("CONDUCTIVITY: %4.2f \r\n", data.COND);
#endif
break;
case HUMD:
val_ptr = command_ptr + 4;
data.HUMD = atof(val_ptr);
#ifdef DEBUG
usb.printf("AIR HUMIDITY: %4.2f \r\n", data.HUMD);
#endif
break;
case PHLV:
val_ptr = command_ptr + 4;
data.PHLV = atof(val_ptr);
#ifdef DEBUG
usb.printf("PH LEVEL: %4.2f \r\n", data.PHLV);
#endif
break;
case LBAT:
val_ptr = command_ptr + 4;
data.LBAT = atoi(val_ptr);
#ifdef DEBUG
usb.printf("LOWBAT: %i \r\n", data.LBAT);
#endif
break;
#endif
#ifdef SensorNode
case PDAT:
val_ptr = command_ptr + 4;
data.PDAT = atoi(val_ptr);
break;
case PING:
val_ptr = command_ptr + 4;
data.PING = atoi(val_ptr);
break;
case WAKE:
val_ptr = command_ptr + 4;
data.WAKE = atoi(val_ptr);
break;
case PALL:
val_ptr = command_ptr + 4;
data.PALL = atoi(val_ptr);
break;
case BTRS:
val_ptr = command_ptr + 4;
data.BTRS = atoi(val_ptr);
break;
case LPMD:
val_ptr = command_ptr + 4;
data.LPMD = atoi(val_ptr);
break;
case BRST:
val_ptr = command_ptr + 4;
data.BRST = atoi(val_ptr);
break;
#endif
default: //ignore unknown data
memset(command, 0, 5);
val_ptr = NULL;
break;
}
#ifdef Display
if (i >= 32) {
i = 0;
j += 20;
}
lcd.locate(j, i);
lcd.printf("%s: %s", command, val_ptr);
i += 8;
#endif
#ifdef DEBUG
usb.printf("Parsing Pass %i\r\n", pcount);
#endif
//Reset pointers and command bytes
memset(command, 0, 5);
val_ptr = NULL;
command_ptr = strtok(NULL, " ");
}
#ifdef DEBUG
usb.printf("Success\r\n");
#endif
}
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::receive(void) //recived data from UART
{
static char input[max_msg]; //Set maximum message size
static unsigned int input_pos = 0; //reset input positon coutner
#ifdef DEBUG
char c = usb.getc();
#else
char c = getc();
#endif
switch (c) {
case '\r': // end of text
#ifdef DEBUG
usb.printf("\r\n Recv: %s\r\n", input);
#endif
parse(translate(input)); //Move to translate for API mode
memset(input, 0, max_msg);
input_pos = 0;
break;
case '\n': // discard carriage return
break;
default:
if (input_pos < (max_msg)) {
input[input_pos++] = c;
#ifdef DEBUG
usb.putc(c);
#endif
break;
}
}
}
#ifdef BaseStation
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::transmit(char msg[], int node)
{
putc(msg_frame[0]);
putc(0x00);
putc(14 + strlen(msg));
for (int x = 1; x < 3; x++) {
putc(msg_frame[x]);
}
switch (node) {
case 1:
for (int x = 0; x < 10; x++) {
putc(node_1[x]);
chksm += node_1[x];
}
break;
case 2:
for (int x = 0; x < 10; x++) {
putc(node_2[x]);
chksm += node_2[x];
}
break;
default:
break;
}
for (int x = 3; x < 5; x++) {
putc(msg_frame[x]);
chksm += msg_frame[x];
}
printf(msg);
for (unsigned int x = 0; x < strlen(msg); x++) {
chksm += msg[x];
}
chksm += chksm_1;
chksm = 0xFF & chksm;
chksm = 0xFF - chksm;
putc(chksm);
chksm = 0;
}
#endif
#ifdef SensorNode
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::transmit(char msg[])
{
putc(msg_frame[0]);
putc(0x00);
putc(14 + strlen(msg));
for (int x = 1; x < 15; x++) {
putc(msg_frame[x]);
}
printf(msg);
// Calculate checksum for message data
for (int x = 0; x < strlen(msg); x++) {
chksm += msg[x];
}
chksm += chksm_1;
chksm = 0xFF & chksm;
chksm = 0xFF - chksm;
putc(chksm);
chksm = 0;
}
#endif
/**
* @brief
* @note
* @param
* @retval
*/
template<typename T>
void AgriNet::add_msg(char type[], T data, bool end)
{
std::string tempstr;
tempstr += string(transmit_msg);
tempstr += type;
std::stringstream val;
val << data;
tempstr += val.str();
tempstr += " ";
if (end) {
tempstr = tempstr + "\r\n";
}
std::strcpy(transmit_msg, tempstr.c_str());
}
/**
* @brief
* @note
* @param
* @retval
*/
void AgriNet::clear_msg(void)
{
memset(transmit_msg, 0, strlen((const char*)transmit_msg));
packet.clear();
}
DigitalOut led1(LED1);
AgriNet AgriNet(USBTX, USBRX);
/**
* @brief
* @note
* @param
* @retval
*/
int main()
{
while (true) {
led1 = !led1;
wait_ms(500);
}
}
I hope it helps too.
Best regards, Zoltan