read json from domoticz to arduino

Moderator: leecollings

Post Reply
goldman541
Posts: 16
Joined: Saturday 19 November 2016 15:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

read json from domoticz to arduino

Post by goldman541 »

i have a arduino mkr 1010 and want to make a automatic plant watering.
i can already send values to domoticz from my sensors.
but i want also to read the status from a selector switch to know in which mode to operate.
if i put a constant value the code works, but i don't get a new json string from domoticz.
Can someone help me with this part of the the code.

Code: Select all

#include <ArduinoJson.h>
#include <RTCZero.h>
#include <ArduinoLowPower.h>
#include <WiFiNINA.h>
const char* ssid = "----";
const char* password = "----";
WiFiClient client;
// Domoticz server info
const char * domoticz_server = "192.168.2.3";
int port = 8080; //Domoticz port
int idx1 = 2602; //plantenbak 1
int idx2 = 2603; //plantenbak 2
int idx3 = 2604; //plantenbak 3
int idx4 = 2605; //plantenbak 4
int plantenbakval1 = 0;
int plantenbakval2 = 0;
int plantenbakval3 = 0;
int plantenbakval4 = 0;
int grondvochtigheid1 = 0;
int grondvochtigheid2 = 0;
int grondvochtigheid3 = 0;
int grondvochtigheid4 = 0;
const int powersoil = 1 ; // Digital Pin 8 will power the sensor, acting as switch
const int plantenbakin1 = A0;
const int plantenbakin2 = A1;
const int plantenbakin3 = A2;
const int plantenbakin4 = A3;
const long interval = 2000;
const unsigned reading_count = 10; // Numbber of readings each time in order to stabilise
unsigned int analogVals[reading_count];
unsigned int counter = 0;
unsigned int values_avg1 = 0;
unsigned int values_avg2 = 0;
unsigned int values_avg3 = 0;
unsigned int values_avg4 = 0;
const unsigned sleepTimeS = 60; // Seconds to sleep between readings
const unsigned sleepTimeMin = 15; // Minutes to sleep between readings




void setup() {
	// initialize serial communication at 115200 bits per second:
	Serial.begin(115200);
	delay(10);
	pinMode(powersoil, OUTPUT);
	pinMode(plantenbakin1, INPUT);
	pinMode(plantenbakin2, INPUT);
	pinMode(plantenbakin3, INPUT);
	pinMode(plantenbakin4, INPUT);
	Serial.println();
	Serial.println();
	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());
	delay(2000);
}

void loop() {
	Serial.print("Starting.");
	delay(500);
	values_avg1 = 0;
	values_avg2 = 0;
	values_avg3 = 0;
	values_avg4 = 0;
	counter = 0;

	Serial.println("sensoren aan");
	digitalWrite(powersoil, HIGH);
	delay(1000);
	// read the input on analog pin 0:
	for (int counter = 0; counter < reading_count; counter++) {
		Serial.print("sensorwaardenlezen.:");
			plantenbakval1 = analogRead(plantenbakin1); 
			plantenbakval2 = analogRead(plantenbakin2);
			plantenbakval3 = analogRead(plantenbakin3);
			plantenbakval4 = analogRead(plantenbakin4);
			delay(100);
			values_avg1 = (values_avg1 + plantenbakval1);
			values_avg2 = (values_avg2 + plantenbakval2);
			values_avg3 = (values_avg3 + plantenbakval3);
			values_avg4 = (values_avg4 + plantenbakval4);
			Serial.print(plantenbakval1);
			Serial.print(",");
			Serial.print(plantenbakval2);
			Serial.print(",");
			Serial.print(plantenbakval3);
			Serial.print(",");
			Serial.println(plantenbakval4);
			Serial.print("totale waarden..........:");
			Serial.print(values_avg1);
			Serial.print(",");
			Serial.print(values_avg2);
			Serial.print(",");
			Serial.print(values_avg3);
			Serial.print(",");
			Serial.println(values_avg4);
	}
	values_avg1 = values_avg1 / reading_count;
	values_avg2 = values_avg2 / reading_count;
	values_avg3 = values_avg3 / reading_count;
	values_avg4 = values_avg4 / reading_count;
	Serial.print("gemidelde waarde.....:");
	Serial.print(values_avg1);
	Serial.print(",");
	Serial.print(values_avg2);
	Serial.print(",");
	Serial.print(values_avg3);
	Serial.print(",");
	Serial.println(values_avg4);
	// make average value
	grondvochtigheid1 = map(values_avg1, 0, 1200, 200, 0);
	grondvochtigheid2 = map(values_avg2, 0, 1200, 200, 0);
	grondvochtigheid3 = map(values_avg3, 0, 1200, 200, 0);
	grondvochtigheid4 = map(values_avg4, 0, 1200, 200, 0);
	// print out the value the sensor reads:
	Serial.print("uitgangswaarde........:");// 0 = nat 200 = droog
	Serial.print(grondvochtigheid1);
	Serial.print(",");
	Serial.print(grondvochtigheid2);
	Serial.print(",");
	Serial.print(grondvochtigheid3);
	Serial.print(",");
	Serial.println(grondvochtigheid4);
	delay(interval);
	Serial.println("sensoren uit");
	digitalWrite(powersoil, LOW);
	delay(interval);
	httpRequestswitch();
	httpRequestMOISTURE(grondvochtigheid1, idx1);
	httpRequestMOISTURE(grondvochtigheid2, idx2);
	httpRequestMOISTURE(grondvochtigheid3, idx3);
	httpRequestMOISTURE(grondvochtigheid4, idx4);
	/*Serial.print("deepsleep voor 5min");
	delay(1000);
	Serial.print("...");
	delay(1000);
	Serial.println("...");
	delay(1000);
	Serial.println("ESP8266 in sleep mode");
	LowPower.sleep(1000);*/
	//300000
	delay(2000);


}

// /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP
void httpRequestMOISTURE(float value, int id) {
	// if there's a successful connection:
	if (client.connect(domoticz_server, port)) {
		Serial.println("client connected sending temp value");
		//Serial.println(value);
		client.print("GET /json.htm?type=command&param=udevice&idx=");
		client.print(id);
		client.print("&nvalue=");
		client.print(value);
		client.println(" HTTP/1.1");
		client.print("Host: ");
		client.print(domoticz_server);
		client.print(":");
		client.println(port);
		client.println("User-Agent: Arduino-ethernet");
		client.println("Connection: close");
		client.println();
		client.stop();
	}
		//delay(requestdelay);
	else {
		Serial.println("client could not connect (DS value)");
		client.stop();
	}
}
void httpRequestswitch() {
	const char* json = "{\"ActTime\":1565731837,\"AstrTwilightEnd\":\"23:34\",\"AstrTwilightStart\":\"04:01\",\"CivTwilightEnd\":\"21:47\",\"CivTwilightStart\":\"05:48\",\"DayLength\":\"14:44\",\"NautTwilightEnd\":\"22:35\",\"NautTwilightStart\":\"04:60\",\"ServerTime\":\"2019-08-13 23:30:37\",\"SunAtSouth\":\"13:48\",\"Sunrise\":\"06:26\",\"Sunset\":\"21:10\",\"app_version\":\"4.10717\",\"result\":[{\"AddjMulti\":1,\"AddjMulti2\":1,\"AddjValue\":0,\"AddjValue2\":0,\"BatteryLevel\":255,\"CustomImage\":11,\"Data\":\"Set Level: 30 %\",\"Description\":\"\",\"DimmerType\":\"none\",\"Favorite\":0,\"HardwareID\":6,\"HardwareName\":\"Dummy\",\"HardwareType\":\"Dummy (Does nothing, use for virtual switches only)\",\"HardwareTypeVal\":15,\"HaveDimmer\":true,\"HaveGroupCmd\":true,\"HaveTimeout\":false,\"ID\":\"00000200\",\"Image\":\"Water\",\"IsSubDevice\":false,\"LastUpdate\":\"2019-08-13 23:13:36\",\"Level\":30,\"LevelActions\":\"fHx8\",\"LevelInt\":30,\"LevelNames\":\"T2ZmfExldmVsMXxMZXZlbDJ8TGV2ZWwz\",\"LevelOffHidden\":false,\"MaxDimLevel\":100,\"Name\":\"test\",\"Notifications\":\"false\",\"PlanID\":\"0\",\"PlanIDs\":[0],\"Protected\":false,\"SelectorStyle\":0,\"ShowNotifications\":true,\"SignalLevel\":\"-\",\"Status\":\"Set Level: 30 %\",\"StrParam1\":\"\",\"StrParam2\":\"\",\"SubType\":\"Selector Switch\",\"SwitchType\":\"Selector\",\"SwitchTypeVal\":18,\"Timers\":\"false\",\"Type\":\"Light/Switch\",\"TypeImg\":\"Light\",\"Unit\":1,\"Used\":1,\"UsedByCamera\":false,\"XOffset\":\"0\",\"YOffset\":\"0\",\"idx\":\"1084\"}],\"status\":\"OK\",\"title\":\"Devices\"}";

	int HTTP_TIMEOUT = 1000;
		const size_t capacity = 2 * JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(16) + JSON_OBJECT_SIZE(50) + 1150;
		DynamicJsonDocument doc(capacity);
		if (client.connect(domoticz_server, port)) {
			Serial.println("verbonden json uitlezen");
			client.print("GET /json.htm?type=devices&rid=1084");
			client.println(" HTTP/1.1");
			client.print("Host: ");
			client.print(domoticz_server);
			client.print(":");
			client.println(port);
			client.println("User-Agent: Arduino-ethernet");
			client.read();
			//Serial.write(client.print("GET / json.htm ? type = devices & rid = 1084"));
			deserializeJson(doc, json);
			client.setTimeout(HTTP_TIMEOUT);
			client.stop();
		}
		
		

		long ActTime = doc["ActTime"]; // 1565731837
		const char* AstrTwilightEnd = doc["AstrTwilightEnd"]; // "23:34"
		const char* AstrTwilightStart = doc["AstrTwilightStart"]; // "04:01"
		const char* CivTwilightEnd = doc["CivTwilightEnd"]; // "21:47"
		const char* CivTwilightStart = doc["CivTwilightStart"]; // "05:48"
		const char* DayLength = doc["DayLength"]; // "14:44"
		const char* NautTwilightEnd = doc["NautTwilightEnd"]; // "22:35"
		const char* NautTwilightStart = doc["NautTwilightStart"]; // "04:60"
		const char* ServerTime = doc["ServerTime"]; // "2019-08-13 23:30:37"
		const char* SunAtSouth = doc["SunAtSouth"]; // "13:48"
		const char* Sunrise = doc["Sunrise"]; // "06:26"
		const char* Sunset = doc["Sunset"]; // "21:10"
		const char* app_version = doc["app_version"]; // "4.10717"

		JsonObject result_0 = doc["result"][0];
		int result_0_AddjMulti = result_0["AddjMulti"]; // 1
		int result_0_AddjMulti2 = result_0["AddjMulti2"]; // 1
		int result_0_AddjValue = result_0["AddjValue"]; // 0
		int result_0_AddjValue2 = result_0["AddjValue2"]; // 0
		int result_0_BatteryLevel = result_0["BatteryLevel"]; // 255
		int result_0_CustomImage = result_0["CustomImage"]; // 11
		const char* result_0_Data = result_0["Data"]; // "Set Level: 30 %"
		const char* result_0_Description = result_0["Description"]; // ""
		const char* result_0_DimmerType = result_0["DimmerType"]; // "none"
		int result_0_Favorite = result_0["Favorite"]; // 0
		int result_0_HardwareID = result_0["HardwareID"]; // 6
		const char* result_0_HardwareName = result_0["HardwareName"]; // "Dummy"
		const char* result_0_HardwareType = result_0["HardwareType"]; // "Dummy (Does nothing, use for virtual switches only)"
		int result_0_HardwareTypeVal = result_0["HardwareTypeVal"]; // 15
		bool result_0_HaveDimmer = result_0["HaveDimmer"]; // true
		bool result_0_HaveGroupCmd = result_0["HaveGroupCmd"]; // true
		bool result_0_HaveTimeout = result_0["HaveTimeout"]; // false
		const char* result_0_ID = result_0["ID"]; // "00000200"
		const char* result_0_Image = result_0["Image"]; // "Water"
		bool result_0_IsSubDevice = result_0["IsSubDevice"]; // false
		const char* result_0_LastUpdate = result_0["LastUpdate"]; // "2019-08-13 23:13:36"
		int result_0_Level = result_0["Level"]; // 30
		const char* result_0_LevelActions = result_0["LevelActions"]; // "fHx8"
		int result_0_LevelInt = result_0["LevelInt"]; // 30
		const char* result_0_LevelNames = result_0["LevelNames"]; // "T2ZmfExldmVsMXxMZXZlbDJ8TGV2ZWwz"
		bool result_0_LevelOffHidden = result_0["LevelOffHidden"]; // false
		int result_0_MaxDimLevel = result_0["MaxDimLevel"]; // 100
		const char* result_0_Name = result_0["Name"]; // "test"
		const char* result_0_Notifications = result_0["Notifications"]; // "false"
		const char* result_0_PlanID = result_0["PlanID"]; // "0"

		int result_0_PlanIDs_0 = result_0["PlanIDs"][0]; // 0

		bool result_0_Protected = result_0["Protected"]; // false
		int result_0_SelectorStyle = result_0["SelectorStyle"]; // 0
		bool result_0_ShowNotifications = result_0["ShowNotifications"]; // true
		const char* result_0_SignalLevel = result_0["SignalLevel"]; // "-"
		const char* result_0_Status = result_0["Status"]; // "Set Level: 30 %"
		const char* result_0_StrParam1 = result_0["StrParam1"]; // ""
		const char* result_0_StrParam2 = result_0["StrParam2"]; // ""
		const char* result_0_SubType = result_0["SubType"]; // "Selector Switch"
		const char* result_0_SwitchType = result_0["SwitchType"]; // "Selector"
		int result_0_SwitchTypeVal = result_0["SwitchTypeVal"]; // 18
		const char* result_0_Timers = result_0["Timers"]; // "false"
		const char* result_0_Type = result_0["Type"]; // "Light/Switch"
		const char* result_0_TypeImg = result_0["TypeImg"]; // "Light"
		int result_0_Unit = result_0["Unit"]; // 1
		int result_0_Used = result_0["Used"]; // 1
		bool result_0_UsedByCamera = result_0["UsedByCamera"]; // false
		const char* result_0_XOffset = result_0["XOffset"]; // "0"
		const char* result_0_YOffset = result_0["YOffset"]; // "0"
		const char* result_0_idx = result_0["idx"]; // "1084"

		const char* status = doc["status"]; // "OK"
		const char* title = doc["title"]; // "Devices"

		Serial.println(result_0_LevelInt);
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest