This is example code of led blinking . variable interval is already initialized in code. But I want to set variable interval on API call, Idon't understand how to do it
const int ledPin = LED_BUILTIN; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT); // set the digital pin as output:
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
Here I have tried
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET"; // or POST
char HOST_NAME[] = "makegjjgj.com";
String PATH_NAME = "/index";
int Delay;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // set the digital pin as output:
// connect to server on port 80:
if (client.connect(HOST_NAME, HTTP_PORT)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP header
while (client.connected()) {
if (client.available()) {
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
StaticJsonDocument<24> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
Delay = doc["Delay"]; // 5
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void loop() {
}
Assuming it works and that Delay is a global variable
if you read in the setup then you will read the information only once and use that for the rest of the code.
if you read in the loop, it will slow somewhat your code - possibly messing with the timing if you want ultra short delays, but it will be updated every time based on what the server sends back
Apparently you did because your question is answered in it.
"interval" should not be declared "const". If it is only required to be set once, set it in "setup()". If it may be changed during runtime, set it in "loop()" but make it in intervals not for each iteration. How to recover the value can be deduced from the other thread.
Is either the right approach? In your sketch the Arduino acts as a client. How will it know when the delay should be updated? It would have to continuously send requests to the server, which would be like a DoS attack, in order to detect a change in the required delay.
Would it not be better to have the Arduino act as a server? That way, a remote client can request a change to the delay when it needs to change. The updated delay value could be indicated as a parameter in the request, so that no JSON message is needed.
I didn't see the data on the serial monitor when I tested. I don't have Arduino right now, I can test using some print statement and tell you when I'm at homey
As you where told multiple times in the other topic, you do not mind the HTTP response (including headers), all you do is read one character and then "deserializeJson" has to handle the rest - which is invalid JSON. The solution to this problem was provided in the other topic, so..........
may be make real cString out of this by adding a trailing null char (and check the returned value to see if you got a timeout)
once done, print it as well to see what you received