Created
November 12, 2013 05:15
-
-
Save m2mIO-gister/7425842 to your computer and use it in GitHub Desktop.
Arduino Example - Rangefinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <PubSubClient.h> // from http://knolleary.net/arduino-client-for-mqtt/ | |
#include <Ethernet.h> | |
#include "Timer.h" | |
#define M2MIO_USERNAME "<username on 2lemetry platform>" | |
#define M2MIO_PASSWORD "<MD5 hash of password (32 characters)>" | |
#define M2MIO_DOMAIN "<domain on 2lemetry platform (found in developer portal)>" | |
#define M2MIO_STUFF "rangefinders" | |
#define M2MIO_DEVICE_ID "device1" | |
#define REPORTING_INTERVAL_MS 3000 | |
#define MQTT_SERVER "q.m2m.io" | |
// fill in with MAC address of your ethernet shield | |
byte MAC_ADDRESS[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
char message_buff[100]; | |
EthernetClient ethernetClient; | |
PubSubClient rangeClient(MQTT_SERVER, 1883, msgCallback, ethernetClient); | |
char clientID[50]; | |
char topic[50]; | |
char msg[50]; | |
int rangePin = A0; // Sensing voltage on analog pin 0 | |
int rawValue = 0; // sensor reading | |
int rangeValue = 0; // range value in inches | |
int ledPin = 8; | |
Timer t; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
// initialize ethernet library | |
if (Ethernet.begin(MAC_ADDRESS) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
return; | |
} | |
// MQTT Client Setup / Connection | |
String clientIDStr = M2MIO_DEVICE_ID; | |
clientIDStr.toCharArray(clientID, clientIDStr.length()+1); | |
Serial.println(clientIDStr); | |
rangeClient.connect(clientID, M2MIO_USERNAME, M2MIO_PASSWORD); | |
String topicStr = M2MIO_DOMAIN; | |
topicStr.concat("/"); | |
topicStr.concat(M2MIO_STUFF); | |
topicStr.concat("/"); | |
topicStr.concat(M2MIO_DEVICE_ID); | |
topicStr.toCharArray(topic, topicStr.length()+1); | |
Serial.println(topicStr); | |
rangeClient.subscribe("io.m2m/rangefinders/device1/cmd"); | |
// setup timer to take periodic readings | |
int tickEvent = t.every(REPORTING_INTERVAL_MS, publishRange); | |
} | |
void loop() { | |
t.update(); // timer processing | |
rangeClient.loop(); // required for client library background processing | |
} | |
void publishRange(){ | |
rawValue = analogRead(rangePin); | |
rangeValue = (int)((((float)rawValue)*4.9)/9.8); | |
Serial.println(rangeValue, DEC); | |
if (rangeClient.connected()) { | |
Serial.println("C"); | |
} else { | |
Serial.println("NC"); | |
Serial.println("Client Connecting..."); | |
rangeClient.connect(clientID, M2MIO_USERNAME, M2MIO_PASSWORD); | |
} | |
String rangeMsg = "{\"range\":"; | |
rangeMsg.concat(rangeValue); | |
rangeMsg.concat("}"); | |
Serial.println(rangeMsg); | |
rangeMsg.toCharArray(msg, rangeMsg.length()+1); | |
rangeClient.publish(topic, msg); | |
} | |
/* | |
* Message Received Callback | |
*/ | |
void msgCallback(char* topic, byte* payload, unsigned int length) { | |
int i = 0; | |
//Serial.println("Message arrived: topic: " + String(topic)); | |
//Serial.println("Length: " + String(length,DEC)); | |
// create character buffer with ending null terminator (string) | |
for(i=0; i<length; i++) { | |
message_buff[i] = payload[i]; | |
} | |
message_buff[i] = '\0'; | |
String msgString = String(message_buff); | |
//Serial.println("Payload: " + msgString); | |
if (msgString.equals("{\"led\":\"on\"}")) { | |
digitalWrite(ledPin, HIGH); | |
} else if (msgString.equals("{\"led\":\"off\"}")) { | |
digitalWrite(ledPin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment