I am trying to fetch some data from my website using esp8266 and Arduino, with the help of WiFiEsp library, I have got data from the website but don't know how to save that data in an array or variable and use it, now I can only see received data on the serial monitor.
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
char ssid[] = "moto g"; // your network SSID (name)
char pass[] = "hvats555"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "himalayavats.com";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 10000L;
WiFiEspClient client;
void setup()
{
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
}
void loop()
{
// if there's incoming data from the net connection send it out the serial
port
// this is for debugging purposes only
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// if 10 seconds have passed since your last connection,
// then connect again and send data
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server
void httpRequest()
{
Serial.println();
// close any connection before send a new request
// this will free the socket on the WiFi shield
client.stop();
// if there's a successful connection
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP PUT request
client.println(F("GET /to_fetch/system_info.json HTTP/1.1"));
client.println(F("Host: himalayavats.com"));
client.println("Connection: close");
client.println();
// note the time that the connection was made
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection
Serial.println("Connection failed");
}
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Response from arduino(in serial monitor):-
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.3.0
Attempting to connect to WPA SSID: moto g
[WiFiEsp] Connected to moto g
You're connected to the network
SSID: moto g
IP Address: 192.168.43.61
Signal strength (RSSI):-372 dBm
[WiFiEsp] Connecting to himalayavats.com
Connecting...
HTTP/1.1 200 OK
Date: Fri, 05 Jan 2018 10:19:46 GMT
Server: Apache
ETag: "12c077d-12-55dc4cfa4fffd"
Accept-Ranges: bytes
Content-Length: 18
Vary: User-Agent
Connection: close
Content-Type: application/json
{"power" : "high"} // <-- i wanna fetch this
NOTE:- Whenever I try to add anything in loop function for example:-
int i = 0;
while(client.avilable()){
c = client.read();
serverResponse[i] = c; // trying to save output in an array
i++;
}
The module doesn't even fetch data from the server it keeps restarting itself. Output from arduino after adding above code in loop funtion:-
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.3.0
Attempting to connect to WPA SSID: moto g
[WiFiEsp] Connected to moto g
You're connected to the network
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.3.0
Attempting to connect to WPA SSID: moto g
[WiFiEsp] Connected to moto g
You're connected to the network
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.3.0
Attempting to connect to WPA SSID: moto g
[WiFiEsp] Connected to moto g
You're connected to the network
This repeating again and again