Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trentsteenholdt-readify/6202058354e52db56c2c5532c6587faa to your computer and use it in GitHub Desktop.
Save trentsteenholdt-readify/6202058354e52db56c2c5532c6587faa to your computer and use it in GitHub Desktop.
AzureIoTLoop
void publishAzuredata(char* event, unsigned int value){
client.publish(event, value);
}
void subscibeAzuredata(char* event){
client.subscribe(iothub_subscribe_endpoint);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(iothub_deviceid, iothub_user, iothub_sas_token)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
if (String(topic) == "device/deviceID/gate") {
if (value == "1") {
digitalWrite(gatePin, HIGH);
delay(2000);
client.publish(topic, 0);
digitalWrite(gatePin, LOW);
}
else if (value == "0") {
//do nothing really
//digitalWrite(gatePin, LOW);
}
}
}
void setup(){
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
subscibeAzuredata("device/deviceID/gate");
subscibeAzuredata("device/deviceID/garage");
connect_mqtt();
}
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment