http get request / variable response

Hi guys

Does anyone know how I can make this work.

I am sending this get request and if the response comes out with ch:2 and val:1 I want to set pin2 high
all the other numbers I don't wanna look at, they are changing.

String restgetstring = String("{"Ch":2,"Md":0,"Val":1,"Stat":1,"PsCtn":0,"PsStop":0,"PsIV":0}");

void loop() {
HTTPClient http;

http.begin(client, "http://"SERVER_IP"/do_value/slot_0/ch_2");
http.addHeader("Authorization", "Basic cm8vdDjhMDAwMSCwMA==");
//http.addHeader("Content-Type", "application/json");
Serial.println("[HTTP] GET...\n");

// start connection and send HTTP header and body

int httpCode = http.GET();

String response = http.getString();

Serial.print("StatusCode: ");
Serial.println(httpCode);
Serial.print("Response: ");
Serial.println(response);

if (response == restgetstring)
{
digitalWrite(pin2,HIGH);

}
if (!(response == restgetstring))
{
digitalWrite(pin2,LOW);
}
}

best regards
Term

the code above won't compile...Don't post snippets (Snippets R Us!)

are you sure you have a perfect match between the response and your restgetstring, wouldn't restgetstring be a substring of the response ?

side note: you don't need to do the test twice for just the opposite condition, if/else is there for that

if (condition) {...} else {....}

Hi

The code is working as long as md & stat doesn't change.
How can I fix it so md and stat can be any number and the pin2 will go high if only ch and val is what I have defined?

best regards
Term

sorry post 1 still looks ugly and does not compile.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define SERVER_IP “192.168.1.110”
#ifndef STASSID
#define STASSID “homenetwork”
#define STAPSK “password1234”
#endif

String restgetstring = String("{“Ch”:2,“Md”:\4,“Val”:1,“Stat”:1,“PsCtn”:0,“PsStop”:0,“PsIV”:0}");

const char* ssid = STASSID;
const char* password = STAPSK;

const int pin2 = 2;
const int wifiled = 6;

void setup()
{

pinMode(wifiled, OUTPUT);
pinMode(pin2, OUTPUT);

Serial.begin(115200);
WiFi.mode(WIFI_STA);

Serial.println();
Serial.println();
Serial.println();
WiFi.hostname(“Test module”);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
digitalWrite(wifiled,LOW);

}
}

void loop() {
digitalWrite(wifiled,HIGH);

WiFiClient client;

HTTPClient http;

http.begin(client, “http://“SERVER_IP”/do_value/slot_0/ch_2”);
http.addHeader(“Authorization”, “Basic cm9vdDowMDAwMDAwMA==”);
//http.addHeader(“Content-Type”, “application/json”);
Serial.println("[HTTP] GET…\n");
// start connection and send HTTP header and body

int httpCode = http.GET();

String response = http.getString();

Serial.print("StatusCode: ");
Serial.println(httpCode);
Serial.print("Response: ");
Serial.println(response);

if (response == restgetstring)
{
digitalWrite(pin2,HIGH);
}
if (!(response == restgetstring))
{
digitalWrite(pin2,LOW);
}
}

"The code is working as long as md & stat doesn't change.
How can I fix it so md and stat can be any number and the pin2 will go high if only ch and val is what I have defined?"

It is not really clear to me just what you are trying to do. Maybe you could explain just how your code is expected to work.