Jeg kaller HS sitt JSON-API fra NodeMCU. Samme kode virker vel på Yun også?
Tror du må ha 2 virtuelle devicer - en for "10 liter" og en for summering. Yun vil da kalle "10-liter-devicen" og det trigger da et event som tar summering+=10.
Jeg bruker TLS på HomeSeer, så om du vil kjøre det mot port 80, så må du sikkert endre litt i koden (har ryddet vekk en del kode, men du skjønner nok prinsippet):
#include <WiFiClientSecure.h>
// Use WiFiClientSecure class to create TLS connection
// https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
WiFiClientSecure client;
// Replace with your network details
const char* ssid = "...";
const char* password = "....";
IPAddress ip(192, 168, 1, 100);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 1, 1);
IPAddress dns(192, 168, 1, 1);
const char* host = "homeseer.local";
const int httpsPort = 443;
// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, dns);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.macAddress());
// Printing the ESP IP address
Serial.println(WiFi.localIP());
configTime(3 * 3600, 3600, "pool.ntp.org", "time.nist.gov");
}
// runs over and over again
void loop() {
sendDataToHS(1234, 10);
delay(30000);
}
bool sendDataToHS(int dvRef, String data) {
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
return false;
}
if (isnan(data.toFloat())) {
return false;
}
String url = "/JSON?request=controldevicebyvalue&ref=" + String(dvRef) + "&value=" + String(data);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Basic .....\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Request sent (device " + String(dvRef) + ", data " + String(data) + ")");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
return true;
break;
}
}
return false;
}