Problem sending bunch of data using array from arduino esp8266 to server.

Hi, Hope you guys are doing fine. I am facing problem like how to send array of data to the server using wifi shield esp8266. I already able to send a single data value like temperature, but not able to send 10 or 20 temperature value array at once to the server. I am new to arduino and I search a lot about it, but could not find any solution. :frowning:

Here is my code.

#include "SoftwareSerial.h"
String ssid ="lab1";
String password="";
String tmp[10] {};
String postData;
String postVariable = "temp=";
SoftwareSerial esp(3, 2);// RX, TX
int array_size;


String server = "192.168.0.45"; // www.example.com

String uri = "index.php";// our example is /esppost.php


void setup() {


esp.begin(115200);

Serial.begin(9600);

reset();

connectWifi();
int temperatureF[]= {25,23,23,33};

for(int i=0;i<4;i++){
postData = postVariable + temperatureF[i];   //Here I am able to send the data of array but I want to send all of array in first loop.
httppost();
delay(1000);
}
//data = "temperature=" + temp + "&humidity=" + ;// data sent must be under this form //name1=value1&name2=value2.

//httppost();

}

//reset the esp8266 module

void reset() {

esp.println("AT+RST");

delay(1000);

if(esp.find("OK") ) Serial.println("Module Reset");

}

//connect to your wifi network

void connectWifi() {

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";

esp.println(cmd);

delay(4000);

if(esp.find("OK")) {

Serial.println("Connected!");

}

else {

connectWifi();

Serial.println("Cannot connect to wifi"); }

}




// convert the bit data to string form




void httppost () {

esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(1000);

String postRequest =

"POST " + uri + " HTTP/1.0\r\n" +

"Host: " + server + "\r\n" +

"Accept: *" + "/" + "*\r\n" +

"Content-Length: " + postData.length() + "\r\n" +

"Content-Type: application/x-www-form-urlencoded\r\n" +

"\r\n" + postData;

String sendCmd = "AT+CIPSEND=5";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

}

}}

void loop(){
}

Is that a typo in the code?

String uri = "index.php";// our example is /esppost.php

Loook at "i" in the word that i suppose should be "url"

HI, Thank you for reply ,
yes its a typo
it's like this. The thing is the code works fine but I want to send the lot of sensor data at once, I am thinking of making a file inside arduino and put all the data in it but I could not able to do so without SD card which I dont want to use in my project because of power restraints.

String uri = "/index.php";// our example is /esppost.php

I want to send the lot of sensor data at once

What should the message containing the whole contents of the array look like given

int temperatureF[]= {25,23,23,33};

HI, I want to send the LDR sensor data, I compressed data so that 1 byte contain 2 sensor value. My project is store sensor data effectively and efficiently in the arduino as soon as the server connection is not in the range. So as the server connection establish the arduino send all the data to the server. So it depend on the avalibility of wireless Access point. so let suppose we have an array of 30 bytes having LDR sensor data. So I randomly make an array to test if my sensor data is send to server or not. These values are random of 8 bits.

byte temperatureF[]= {233,123,232,11,12};
 
[\code]

I get what you ant to do but still need to know how the message to the server should be formatted. Please give an example of the message format you want based on your example array. The server has to be able to interpret the message so its format is important

Hi, At the server side I have a php file like this.

<?php
$time = time();
$tempF = $_POST['temp'];
$file = 'temp.html';
$data = $time."  -  ".$tempF;
echo "abbdb = " .($_POST['temp']['error']) ;
file_put_contents($file, $data, FILE_APPEND);
?>

So the arduino should like massage in this format like this output. As an example sensor values

23,34,45,56,76 // sensor output at the server

I think that you mean. Sorry if I dont understand properly.

String compounding operations might be memory intensive. The dynamic String variables might need to be set back to "" as soon as they are no longer needed before more compounding operations.

yeah zoomkat you are right. I will also look for it. Thanks for that

Change this

int temperatureF[]= {25,23,23,33};

for(int i=0;i<4;i++){
postData = postVariable + temperatureF[i];   //Here I am able to send the data of array but I want to send all of array in first loop.
httppost();
delay(1000);
}

to this

int temperatureF[]= {25,23,23,33};

postData = postVariable;
for(int i=0;i<4;i++){
postData = postData + temperatureF[i] + ",";
}
httppost();
delay(1000);

Does it do what you want ?

Thank you for your reply. Well this is already working, the problem is it take approximately 3 sec to send one value of the array, and there are approximately more then 100 values which will be stored in array as this is just for example. So I want it without for loop and want it to send all the data of the array at once to the server, or if there is any other option to do so.

I want it without for loop and want it to send all the data of the array at once to the server

You can want what you like but it does not mean that its is possible

Did you try the code I posted ?
The original had multiple posts with a 1 second delay between each of them. My version has one post and the only delay() is after it anyway

Yeah it work :slight_smile: Thank you so much for your precious time. It means alot.

Do you understand how it works and how it differs from the original ?