|
@@ -0,0 +1,137 @@
|
|
|
|
|
+#include <ESP8266WiFi.h>
|
|
|
|
|
+#include <ArduinoMqttClient.h>
|
|
|
|
|
+#include <ArduinoJson.h>
|
|
|
|
|
+
|
|
|
|
|
+#include "secrets.h"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+WiFiClient wifiClient;
|
|
|
|
|
+MqttClient mqttClient(wifiClient);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+//pin 0 red led
|
|
|
|
|
+//pin 2 blue led
|
|
|
|
|
+//#include <secrets>;
|
|
|
|
|
+
|
|
|
|
|
+// constants won't change:
|
|
|
|
|
+const long interval = 10000; // interval at which to read voltage and report
|
|
|
|
|
+unsigned long previousMillis = 0; // will store last time LED was updated
|
|
|
|
|
+
|
|
|
|
|
+const int analogInPin = A0;
|
|
|
|
|
+int sensorValue = 0;
|
|
|
|
|
+
|
|
|
|
|
+ StaticJsonDocument<200> doc;
|
|
|
|
|
+ int charCount;
|
|
|
|
|
+ char json[200];
|
|
|
|
|
+ int myMessage;
|
|
|
|
|
+ int myPin;
|
|
|
|
|
+void setup()
|
|
|
|
|
+{
|
|
|
|
|
+ Serial.begin(115200);
|
|
|
|
|
+ Serial.println();
|
|
|
|
|
+
|
|
|
|
|
+ pinMode(0, OUTPUT);
|
|
|
|
|
+ digitalWrite(0, HIGH);
|
|
|
|
|
+
|
|
|
|
|
+ pinMode(4, OUTPUT);
|
|
|
|
|
+ digitalWrite(4, LOW);
|
|
|
|
|
+ pinMode(12, OUTPUT);
|
|
|
|
|
+ digitalWrite(12, LOW);
|
|
|
|
|
+ pinMode(14, OUTPUT);
|
|
|
|
|
+ digitalWrite(14, LOW);
|
|
|
|
|
+
|
|
|
|
|
+ WiFi.begin(SECRET_SSID, SECRET_PASS);
|
|
|
|
|
+
|
|
|
|
|
+ Serial.print("Connecting");
|
|
|
|
|
+ while (WiFi.status() != WL_CONNECTED)
|
|
|
|
|
+ {
|
|
|
|
|
+ delay(500);
|
|
|
|
|
+ Serial.print(".");
|
|
|
|
|
+ }
|
|
|
|
|
+ Serial.println();
|
|
|
|
|
+
|
|
|
|
|
+ Serial.print("Connected, IP address: ");
|
|
|
|
|
+ Serial.println(WiFi.localIP());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if (!mqttClient.connect("192.168.1.38", 1883)) {
|
|
|
|
|
+ Serial.print("MQTT connection failed! Error code = ");
|
|
|
|
|
+ Serial.println(mqttClient.connectError());
|
|
|
|
|
+
|
|
|
|
|
+ while (1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Serial.print("Connected to MQTT");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // subscribe to a topic
|
|
|
|
|
+ mqttClient.subscribe("test1");
|
|
|
|
|
+ mqttClient.subscribe("test2");
|
|
|
|
|
+ mqttClient.subscribe("test3");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void loop() {
|
|
|
|
|
+ int messageSize = mqttClient.parseMessage();
|
|
|
|
|
+ if (messageSize) {
|
|
|
|
|
+ // we received a message, print out the topic and contents
|
|
|
|
|
+ Serial.print("Received a message with topic '");
|
|
|
|
|
+ Serial.print(mqttClient.messageTopic());
|
|
|
|
|
+ Serial.print("', length ");
|
|
|
|
|
+ Serial.print(messageSize);
|
|
|
|
|
+ Serial.println(" bytes:");
|
|
|
|
|
+
|
|
|
|
|
+ myPin=0;
|
|
|
|
|
+ if(mqttClient.messageTopic()=="test1"){
|
|
|
|
|
+ myPin=4;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(mqttClient.messageTopic()=="test2"){
|
|
|
|
|
+ myPin=12;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(mqttClient.messageTopic()=="test3"){
|
|
|
|
|
+ myPin=14;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Serial.println(myPin);
|
|
|
|
|
+
|
|
|
|
|
+ // use the Stream interface to print the contents
|
|
|
|
|
+ charCount = 0;
|
|
|
|
|
+ while (mqttClient.available()) {
|
|
|
|
|
+ json[ charCount++ ] = (char)mqttClient.read();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Serial.println(json);
|
|
|
|
|
+
|
|
|
|
|
+ DeserializationError error = deserializeJson(doc, json);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if(doc["state"] == "ON"){
|
|
|
|
|
+ Serial.println("Tested True");
|
|
|
|
|
+ digitalWrite(myPin, HIGH);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Serial.println("Tested False");
|
|
|
|
|
+ digitalWrite(myPin, LOW);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ unsigned long currentMillis = millis();
|
|
|
|
|
+ if (currentMillis - previousMillis >= interval) {
|
|
|
|
|
+ previousMillis = currentMillis;
|
|
|
|
|
+ sensorValue = analogRead(analogInPin);
|
|
|
|
|
+ Serial.print("sensor = ");
|
|
|
|
|
+ Serial.print(sensorValue);
|
|
|
|
|
+ Serial.println("");
|
|
|
|
|
+
|
|
|
|
|
+ mqttClient.beginMessage("houseVoltage");
|
|
|
|
|
+ mqttClient.print(sensorValue);
|
|
|
|
|
+ mqttClient.endMessage();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|