~daubers/+junk/homeeasy-mqtt

« back to all changes in this revision

Viewing changes to mqtt_basic.pde

  • Committer: Matt Daubney
  • Date: 2011-09-06 19:51:29 UTC
  • Revision ID: matt@daubers.co.uk-20110906195129-hkv8fdlw1dbvehbv
* Started work on the mqtt port

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Basic MQTT example 
 
3
 
 
4
  - connects to an MQTT server
 
5
  - publishes "hello world" to the topic "outTopic"
 
6
  - subscribes to the topic "inTopic"
 
7
*/
 
8
 
 
9
#include <SPI.h>
 
10
#include <Ethernet.h>
 
11
#include <PubSubClient.h>
 
12
#include "HomeEasy.h"
 
13
 
 
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 };
 
18
 
 
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];
 
23
  int i;
 
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];
 
27
  }
 
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);
 
31
  }
 
32
}
 
33
 
 
34
PubSubClient client(server, 1883, callback);
 
35
 
 
36
void setup()
 
37
{
 
38
  Serial.begin(9600);
 
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/#");
 
44
  }
 
45
}
 
46
 
 
47
char value2[5];
 
48
char light[5];
 
49
int curLight = 0;
 
50
int prevLight = 0; 
 
51
void loop()
 
52
{
 
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);
 
59
    prevLight = curLight;
 
60
  } 
 
61
  //Serial.println(getLight(5));
 
62
  client.loop();
 
63
}
 
64
 
 
65
int getLight(int pin) {
 
66
  return (analogRead(pin));
 
67
}
 
68
 
 
69
float getTemp(int pin){
 
70
 
 
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
 
73
}
 
74
 
 
75
char *ftoa(char *a, double f, int precision)
 
76
{
 
77
  long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
 
78
  
 
79
  char *ret = a;
 
80
  long heiltal = (long)f;
 
81
  itoa(heiltal, a, 10);
 
82
  while (*a != '\0') a++;
 
83
  *a++ = '.';
 
84
  long desimal = abs((long)((f - heiltal) * p[precision]));
 
85
  itoa(desimal, a, 10);
 
86
  return ret;
 
87
}
 
88