Skip to content

Instantly share code, notes, and snippets.

@Elwell
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elwell/a15c88485e1e3b65ee33 to your computer and use it in GitHub Desktop.
Save Elwell/a15c88485e1e3b65ee33 to your computer and use it in GitHub Desktop.
testcase for pubsubclient retained
// Reed Switch to MQTT publisher for home
// Andrew Elwell, May 2015
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
byte mac[] = { 0x00, 0x04, 0xA3, 0x2C, 0x1D, 0xF1 };
byte server[] = { 10, 1, 1, 251 };
//void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, 0, ethClient);
uint8_t op[4] = { 0x6f, 0x70, 0x65, 0x6e }; // 'open'
uint8_t cl[6] = { 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x64 }; // 'closed'
//int led = 13; // BLINKY THING - cant use if ethernet active
int gate= 2; // Blue -> Green pair
int gateState = 0;
int gatelast = 0;
void setup() {
delay( 50 ); // allow some time (50 ms) after powerup and sketch start, for the Wiznet W5100 Reset IC to release and come out of reset.
if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
return;
}
// pinMode(led, OUTPUT);
pinMode(gate, INPUT); // set pin to input
digitalWrite(gate, HIGH); // turn on pullup resistors
//Connect to MQTT
if (!client.connected())
{
client.connect("Etherten01");
client.publish("etherten/status", "ONLINE");
}
}
// the loop routine runs over and over again forever:
void loop() {
gateState = digitalRead(gate);
if (gateState != gatelast) {
if (gateState == HIGH) {
// Trying to set retained, so need the following call
client.publish("etherten/gate", op, 4, 1);
// ^^^ not working, as expecting uint8_t rather than char as per the non-retained version below.
// client.publish("etherten/gate", "open");
} else {
//client.publish("etherten/gate", "closed");
client.publish("etherten/gate", cl, 6, 1);
}
}
gatelast = gateState ;
client.loop();
}
@Elwell
Copy link
Author

Elwell commented May 16, 2015

reedsw_etherten_test.ino: In function 'void loop()':
reedsw_etherten_test:50: error: invalid conversion from 'const char_' to 'uint8_t_'
reedsw_etherten_test:50: error: initializing argument 2 of 'boolean PubSubClient::publish(char_, uint8_t_, unsigned int, boolean)'
reedsw_etherten_test:55: error: invalid conversion from 'const char_' to 'uint8_t_'
reedsw_etherten_test:55: error: initializing argument 2 of 'boolean PubSubClient::publish(char_, uint8_t_, unsigned int, boolean)'

using the 1.0.5 IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment