Skip to content

Commit b08e168

Browse files
committed
Create MQTTGateway.ino
MQTTGateway Beta 0.1
1 parent 8e53f75 commit b08e168

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

MQTTGateway/MQTTGateway.ino

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// MyMQTT Gateway 0.1b
2+
//
3+
// Created by Daniel Wiegert <[email protected]>
4+
// Based on Mysensors Ethernet Gateway by Henrik Ekblad <[email protected]>
5+
// http://www.mysensors.org
6+
//
7+
// Requires MySensors lib 1.4b
8+
//
9+
// Don't forget to look at the definitions in MyMQTT.h!
10+
// Don't forget to configure Radio pins, IP and MAC-address!
11+
//
12+
// Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light
13+
// NodeID and SensorID is Decimal number.
14+
// Last segment is translation of the sensor type, look inside MyMQTT.cpp for
15+
// the definitions. User can change this to their needs.
16+
//
17+
// Features:
18+
// Supports automatic nodeID delegation
19+
// Recieve sketchname and version Example: (openhab item)
20+
//String sketch20 "Node 20 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"}
21+
//String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"}
22+
// ...
23+
//
24+
// Todo:
25+
// DOCUMENTATION...
26+
// Special commands : clear or set EEPROM Values
27+
// Special commands : Reboot
28+
// ...
29+
30+
31+
#include <SPI.h>
32+
#include <MyMQTT.h>
33+
#include <Ethernet.h>
34+
35+
36+
// Use this for IBOARD modded to use standard MISO/MOSI/SCK More information see;
37+
// http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5
38+
#define RADIO_CE_PIN 3 // radio chip enable
39+
#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
40+
41+
//Use this for default configured pro mini / nano etc :
42+
//#define RADIO_CE_PIN 5 // radio chip enable
43+
//#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
44+
45+
#define IP_PORT 1883 // MQTT Listening port
46+
IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here
47+
byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change this!
48+
// * NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Locally Administered Address Ranges.
49+
50+
EthernetServer server = EthernetServer(IP_PORT);
51+
MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
52+
53+
void processEthernetMessages() {
54+
char inputString[MQTT_MAX_PACKET_SIZE] = "";
55+
int inputSize = 0;
56+
EthernetClient client = server.available();
57+
if (client) {
58+
while (client.available()) {
59+
char inChar = client.read();
60+
inputString[inputSize] = inChar;
61+
inputSize++;
62+
}
63+
gw.processMQTTMessage(inputString, inputSize);
64+
}
65+
}
66+
67+
void writeEthernet(char *writeBuffer, int *writeSize) {
68+
server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
69+
}
70+
71+
int main(void) {
72+
init();
73+
Ethernet.begin(mac, myIp);
74+
delay(1000); // Wait for Ethernet to get configured.
75+
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
76+
server.begin();
77+
while (1) {
78+
processEthernetMessages();
79+
gw.processRadioMessage();
80+
}
81+
}
82+
83+

0 commit comments

Comments
 (0)