Skip to content

Commit

Permalink
Arduino Client supports MQTT-SN over UDP
Browse files Browse the repository at this point in the history
  • Loading branch information
ty4tw committed Jun 21, 2014
1 parent c83cc12 commit 2dc61c4
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 56 deletions.
Binary file modified Client/Debug/Client
Binary file not shown.
2 changes: 1 addition & 1 deletion Client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $(SUBDIR)/mqttsn.cpp \
$(SUBDIR)/mqttsnClient.cpp \
$(SUBDIR)/zbeeStack.cpp \
$(SUBDIR)/udpStack.cpp \
$(SUBDIR)/util.cpp
$(SUBDIR)/mqUtil.cpp

CXX := g++
CPPFLAGS +=
Expand Down
2 changes: 1 addition & 1 deletion Client/src/LinuxClientSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ XBEE_APP_CONFIG = { { 0, 0, 0 },{ 0, 0, false, false, 0, 0 } };
#endif

#ifdef NETWORK_UDP
UDP_APP_CONFIG = { { 0, 0 }, { 0, 0, false, false, 0, 0 } };
UDP_APP_CONFIG = {{ {0,0,0,0}, 0, {0,0,0,0}, {0,0,0,0,0,0} },{ 0, 0, false, false, 0, 0 } };
#endif
/*==================================================
* Create Topic
Expand Down
36 changes: 29 additions & 7 deletions Client/src/SoilMoistureClientSample.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@

#include <MQTTSN_Application.h>
#include <mqttsnClientAppFw4Arduino.h>

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
/*============================================
*
* MQTT-SN Client Application for Arduino
*
*===========================================*/
#ifdef NETWORK_XBEE
XBEE_APP_CONFIG = {
{
9600, //Baudrate
Expand All @@ -52,11 +55,30 @@
300, //KeepAlive
true, //Clean session
true, //EndDevice
"willTopic", //WillTopic or 0 DO NOT USE 0 STRING!
"willMessage" //WillMessage or 0 DO NOT USE 0 STRING!
"willTopic", //WillTopic or 0 DO NOT USE NULL STRING!
"willMessage" //WillMessage or 0 DO NOT USE NULL STRING!
}
};

#endif

#ifdef NETWORK_UDP
UDP_APP_CONFIG = {
{
"225.1.1.1", // Multicast group IP
1883, // Multicast group Port
{0,0,0,0,0,0}, // mac address (for Arduino App)
"192.168.11.18" // Local IP (for Arduino App)
},
{
"Node01", //ClientId
300, //KeepAlive
true, //Clean session
true, //EndDevice
"willTopic", //WillTopic or 0 DO NOT USE NULL STRING!
"willMessage" //WillMessage or 0 DO NOT USE NULL STRING!
}
};
#endif
/*------------------------------------------------------
* Create Topic
*------------------------------------------------------*/
Expand Down Expand Up @@ -86,15 +108,15 @@ int measure(){
soilR = 9999;
}
char buf[30];
GET_DATETIME(buf);
sprintf(buf + 17," %4d[Kohom]",soilR);
//GET_DATETIME(buf);
//sprintf(buf + 17," %4d[Kohom]",soilR);
return PUBLISH(tpMeasure,buf, 29,1);
}


/*--------------- List of task invoked by Timer ------------*/

