|
|
Line 12: |
Line 12: |
| == NodeMCU script == | | == NodeMCU script == |
| todo | | todo |
− |
| |
− | == Arduino Custom Sketch ==
| |
− | In Domoticz we will first create a virtual hardware device. The we will create a virtual device. Note the IDX from this newly created device since we need to enter this into the sketch.
| |
− |
| |
− | For more information about Domoticz virtual hardware and devices, please consult the Domoticz website.
| |
− |
| |
− | <code>
| |
− | /* Pulscounter demo sketch for Domoticz
| |
− | * Gets data from Domotics using json api, increment the value and update Domoticz counter
| |
− | * Connect your device to GPIO2 pin.
| |
− | * Change the Wifi config and the Domoticz virtual devide IDX value (check Domoticz/Setup/Devices)
| |
− | * And set the IP and port for your Domoticz server
| |
− | */
| |
− |
| |
− | // ******************** User defined settings ****************************************
| |
− | #define WIFI_SSID "........" // Your local Wifi SSID
| |
− | #define WIFI_KEY "........" // Your local Wifi WPA key
| |
− | #define DOMOTICZ_IP "192.168.0.8" // Domoticz server address
| |
− | #define DOMOTICZ_PORT 8080 // Domoticz server portnumber
| |
− | #define DOMOTICZ_IDX 72 // IDX of Domoticz virtual device
| |
− | #define DOMOTICZ_SENDDELAY 5 // seconds between updates
| |
− | #define ESP_COUNTER_IO_PIN 2 // IO Pin used, default is GPIO2
| |
− | // ********************* End User defined settings ************************************
| |
− |
| |
− | // Do not change anything below...
| |
− | #include <ESP8266WiFi.h>
| |
− |
| |
− | // Pulse counter stuff
| |
− | unsigned long pulseCounter=0;
| |
− |
| |
− | const char* ssid = WIFI_SSID;
| |
− | const char* password = WIFI_KEY;
| |
− | const char* host = DOMOTICZ_IP;
| |
− | const int httpPort = DOMOTICZ_PORT;
| |
− |
| |
− | float value=0;
| |
− |
| |
− | void setup() {
| |
− | Serial.begin(115200);
| |
− | delay(10);
| |
− | Serial.print("Connecting to ");
| |
− | Serial.println(ssid);
| |
− |
| |
− | WiFi.begin(ssid, password);
| |
− |
| |
− | while (WiFi.status() != WL_CONNECTED) {
| |
− | delay(500);
| |
− | Serial.print(".");
| |
− | }
| |
− |
| |
− | Serial.println("");
| |
− | Serial.println("WiFi connected");
| |
− | Serial.println("IP address: ");
| |
− | Serial.println(WiFi.localIP());
| |
− |
| |
− | attachInterrupt(ESP_COUNTER_IO_PIN,ISR,FALLING);
| |
− |
| |
− | }
| |
− |
| |
− | void loop() {
| |
− | if (getdata(&value))
| |
− | {
| |
− | Serial.print("Current Value:");
| |
− | Serial.println(value);
| |
− | Serial.print("Delta Value:");
| |
− | Serial.println(pulseCounter);
| |
− | value=(value + pulseCounter)*100;
| |
− | pulseCounter=0;
| |
− | Serial.print("New Value:");
| |
− | Serial.println(value);
| |
− | senddata(value);
| |
− | }
| |
− | delay(DOMOTICZ_SENDDELAY * 1000);
| |
− | }
| |
− |
| |
− | boolean getdata(float *data)
| |
− | {
| |
− | boolean success=false;
| |
− | Serial.print("connecting to ");
| |
− | Serial.println(host);
| |
− |
| |
− | // Use WiFiClient class to create TCP connections
| |
− | WiFiClient client;
| |
− | if (!client.connect(host, httpPort)) {
| |
− | Serial.println("connection failed");
| |
− | return false;
| |
− | }
| |
− |
| |
− | // We now create a URI for the request
| |
− | String url = "/json.htm?type=devices&rid=";
| |
− | url += DOMOTICZ_IDX;
| |
− |
| |
− | Serial.print("Requesting URL: ");
| |
− | Serial.println(url);
| |
− |
| |
− | // This will send the request to the server
| |
− | client.print(String("GET ") + url + " HTTP/1.1\r\n" +
| |
− | "Host: " + host + "\r\n" +
| |
− | "Connection: close\r\n\r\n");
| |
− |
| |
− | unsigned long timer=millis()+200;
| |
− | while(!client.available() && millis()<timer) {}
| |
− |
| |
− | // Read all the lines of the reply from server and print them to Serial
| |
− |
| |
− | while(client.available()){
| |
− | String line = client.readStringUntil('\n');
| |
− | if (line.substring(10,14) == "Data")
| |
− | {
| |
− | String strValue=line.substring(19);
| |
− | byte pos=strValue.indexOf(' ');
| |
− | strValue=strValue.substring(0,pos);
| |
− | strValue.trim();
| |
− | value = strValue.toFloat();
| |
− | *data=value;
| |
− | Serial.println("Succes!");
| |
− | success=true;
| |
− | }
| |
− | }
| |
− | Serial.println("closing connection");
| |
− | return success;
| |
− | }
| |
− |
| |
− | boolean senddata(float value)
| |
− | {
| |
− | boolean success=false;
| |
− |
| |
− | Serial.print("connecting to ");
| |
− | Serial.println(host);
| |
− |
| |
− | // Use WiFiClient class to create TCP connections
| |
− | WiFiClient client;
| |
− | if (!client.connect(host, httpPort)) {
| |
− | Serial.println("connection failed");
| |
− | return false;
| |
− | }
| |
− |
| |
− | // We now create a URI for the request
| |
− | String url = "/json.htm?type=command¶m=udevice&idx=";
| |
− | url += DOMOTICZ_IDX;
| |
− | url += "&svalue=";
| |
− | url += value;
| |
− |
| |
− | Serial.print("Requesting URL: ");
| |
− | Serial.println(url);
| |
− |
| |
− | // This will send the request to the server
| |
− | client.print(String("GET ") + url + " HTTP/1.1\r\n" +
| |
− | "Host: " + host + "\r\n" +
| |
− | "Connection: close\r\n\r\n");
| |
− |
| |
− | unsigned long timer=millis()+200;
| |
− | while(!client.available() && millis()<timer) {}
| |
− |
| |
− | // Read all the lines of the reply from server and print them to Serial
| |
− | while(client.available()){
| |
− | String line = client.readStringUntil('\n');
| |
− | if (line.substring(0,15) == "HTTP/1.0 200 OK")
| |
− | {
| |
− | Serial.println("Succes!");
| |
− | success=true;
| |
− | }
| |
− | }
| |
− | Serial.println("closing connection");
| |
− | return success;
| |
− | }
| |
− |
| |
− | void ISR()
| |
− | {
| |
− | pulseCounter++;
| |
− | }
| |
− | </code>
| |
| | | |
| == Arduino ESP Easy == | | == Arduino ESP Easy == |
− | Use the device tab on the ESP Easy webinterface and create a new task by editing one of the available tasks. Select "Pulse Counter" from the dropdown box and enter the IDX found in the Domoticz device page. Also select the GPIO pin that you have used to connect the pulse counting device. That should be all. | + | Use the device tab on the ESP Easy webinterface and create a new task by editing one of the available tasks. Select "Pulse Counter" from the dropdown box and enter the IDX found in the Domoticz device page. Also select the GPIO pin that you have used to connect the pulse counting device. With the current release, you need to REBOOT the ESP Easy to get the show running. (we will change this requirement in the next release). That should be all. |
| | | |
| [[File:Domoticz_PowerUsage.png]] | | [[File:Domoticz_PowerUsage.png]] |