Last active
April 24, 2017 09:56
-
-
Save jnsdbr/a997deb3dfd22843123e4f1d5b75f089 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "..."; | |
const char* password = "..."; | |
const char* mqtt_server = "..."; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
void setup() { | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void setup_wifi() | |
{ | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
} | |
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(); | |
} | |
void reconnect() | |
{ | |
// Loop until we're reconnected | |
while (!client.connected()) | |
{ | |
// Attempt to connect | |
if (client.connect("c1")) | |
{ | |
client.publish("controllers/c1", "online"); | |
client.subscribe("controllers/c1/commands"); | |
} | |
else | |
{ | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void loop() | |
{ | |
if (!client.connected()) | |
{ | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 10) | |
{ | |
lastMsg = now; | |
client.publish("controllers/c1", "1"); | |
} | |
} |
This file contains hidden or 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
import mqtt.*; | |
MQTTClient client; | |
int timer = 0; | |
PFont font; | |
void setup() | |
{ | |
size(400, 400); | |
client = new MQTTClient(this); | |
client.connect("mqtt://localhost", "mqtt-delay-test"); | |
client.subscribe("controllers/c1"); | |
client.subscribe("controllers/c2"); | |
font = createFont("Courier", 20); | |
textFont(font); | |
} | |
void draw() | |
{ | |
background(200); | |
noStroke(); | |
} | |
void messageReceived(String topic, byte[] payload) | |
{ | |
String incomingMessage = new String(payload); | |
//println("[" + topic + "] " + incomingMessage); | |
if (topic.equals("controllers/c1")) | |
{ | |
int value = Integer.parseInt(incomingMessage); | |
if (value == 1) | |
{ | |
fill(0); | |
text(millis() - timer, width / 2 - 100, 100); | |
ellipse(width / 2 - 100, height / 2, millis() - timer, millis() - timer); | |
timer = millis(); | |
} | |
} | |
if (topic.equals("controllers/c2")) | |
{ | |
int value = Integer.parseInt(incomingMessage); | |
if (value == 1) | |
{ | |
fill(0); | |
text(millis() - timer, width / 2 + 100, 100); | |
ellipse(width / 2 + 100, height / 2, millis() - timer, millis() - timer); | |
timer = millis(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment