4
- connects to an MQTT server
5
- publishes "hello world" to the topic "outTopic"
6
- subscribes to the topic "inTopic"
11
#include <PubSubClient.h>
14
// Update these with values suitable for your network.
15
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
16
byte server[] = { 192, 168, 2, 4 };
17
byte ip[] = { 192, 168, 2, 5 };
19
void callback(char* topic, byte* payload,int length) {
20
//This is called if a message arrives on a topic we're subscribed too
21
Serial.println(topic);
22
char message[length-1];
24
// Sort out the payload so we don't get memory over run stuffs
25
for (i=0; i<=length-1; i++){
26
message[i] = (char) payload[i];
28
//now find out which channel we're acting on :)
29
if (strcmp(topic,(char*)"home/homeeasy/channel/0\0") == 0){
30
Serial.println(message);
34
PubSubClient client(server, 1883, callback);
39
Ethernet.begin(mac, ip);
40
//connect and leave a will message to turn things off if we die (for some reason)
41
if (client.connect("lightTempHE","home/status/lightTempHE",0,0,"Off")) {
42
client.publish("home/status/lightTempHE","On");
43
client.subscribe("home/homeeasy/#");
53
ftoa(value2,getTemp(0),2);
54
client.publish("outTopic", value2);
55
curLight = getLight(5);
56
itoa(curLight,light,10);
57
if (curLight != prevLight){
58
client.publish("lightTopic", light);
61
//Serial.println(getLight(5));
65
int getLight(int pin) {
66
return (analogRead(pin));
69
float getTemp(int pin){
71
return ((((analogRead(pin) * (5000.0/1024.0)-500.0))/10.0)-4); //converting from a 0 to 1023 digital range
72
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
75
char *ftoa(char *a, double f, int precision)
77
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
80
long heiltal = (long)f;
82
while (*a != '\0') a++;
84
long desimal = abs((long)((f - heiltal) * p[precision]));