Go Down

Topic: send data to server using POST method wth ESP8266 and arduino  (Read 143 times) previous topic - next topic

Kumkum23

Nov 07, 2019, 12:08 pm Last Edit: Nov 07, 2019, 01:14 pm by Kumkum23 Reason: I did not attach the screenshot of my output.
hai folks,
I am trying to send data to server with esp8266 module and arduino uno using POST method. I was testing some code from online as this is the 1st time ever I am working with ESP8266 module. Module is getting connected to the wifi network.

But I am getting error of TIMEOUT while connecting to the server. It's showing TIMEOUT and then no tag found.

What could be the possible reason for this error? 
How should I solve this?

I am attaching code and a screenshot of serial monitior output. Kindly check it and help me if possible. Thanks in advance.

Code: [Select]


#include "WiFiEsp.h"

// Emulate Serial1 on pins 7/8 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(7, 8); // RX, TX
#endif

char ssid[] = "wifi ssid";            // your network SSID (name)
char pass[] = "password";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char server[] = "server URL";

// Initialize the Ethernet client object
WiFiEspClient client;

void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);
  // 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);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

  printWifiStatus();

  Serial.println();
  Serial.println("Starting connection to server...");
  // if you get a connection, report back via serial
  if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    // Make a HTTP request
    String content = "Hey, just testing a post request.";
    client.println("POST YOUR_RESOURCE_URI HTTP/1.1");
    client.println("Host: SERVER:PORT");
    client.println("Accept: */*");
    client.println("Content-Length: " + content.length());
    client.println("Content-Type: application/json");
    client.println();
    client.println(content);
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client
  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnecting from server...");
    client.stop();

    // do nothing forevermore
    while (true);
  }
}


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");
}





Juraj

sever should be server, not url
client.println("Content-Length: " + content.length()); is wrong. divide it to two prints

Kumkum23

changed the server and also divided the Client.print() into two prints. Still getting same error as TIMEOUT and no tag found.

Any other suggestions to solve this?

Juraj

changed the server and also divided the Client.print() into two prints. Still getting same error as TIMEOUT and no tag found.

Any other suggestions to solve this?
show current code with more realistic server and url strings

Kumkum23

Also I am attaching the output
Code: [Select]


#include "WiFiEsp.h"

// Emulate Serial1 on pins 7/8 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(7, 8); // RX, TX
#endif

char ssid[] = "SARAI-ACT";            // your network SSID (name)
char pass[] = "sarai@sm";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char server[] = "134.209.149.196:8080";

// Initialize the Ethernet client object
WiFiEspClient client;

void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);
  // 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);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

  printWifiStatus();

  Serial.println();
  Serial.println("Starting connection to server...");
  // if you get a connection, report back via serial
  if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    // Make a HTTP request
    float t= 12.23;
    char Temp[10]= {0};
    char temp_str[]={10};
    dtostrf(t,5,2,temp_str);
  sprintf(Temp, "%s", temp_str);
  memset(temp_str, 0, sizeof(temp_str));
    String content = "{ \"temperature\" : \""+String(Temp)+"\"}";
    client.println("POST /coolgix/api/iot/v1/check-temp-data HTTP/1.1");
    client.println("Host: 134.209.149.196:8080");
    client.println("Accept: */*");
    client.println("Content-Length: " + content.length());
    client.println("Content-Type: application/json");
    client.println();
    client.println(content);
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client
  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnecting from server...");
    client.stop();

    // do nothing forevermore
    while (true);
  }
}


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");
}

Juraj

sever for connect() must be an IP address or a server name. no port. the Host header in HTTP request doesn't require the port.
your Content-length header is invalid. You can use + to convert number to string and concatenate it to other constant string.

Kumkum23

Attempted all the steps you suggested. But I am getting same TIMEOUT, data packet send error and failed to write to socket error. What should I do now?

Juraj

Attempted all the steps you suggested. But I am getting same TIMEOUT, data packet send error and failed to write to socket error. What should I do now?
show the current sketch and the output in Serial Monitor

Go Up