TASK_LIST = { //{ MQString* topic, time duration in second},
TASK_LIST = { //{ MQString* topic, executing duration in second},
{measure, 40},
END_OF_TASK_LIST};

Expand Down
7 changes: 4 additions & 3 deletions Client/src/lib/MQTTSN_Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* Debug Flag
======================================*/
#define NW_DEBUG
#define MQTTSN_DEBUG
//#define MQTTSN_DEBUG


/****************************************
Expand Down Expand Up @@ -120,9 +120,10 @@ typedef struct {
}XBeeAppConfig;

typedef struct {
char* ipAddress;
uint8_t ipAddress[4];
uint16_t portNo;
//uint16_t cPortNo;
uint8_t ipLocal[4];
uint8_t macAddr[6];
}UdpConfig;

typedef struct {
Expand Down
6 changes: 3 additions & 3 deletions Client/src/lib/util.cpp → Client/src/lib/mqUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* util.cpp
* mqUtil.cpp
*
* The BSD License
*
Expand Down Expand Up @@ -35,10 +35,10 @@

#ifndef ARDUINO
#include "MQTTSN_Application.h"
#include "util.h"
#include "mqUtil.h"
#else
#include <MQTTSN_Application.h>
#include <util.h>
#include <mqUtil.h>
#endif /* ARDUINO */

#ifdef MBED
Expand Down
2 changes: 1 addition & 1 deletion Client/src/lib/util.h → Client/src/lib/mqUtil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* util.h
* mqUtil.h
*
* The BSD License
*
Expand Down
13 changes: 7 additions & 6 deletions Client/src/lib/mqttsnClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@
*/

#ifdef ARDUINO
#if defined(MQTTSN_DEBUG) || defined(ZBEE_DEBUG)
#include <SoftwareSerial.h>
extern SoftwareSerial debug;
#endif

#endif /* ARDUINO */

#ifdef ARDUINO
#include <MQTTSN_Application.h>
#include <util.h>
#include <mqUtil.h>
#include <mqttsnClient.h>
#if defined(MQTTSN_DEBUG) || defined(NW_DEBUG)
#include <SoftwareSerial.h>
extern SoftwareSerial debug;
#endif
#else
#include "MQTTSN_Application.h"
#include "util.h"
#include "mqUtil.h"
#include "mqttsnClient.h"
#endif

Expand Down
6 changes: 3 additions & 3 deletions Client/src/lib/mqttsnClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@
#include <MQTTSN_Application.h>
#include <Network.h>
#include <mqttsn.h>
#include <util.h>
#include <mqUtil.h>
#else
#if defined(ARDUINO) && ARDUINO < 100
#include "WProgram.h"
#include <inttypes.h>
#include <mqttsn.h>
#include <Network.h>
#include <util.h>
#include <mqUtil.h>
#else
#ifdef LINUX
#include <sys/time.h>
#endif
#include <iostream>
#include "MQTTSN_Application.h"
#include "Network.h"
#include "util.h"
#include "mqUtil.h"
#include "mqttsn.h"
#endif
#endif
Expand Down
4 changes: 2 additions & 2 deletions Client/src/lib/mqttsnClientAppFw4Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ int MqttsnClientApplication::disconnect(uint16_t duration){
uint32_t MqttsnClientApplication::getUnixTime(){
return XTimer::getUnixTime();
}

/*
#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
Expand Down Expand Up @@ -397,7 +397,7 @@ void MqttsnClientApplication::getDateTime(char* buf) {
}
}
}

*/

/*-------------- Indicator --------------*/
void MqttsnClientApplication::indicatorOn(){
Expand Down
7 changes: 5 additions & 2 deletions Client/src/lib/mqttsnClientAppFw4Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@

#include <MQTTSN_Application.h>
#include <mqttsnClient.h>
#include <util.h>
#include <mqUtil.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>

#define MQ_LED_PIN 13
#define MQ_INT0_PIN 2
Expand Down Expand Up @@ -76,7 +79,7 @@ enum MQ_INT_STATUS{ WAIT, INT0_LL, INT0_WAIT_HL, INT_WDT};
#define INDICATOR_OFF() theApplication->indicatorOff()
#define BLINK_INDICATOR(...) theApplication->blincIndicator(__VA_ARGS__)
#define GETUTC() theApplication->getUnixTime()
#define GET_DATETIME(...) theApplication->getDateTime(__VA_ARGS__)
//#define GET_DATETIME(...) theApplication->getDateTime(__VA_ARGS__)


extern void setUint32(uint8_t*, uint32_t);
Expand Down
8 changes: 6 additions & 2 deletions Client/src/lib/mqttsnClientAppFw4Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,13 @@ void MqttsnClientApplication::initialize(int argc, char** argv){
theAppConfig.netCfg.device = strdup(dev);
#endif
#ifdef NETWORK_UDP
if(portNo || ipAddr){
if(portNo || ipAddr[0]){
theAppConfig.netCfg.portNo = portNo;
theAppConfig.netCfg.ipAddress = ipAddr;
uint32_t ipaddr = inet_addr(ipAddr);
theAppConfig.netCfg.ipAddress[0] = (ipaddr & 0xff000000) >> 24;
theAppConfig.netCfg.ipAddress[1] = (ipaddr & 0x00ff0000) >> 16;
theAppConfig.netCfg.ipAddress[2] = (ipaddr & 0x0000ff00) >> 8;
theAppConfig.netCfg.ipAddress[3] = (ipaddr & 0x000000ff);
}else{
printf("argument error\n");
exit(1);
Expand Down
2 changes: 1 addition & 1 deletion Client/src/lib/mqttsnClientAppFw4Linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#include "MQTTSN_Application.h"
#include "mqttsnClient.h"
#include "util.h"
#include "mqUtil.h"


#define PUBLISH(...) theApplication->publish(__VA_ARGS__)
Expand Down
Loading

0 comments on commit 2dc61c4

Please sign in to comment.