void OnGUI() { string sendCmd = null; if (GUI.Button(new Rect(20, 40, 80, 20), "Send ON")) { sendCmd = "ON"; Debug.Log("Button ON pressed"); } if (GUI.Button(new Rect(20, 70, 80, 20), "Send OFF")) { sendCmd = "OFF"; Debug.Log("Button OFF pressed"); } if (sendCmd != null) { // SonoffIn lampIn = new SonoffIn(); // lampIn.cmd = sendCmd; // Debug.Log("sending cmd " + lampIn.cmd + "..."); // string jsonMsg = JsonUtility.ToJson(lampIn); // write the new state on the "in" topic //client.Publish("sonoff17/in", System.Text.Encoding.UTF8.GetBytes(jsonMsg), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true); // DEBUG FIXME just for debug, send to in topic to close the loop SonoffFbk lampOut = new SonoffFbk(); lampOut.state = sendCmd; Debug.Log("sending cmd " + lampOut.state + "..."); string jsonMsg = JsonUtility.ToJson(lampOut); client.Publish("sonoff17/out", System.Text.Encoding.UTF8.GetBytes(jsonMsg), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true); Debug.Log("sent"); } }
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { SonoffFbk lampOut = JsonUtility.FromJson <SonoffFbk>(System.Text.Encoding.UTF8.GetString(e.Message)); Debug.Log("Feedback lamp state: " + lampOut.state); lampEnableFeedbackState = string.Compare(lampOut.state, "ON") == 0; refreshState = true; }