0

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

  • data and http header are separated by an empty line. then see ArduinoJson library – Juraj Jan 5 '18 at 10:47
  • @Juraj can you please explain data and header saperately by an empty line, is it good? Arduino output is exactly like this. – Himalya vats Jan 5 '18 at 11:01
  • what you printed is a HTTP response. starting with HTTP/1.1 status line, then there are the HTTP headers. and then the empty line separates the header from body. in the body are your data. try using ArduinoHttpClient library over WiFiEspClient, if you do not want to parse the response self. – Juraj Jan 5 '18 at 11:25
1

When Arduinos unexpectedly restarts it is usually because you have drawn too much power or have caused a segmentation error or BSOD in Windows terms. You aren't drawing to much power, so I suspect you are accessing elements of the array outside those that you should be. Check that i never exceeds the maximum size of serverResponse, because:

char serverResonse[10]
for int i - 0; i < 50; i++)
  serverResponse[i] = 42;  // This will crash on the 11th loop.

I can't say if the above code will cause a restart, but that's dependent on what was using the 11th byte of the array before it got stomped on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.