Created
October 2, 2013 08:30
-
-
Save justx1/6790670 to your computer and use it in GitHub Desktop.
Publishing Arduino Sensor Data through MQTT over Ethernet
http://e.verything.co/post/61576413925/publishing-arduino-sensor-data-through-mqtt-over
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
/* | |
* Temperature Reading | |
* | |
* Reads sensor value and publishes through MQTT. | |
* MQTT Arduino PubSubClient http://knolleary.net/arduino-client-for-mqtt/ | |
* | |
*/ | |
#include <SPI.h> | |
#include <PubSubClient.h> | |
#include <Ethernet.h> | |
// Pins | |
const int tempPinIn = 0; // Analog 0 is the input pin | |
// Variables | |
char* tempC; | |
unsigned long time; | |
char message_buffer[100]; | |
// Network Settings | |
// MAC address of ethernet shield | |
// Look for it on a sticket at the bottom of the shield. | |
// Old Arduino Ethernet Shields or clones may not have a dedicated MAC address. Set any hex values here. | |
byte MAC_ADDRESS[] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF }; | |
// IP address of MQTT server | |
byte MQTT_SERVER[] = { 192, 168, 1, 115 }; | |
EthernetClient ethClient; | |
PubSubClient client(MQTT_SERVER, 1883, callback, ethClient); | |
void setup() | |
{ | |
// Initilize serial link for debugging | |
Serial.begin(9600); | |
if (Ethernet.begin(MAC_ADDRESS) == 0) | |
{ | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
return; | |
} | |
} | |
void loop() | |
{ | |
if (!client.connected()) | |
{ | |
//client.connect("clientID", "mqtt_username", "mqtt_password"); | |
client.connect("sfo-arduino"); | |
client.publish("sfo/arduino/alive", "I'm alive!"); | |
} | |
tempC = dtostrf(((((analogRead(tempPinIn) * 5.0) / 1024) - 0.5) * 100), 5, 2, message_buffer); // TMP36 sensor calibration | |
// Publish sensor reading every X milliseconds | |
if (millis() > (time + 60000)) { | |
time = millis(); | |
client.publish("arduino/temperature",tempC); | |
} | |
// MQTT client loop processing | |
client.loop(); | |
} | |
// Handles messages arrived on subscribed topic(s) | |
void callback(char* topic, byte* payload, unsigned int length) { | |
} |
Do you have any idea why the analogread function is causing my MQTT client to disconnect all the time ?
Olivia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you Just what I was looking for!!