hey guys n gals new here been watching mk smarthouse awhile now, im having trouble working out the coding for my arduino,
i have succesfully had it running with openhab and the 16 channel relay board but i dont think its right and the more i read the more i confuse myself, i run openhab on a pc but i have mqtt succesfully running i think my major problem is the arduino sketch (i did not write this code i cannot remember where i got it but credit to them) and that i cant work out how to create the items and things related to the sketch (it doesnt specify each pin) since i had it working i kind of gave up and settled with it, however now i would like to start fresh and get it working correctly so i can setup things and items etc and be able to do rules and expand.
i will attach the sketch and items file (i dont have a things file or sitemap setup) if anyone could help or even write or correct my sketch it would be amazing, i seem to learn this stuff easier by seeing working code and then editing to see its effects.
**Arduino Mega & ethernet & 16 relay**
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define MQTT_CLIENTID "lightSwitchArduino"
#define MQTT_SERVER "192.168.1.103"
#define MQTT_WILLTOPIC "clients/LightSwitcharduino/"
#define MQTT_WILLMESSAGE "dead"
#define TRUE (1)
// Update these with values suitable for your network.
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x07 };
//byte server[] = { 192, 168, 1, 103 };
IPAddress ip(192, 168, 1, 50);
IPAddress server(192, 168, 1, 103);
const int StatLED = 13;
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void callback(char* topic, byte* payload, unsigned int length)
{
char buff[length + 1];
int n;
for (n = 0; n < length; n++)
{
buff[n] = payload[n];
}
buff[n] = '\0';
if (!strcmp(topic, "arduino/lightControl"))
{
if(length==4) // only accepts message length of 4 (plus null): 1&2) two-digit-pin-number 3) underscore 4) one-digit-command-number
{
int number = buff[3] - '0'; // command number at byte 3 of message buffer, convert from ascii code for the number to integer
int channel = (buff[0] - '0') * 10 ; // 10's digit of pin number at byte 0 of message buffer, convert from ascii character code to integer
channel = channel + (buff[1] - '0') ; // add 1's digit of pin number from byte 1 of message buffer, convert from ascii to integer
Serial.println(number);
Serial.println(channel);
char feedbackTopic[256];
sprintf(feedbackTopic, "arduino/lightControl/%i",channel);
pinMode(channel, OUTPUT); // output
digitalWrite(channel, number);
if(number==0)
{
client.publish(feedbackTopic, "ON");
}
else
{
client.publish(feedbackTopic, "OFF");
}
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(StatLED, OUTPUT); // LED
pinMode(38, OUTPUT);
digitalWrite(StatLED, LOW);
digitalWrite(38, HIGH);
char willtopic[128] = MQTT_WILLTOPIC;
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
if (client.connect(MQTT_CLIENTID, willtopic, MQTTQOS2, 1, MQTT_WILLMESSAGE))
{
//client.publish(willtopic, NULL, 0, TRUE);
client.subscribe("arduino/lightControl");
digitalWrite(StatLED, HIGH);
}
}
void loop()
{
if(!client.connected())
{
char willtopic[128] = MQTT_WILLTOPIC;
client.connect(MQTT_CLIENTID, willtopic, MQTTQOS2, 1, MQTT_WILLMESSAGE);
//client.publish(willtopic, NULL, 0, TRUE);
client.subscribe("arduino/lightControl");
digitalWrite(StatLED, HIGH);
}
client.loop();
}
**Items**
Switch Light_PIN23 "Turn On Pin 23 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:23_0],>[mosquitto:arduino/lightControl:command:OFF:23_1],<[mosquitto:arduino/lightControl/23:state:default]", autoupdate="false" }
Switch Light_PIN24 "Turn On Pin 24 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:24_0],>[mosquitto:arduino/lightControl:command:OFF:24_1],<[mosquitto:arduino/lightControl/24:state:default]", autoupdate="false" }
Switch Light_PIN25 "Turn On Pin 25 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:25_0],>[mosquitto:arduino/lightControl:command:OFF:25_1],<[mosquitto:arduino/lightControl/25:state:default]", autoupdate="false" }
Switch Light_PIN26 "Turn On Pin 26 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:26_0],>[mosquitto:arduino/lightControl:command:OFF:26_1],<[mosquitto:arduino/lightControl/26:state:default]", autoupdate="false" }
Switch Light_PIN27 "Turn On Pin 27 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:27_0],>[mosquitto:arduino/lightControl:command:OFF:27_1],<[mosquitto:arduino/lightControl/27:state:default]", autoupdate="false" }
Switch Light_PIN28 "Turn On Pin 28 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:28_0],>[mosquitto:arduino/lightControl:command:OFF:28_1],<[mosquitto:arduino/lightControl/28:state:default]", autoupdate="false" }
Switch Light_PIN29 "Turn On Pin 29 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:29_0],>[mosquitto:arduino/lightControl:command:OFF:29_1],<[mosquitto:arduino/lightControl/29:state:default]", autoupdate="false" }
Switch Light_PIN30 "Turn On Pin 30 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:30_0],>[mosquitto:arduino/lightControl:command:OFF:30_1],<[mosquitto:arduino/lightControl/30:state:default]", autoupdate="false" }
Switch Light_PIN31 "Turn On Pin 31 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:31_0],>[mosquitto:arduino/lightControl:command:OFF:31_1],<[mosquitto:arduino/lightControl/31:state:default]", autoupdate="false" }
Switch Light_PIN32 "Turn On Pin 32 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:32_0],>[mosquitto:arduino/lightControl:command:OFF:32_1],<[mosquitto:arduino/lightControl/32:state:default]", autoupdate="false" }
Switch Light_PIN33 "Turn On Pin 33 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:33_0],>[mosquitto:arduino/lightControl:command:OFF:33_1],<[mosquitto:arduino/lightControl/33:state:default]", autoupdate="false" }
Switch Light_PIN34 "Turn On Pin 34 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:34_0],>[mosquitto:arduino/lightControl:command:OFF:34_1],<[mosquitto:arduino/lightControl/34:state:default]", autoupdate="false" }
Switch Light_PIN35 "Turn On Pin 35 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:35_0],>[mosquitto:arduino/lightControl:command:OFF:35_1],<[mosquitto:arduino/lightControl/35:state:default]", autoupdate="false" }
Switch Light_PIN36 "Turn On Pin 36 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:36_0],>[mosquitto:arduino/lightControl:command:OFF:36_1],<[mosquitto:arduino/lightControl/36:state:default]", autoupdate="false" }
Switch Light_PIN37 "Turn On Pin 37 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:37_0],>[mosquitto:arduino/lightControl:command:OFF:37_1],<[mosquitto:arduino/lightControl/37:state:default]", autoupdate="false" }
Switch Light_PIN38 "Turn On Pin 38 Relay" {mqtt=">[mosquitto:arduino/lightControl:command:ON:38_0],>[mosquitto:arduino/lightControl:command:OFF:38_1],<[mosquitto:arduino/lightControl/38:state:default]", autoupdate="false" }