-1

I have been looking for over 2 hours now and just cant get it figured out. Im very confused as to how to do this. The solution I thought I found was to use HTTP POST. But I just cant get it to work. To be honest i'm guessing this should be very simple. Im using the variable

sensorValue = analogRead(0); 

And then want to sent that Variable to my Webserver with the ip 172.20.10.5

Then I wish to save this variable into an array on my Webserver. I am sorry for this request I of course looked at least on 20 diffrent sites and questions and cant find a solution.

#include <SPI.h>
#include <Ethernet.h>
#include "ESP8266WiFi.h"

IPAddress server(172,20,10,5);
 if(client.connect(server, 80)) {
  client.println("POST /Raum1 HTTP/1.1");
  client.println("Host: 172.20.10.5");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(sensorValue.length());
  client.println();
  client.println(sensorValue);
}
void setup(){
Serial.begin(9600);                            // sets the serial port to 9600
  WiFi.begin("iPad von Tobias", "12345678");  //Connect to the WiFi network

  while (WiFi.status() != WL_CONNECTED) {  //Wait for connection

    delay(500);
    Serial.println("Waiting to connect…");

  }

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //Print the local IP
 }

//byte server[] = {172, 20, 10, 5}; //IP
void loop(){
sensorValue = analogRead(0);       // read analog input pin 0
Serial.print("AirQuality=");
Serial.print(sensorValue, DEC);               // prints the value read
Serial.println(" PPM");
delay(1000);

This is my current code on the ESP8266 I have the sensor on. The error im getting the most is: "client" has not been definied in this context. I had the HTTP POST in the loop, out of the loop, in the setup, it just doesn't work. I tried diffrent methods to write the IPAddress which didnt help either. I also do not know how to then save that Data on the Webserver. They are connected over WIFI. Maybe the http way is completly wrong?. I can't find anything for this.

I would most likely have it look like this but this doenst work either.

#include <SPI.h>
#include <Ethernet.h>
#include "ESP8266WiFi.h"
int sensorValue;

void setup(){
Serial.begin(9600);                            // sets the serial port to 9600#
WiFi.begin("iPad von Tobias", "12345678");  //Connect to the WiFi network

  while (WiFi.status() != WL_CONNECTED) {  //Wait for connection

    delay(500);
    Serial.println("Waiting to connect…");

  }

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //Print the local IP
 }

 IPAddress server(172,20,10,5);
 client.connect(server, 80)
void loop(){sensorValue = analogRead(0);       // read analog input pin 0
Serial.print("AirQuality=");
Serial.print(sensorValue, DEC);               // prints the value read
Serial.println(" PPM");
delay(1000);
  client.println("POST /Api/AddParking/3 HTTP/1.1");
  client.println("Host: 172.20.10.5");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(sensorValue.length());
  client.println();
  client.println(sensorValue);
}

Thank you for your help, I dont need a direct solution if you can point me to literatur that will help me solve this problem.

  • use GET request to post a single value – Juraj Sep 19 at 11:15
  • And how do i do that? @Juraj – MrAeRoZz Sep 19 at 12:07
  • try to adapt this example github.com/arduino-libraries/WiFi/blob/master/examples/… – Juraj Sep 19 at 13:06
  • In the first block of code, you have an if statement that is not inside a function. It is immediately after ` IPAddress server(...`. This is invalid syntax. If statements must be inside a function and this could well be the source of the error about client not being defined. Also, you never actually declare client, you might start with the examples and try to adapt them... – GMc Sep 23 at 8:11
-1

If found that you can also use UDP to do what I want.

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.