Hi guys,
So I have been working with Wifi shield and arduino uno. I have managed to (with help from online) have my Arduino connect to my website server and open a txt file from the website, read the info from the txt file and display it on the serial monitor.
right now the txt file reads *60. so the serial monitor displays 60.
Now say if I want to ready info from multiple line (from three lines).
first line will say *60
second line will say *70
third line will say *80 .how can i achieve this.
I am trying to understand the part of the code that i got from online which is in the loop function (shown below) that extracts data as string (*60) and converts it into a double(well int) naming it point1. After understanding the snippet i would like to create two more variables point2 and point3 to store info in second and third line. I have written what i don't understand in the comments of the code. please take a look and thank you in advance.
#include <SPI.h>
#include <WiFi.h>
double point1;
int led1 = 8; // act as the Display
int led2 = 9; // act as the Display
char ssid[] = "ThomsonEEB616"; // your network SSID (name)
char pass[] = "FF07A32DE7"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "www.mywebsite.com"; // name address for Google (using DNS)
String dataLocation1 = "/data1.txt HTTP/1.1";
//String dataLocationL1 = "/dataL1.txt HTTP/1.1";
WiFiClient client;
String currentLine = ""; // string for incoming serial data
String currRates = "";
boolean readingRates = false; // is reading?
const int requestInterval = 10000; // milliseconds delay between requests
boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
/////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
// digitalWrite(led1, LOW);
// digitalWrite(led2, LOW);
// start the Wifi connection:
if (WiFi.begin(ssid, pass) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Checking for new data
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = ""; // WHY DO I NEED TO CLEAR THE LINE?
}
if (currentLine.endsWith("*")) { //WHAT IS READING RATE WHY IS IT USED?
readingRates = true;
}
else if (readingRates) {
if (!currentLine.endsWith(" ")) {//is our ending character
currRates += inChar;
}
else {
readingRates = false;
String justRates = currRates.substring(0, currRates.length()); //WHAT IS GOING ON HERE?
Serial.println(justRates);
// Split justRates
int firstSpaceIndex = justRates.indexOf(" ");
String firstVal = justRates.substring(0, firstSpaceIndex);
// Convert strings to floats and store
// double strtod (const char* firstVal);
// char buf[firstVal.length()];
// firstVal.toCharArray(buf,firstVal.length()+1);
// double point1 = atof(buf,2); // Edit the 6 to how many digits precision you need
point1 = (firstVal.toInt()); // convert sring to integers
// Reset for next reading
currRates = "";
client.stop();
/////////////////////////output/////////////////////////////
Serial.println(point1);
Serial.println();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServerR();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void connectToServerR() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(server, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to dataLocation:
client.println("GET " + dataLocation1);
client.println("Host: mywebsite.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}