Вы находитесь на странице: 1из 5

#include <SPI.

h>
#include <PubSubClient.h>
#include <Ethernet.h>
/*
* LightSensorMqttDemo
*
* A simple m2m.io platform demo for Arduino.
*/
#define MQTT_SERVER "q.m2m.io"
// MAC Address of Arduino Ethernet Sheild (on sticker on shield)
byte MAC_ADDRESS[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x31, 0xB8 };
PubSubClient client;
// Pin 9 is the LED output pin
int ledPin = 9;
// Analog 0 is the input pin
int lightPinIn = 0;
// defines and variable for sensor/control mode
#define MODE_OFF
0 // not sensing light, LED off
#define MODE_ON
1 // not sensing light, LED on
#define MODE_SENSE 2 // sensing light, LED controlled by software
int senseMode = 0;
unsigned long time;
char message_buff[100];
void setup()
{
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
// init serial link for debugging
Serial.begin(9600);
if (Ethernet.begin(MAC_ADDRESS) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
client = PubSubClient(MQTT_SERVER, 1883, callback);
}
void loop()
{
if (!client.connected())
{
// clientID, username, MD5 encoded password
client.connect("arduino-mqtt", "john@m2m.io", "00000000000000000000000000000");
client.publish("io.m2m/arduino/lightsensor", "I'm alive!");
client.subscribe("io.m2m/arduino/lightsensor");
}
switch (senseMode) {
case MODE_OFF:
// light should be off
digitalWrite(ledPin, LOW);
break;
case MODE_ON:
// light should be on
digitalWrite(ledPin, HIGH);
break;

case MODE_SENSE:
// light is adaptive to light sensor
// read from light sensor (photocell)
int lightRead = analogRead(lightPinIn);
//
//
//
//
if

if there is light in the room, turn off LED


else, if it is "dark", turn it on
scale of light in this circit is roughly 0 - 900
500 is a "magic number" for "dark"
(lightRead > 500) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
// publish light reading every 5 seconds
if (millis() > (time + 5000)) {
time = millis();
String pubString = "{\"report\":{\"light\": \"" + String(lightRead) + "\"}}";
pubString.toCharArray(message_buff, pubString.length()+1);
//Serial.println(pubString);
client.publish("io.m2m/arduino/lightsensor", message_buff);
}

// MQTT client loop processing


client.loop();

// handles message arrived on subscribed topic(s)


void callback(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("{\"command\":{\"lightmode\": \"OFF\"}}")) {
senseMode = MODE_OFF;
} else if (msgString.equals("{\"command\":{\"lightmode\": \"ON\"}}")) {
senseMode = MODE_ON;
} else if (msgString.equals("{\"command\":{\"lightmode\": \"SENSE\"}}")) {
senseMode = MODE_SENSE;
}
}

/*
Example of using a Stream object to store the message payload
Uses SRAM library: https://github.com/ennui2342/arduino-sram
but could use any Stream based class such as SD
- connects to an MQTT server
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include
#include
#include
#include

<SPI.h>
<Ethernet.h>
<PubSubClient.h>
<SRAM.h>

// Update these with values suitable for your network.


byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);
SRAM sram(4, SRAM_1024);
void callback(char* topic, byte* payload, unsigned int length) {
sram.seek(1);
// do something with the message
for(uint8_t i=0; i<length; i++) {
Serial.write(sram.read());
}
Serial.println();
// Reset position for the next message to be stored
sram.seek(1);
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient, sram);
void setup()
{
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
sram.begin();
sram.seek(1);
Serial.begin(9600);
}
void loop()
{
client.loop();
}

/*
Basic MQTT example
This sketch demonstrates the basic
capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic
"outTopic"
- subscribes to the topic "inTopic", printing
out any messages
it receives. NB - it assumes the received
payloads are strings not binary

It will reconnect to the server if the


connection is lost using a blocking
reconnect function. See the
'mqtt_reconnect_nonblocking' example for
how to
achieve the same result without blocking
the main loop.
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT
connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an
announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup()
{
Serial.begin(57600);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}

/*
Publishing in the callback
- connects to an MQTT server
- subscribes to the topic "inTopic"
- when a message is received, republishes it to "outTopic"
This example shows how to publish messages within the
callback function. The callback function header needs to
be declared before the PubSubClient constructor and the
actual callback defined afterwards.
This ensures the client reference in the callback function
is valid.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
// In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet.
// Allocate the correct amount of memory for the payload copy
byte* p = (byte*)malloc(length);
// Copy the payload to the new buffer
memcpy(p,payload,length);
client.publish("outTopic", p, length);
// Free the memory
free(p);
}
void setup()
{
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
void loop()
{
client.loop();
}

Вам также может понравиться