void callback(const MQTT::Publish& pub) { if (pub.topic().endsWith("request")) { String sRequest = pub.payload_string(); StaticJsonBuffer<1000> request; JsonObject& req = request.parseObject(sRequest); StaticJsonBuffer<1000> reply; JsonObject& repl = reply.createObject(); handle(repl, req); if (!req.success()) { LOGF(" UDP message JSON parsing fails"); client.publish("system/log", " UDP message JSON parsing fails"); return; } else { handle(repl, req); String str; repl.printTo(str); client.publish(prefix + "bootloader/reply", str); } } else { LOGF(" unknown request %s", pub.payload_string().c_str()); } }
void callback(const MQTT::Publish& pub) { yield(); if (millis() - MQTTtick > MQTTlimit) { MQTTtick = millis(); int commandLoc; String command = ""; String deviceName = ""; String endPoint = ""; topic = pub.topic(); payload = pub.payload_string(); // -- topic parser // syntax: // global: / global / path / command / function // device setup: / deviceInfo / command / name // normal: path / command / name / endpoint // check item 1 in getValue String firstItem = getValue(topic, '/', 1); if (firstItem == "global") { // -- do nothing until I change to $ prefix before command types } else if (firstItem == "deviceInfo") { // get name and command deviceName = getValue(topic, '/', 3); command = getValue(topic, '/', 2); if ((deviceName == thisDeviceName) && (command == "control")) { if (payload == "no states") { // -- do something to send the default states, but now that this is managed by persistence this shouldn't be necessary // -- maybe it just resets all the states to 0 or whatever was originally programmed into the sketch //sendJSON = true; } else if (payload == "blink on") { Serial.end(); pinMode(BUILTIN_LED, OUTPUT); ticker.attach(0.6, tick_fnc); } else if (payload == "blink off") { ticker.detach(); digitalWrite(BUILTIN_LED, HIGH); pinMode(BUILTIN_LED, INPUT); Serial.begin(115200); } else { // -- persistence will no longer send the default object, if it's an arduino based esp chip, it will just send the control messages to /[device_path]/control/[device_name]/[endpoint_key] } } } else { int i; int maxitems; // count number of items for (i=1; i<topic.length(); i++) { String chunk = getValue(topic, '/', i); if (chunk == NULL) { break; } } // get topic variables maxitems = i; for (i=1; i<maxitems; i++) { String chunk = getValue(topic, '/', i); if (chunk == "control") { commandLoc = i; command = chunk; deviceName = getValue(topic, '/', i + 1); endPoint = getValue(topic, '/', i + 2); break; } } //Serial.println("device and endpoint incoming..."); //Serial.println(deviceName); //Serial.println(endPoint); // send endpoint_key to function stored in namepins.h at compile time // function returns static_endpoint_id associated with that endpoint String lookup_val = lookup(endPoint); //Serial.println("looking value incoming..."); //Serial.println(lookup_val); // sketch acts on that value as it normally would, using the static_endpoint_id to know for sure what it should do (turn output pin on/off, adjust RGB light, etc) if (lookup_val == "RGB") { // deserialize payload, get valueKey // or just look for value or red,green,blue String findKey = getValue(payload, '"', 1); String findValue = getValue(payload, ':', 1); findValue.remove(findValue.length() - 1); if (findKey == "red") { redValue = findValue.toInt(); } else if (findKey == "green") { greenValue = findValue.toInt(); } else if (findKey == "blue") { blueValue = findValue.toInt(); } //neoPixelChange = true; } /* else if (lookup_val == "SECOND STATIC ENDPOINT ID") { } else if (lookup_val == "THIRD STATIC ENDPOINT ID") { } */ // sketch confirms the value by sending it back on /[path]/[confirm]/[device_name]/[endpoint_key] confirmPath = ""; confirmPath = thisDevicePath; confirmPath += "/confirm/"; confirmPath += thisDeviceName; confirmPath += "/"; confirmPath += endPoint; confirmPayload = payload; //sendConfirm = true; client.publish(MQTT::Publish(confirmPath, confirmPayload).set_qos(2)); } } }