JSONVar and IF statement

Hello Guys!
I am trying to control an impulse relay over internet.

My code isn't hard since I am rookie, but I keep getting error that I can not surpass. I have searched through this forum and found similar topics (for example this one), but it doesn't work for me.

I am reading MySQL database, and I want according to a value to turn selected GPIO.
Where I have a problem is that I can not compare the JSON variable with integer nor char.
I keep getting this message:

ambiguous overload for 'operator==' (operand types are 'JSONVar' and 'int')

I tryed to convert the variable to integer, or string or char but it doesn't work.
I get only different error naming that other variable.

JSONVar keys = myObject.keys();
JSONVar value = int (myObject[keys[0]]);

if (value==0){

Serial.print("GPIO: ");
Serial.print(keys[0]);
Serial.println(value);
pinMode(26, OUTPUT);
digitalWrite(26, HIGH);
delay(100);
digitalWrite(26, LOW);
}

if (value==1){

Serial.print("GPIO: ");
Serial.print(keys[1]);
Serial.println(value);
pinMode(27, OUTPUT);
digitalWrite(27,HIGH);
delay(100);
digitalWrite(27, LOW);
}

else {
Serial.println("Error");
}

I am using this JSON library

#include <Arduino_JSON.h>

I have also tried doing it like this:

JSONVar keys = myObject.keys();
JSONVar value = myObject[keys[0]];
intVal = int(value);
if (intVal==0){

Serial.print("GPIO: ");
Serial.print(keys[0]);
Serial.println(intVal);
pinMode(26, OUTPUT);
digitalWrite(26, HIGH);
delay(100);
digitalWrite(26, LOW);
}
if (intVal==1){

Serial.print("GPIO: ");
Serial.print(keys[1]);
Serial.println(intVal);
pinMode(27, OUTPUT);
digitalWrite(27,HIGH);
delay(100);
digitalWrite(27, LOW);
}
else {
Serial.println("Error");
}

I have also tryed by usind switch..case, and it doesn't give me error, but it reads the "intVal" as 0 no matter if it changed in the database to 1. As if I can not convert it to anything else but 0. ::slight_smile:

Can you show an example of the JSON response that the MySQL server delivers (maybe via a web server) to your Arduino.

It may look something like this:

{
  "coord": {
    "lon": -122.08,
    "lat": 37.39
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 282.55,
    "feels_like": 281.86,
    "temp_min": 280.37,
    "temp_max": 284.26,
    "pressure": 1023,
    "humidity": 100
  },
  "visibility": 16093,
  "wind": {
    "speed": 1.5,
    "deg": 350
  },
  "clouds": {
    "all": 1
  },

6v6gt:
Can you show an example of the JSON response that the MySQL server delivers (maybe via a web server) to your Arduino.

I have copied the code from this tutorial, and I am now trying to customize it to my needs.
The code from tutorial that looks like this:

JSONVar keys = myObject.keys();
    
      for (int i = 0; i < keys.length(); i++) {
        JSONVar value = myObject[keys[i]];
        Serial.print("GPIO: ");
        Serial.print(keys[i]);
        Serial.print(" - SET to: ");
        Serial.println(value);
        pinMode(atoi(keys[i]), OUTPUT);
        digitalWrite(atoi(keys[i]), atoi(value));
      }

... I have customized into that what I have shown in first post.
There are several files in the tutorial, and I am customizing only the Arduino sketch because I am unfamiliar with PHP and MySQL.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.