Hi Guys,
I've been at this arduino scripting less than a week so take it easy on me please ![]()
I want to make my JSON script loop over several Strings name{1-3}, I can't see how you would loop through them, guessing some sort of array? Can someone show me some examples of how to make a script loop through a series of Variables?
I'm testing on a Wemos D1 mini.
// Libraries
#include <ESP8266WiFi.h> //Provides WiFI
#include <ArduinoJson.h> //Handles the returned JSON data
 // WiFi settings
 const char* ssid  = "VM123456789";
 const char* password = "xxx";
 //hostname
 const char* host = "jsonplaceholder.typicode.com"; //Test JSON data
 //Declare the url - may not be required.
 String url = "";
Â
 //My Custom Strings - Loop through these might be more or less.
 String name1 = "1";
 String name2 = "2";
 String name3 = "3";
Â
void setup() {
 Serial.begin(115200); // Serial Setup
 Serial.print("Test Host: ");
 Serial.println(host);
 Serial.print("Test String: ");
 Serial.println(name1); //check String name via serial
Â
 WiFi.begin(ssid, password); // Do wifi stuff
Â
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 }
Â
 Serial.println(WiFi.localIP()); //Show WiFi connection
 delay(100);
}
void jsonfunc(){
  WiFiClient client;
 const int httpPort = 80;
 if (!client.connect(host, httpPort)) {
  Serial.println("connection failed");
  return;
 }
 // We now create a URI for the request
 String url = String("/todos/") +
 String(name1);
 Serial.print("URL: ");
 Serial.print(host);
 Serial.println(url);
Â
  // This will send the request to the server
 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
       "Host: " + host + "\r\n" +
       "Connection: close\r\n\r\n");
 Serial.println("Got Data...");
 delay(1000);
Â
 // Read all the lines of the reply from server.
 String answer;
 while(client.available()){
  String line = client.readStringUntil('\r');
  answer += line;
 }     Â
Â
 client.stop();
 Serial.println();
 Serial.println("closing connection");
Â
 // Process answer
 //Serial.println("Answer: ");
 //Serial.println(answer);
 // Convert to JSON
 String jsonAnswer;
 int jsonIndex;
 for (int i = 0; i < answer.length(); i++) {
 if (answer[i] == '{') {
  jsonIndex = i;
  break;
  }
 }
 // Get JSON data
 jsonAnswer = answer.substring(jsonIndex);
 Serial.println();
 Serial.print("JSON answer: ");
 Serial.println(jsonAnswer);
 jsonAnswer.trim();
 //Get current currency price index data
 int rateIndex = jsonAnswer.indexOf("title");
 String result = jsonAnswer.substring(rateIndex + 10, rateIndex + 27);
 //Print Results to the serial
 Serial.print("");
 Serial.print("");
 Serial.print("Returns = ");
 Serial.println(result);
}
void loop() {
 jsonfunc(); //Call the jsonfunc Function :)
 delay(10000);//Wait before looping
}
Thanks for any help in advance.
P