Using POST and WIFININA on Uno Wifi Rev2

Hi all,

i have an Uno Wifi Rev2 and 5 sensors attached to it. The sensors work perfectly and the Uno is connected to my Wifisignal (WIFININA library).

I want to send the sensordata to a SQL database i have created in my domain. For this i created an php file in my domain which i can feed data, by sending it POST commands. I have tested the php file by use of a standalone POST tool and it works perfect (table in database gets filled with the test data i send)

Now i want to send the sensordata to the php. Therefore i have to use a POST command in my Arduino code. I'm having trouble getting this done.

My code:

#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h" 
#include <Wire.h>
#include "DFRobot_SHT20.h"
extern "C" {
#include "utility/twi.h" 
}
#define TCAADDR 0x70   
 
char ssid[] = SECRET_SSID; 
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS;
int waterdruk =1;
const char* serverName = "http://someserver.nl/post-esp-data.php";
String apiKeyValue = "ABC";

DFRobot_SHT20 sht20[2];            

WiFiClient client;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
   while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);  
    WiFi.config(IPAddress(192, 168, 1, 91)); 
    delay(10000);
  }
 while (!Serial);
    delay(1000);
    Wire.begin();
    Serial.begin(9600);
 }

void loop() {

    //sensor1: schuur
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 1);
    Wire.endTransmission(); 
    sht20[1].initSHT20();
    delay(100);
    float humd = sht20[1].readHumidity();
    float temp = sht20[1].readTemperature();

    //sensor2: tuin
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 2);
    Wire.endTransmission(); 
    sht20[2].initSHT20();
    delay(100);
    float humd2 = sht20[2].readHumidity();
    float temp2 = sht20[2].readTemperature();
    
    String httpRequestData = "api_key=" + apiKeyValue + "&datum=" + "2019-06-11" + "&humtemp_schuur_humid=" + humd + "&humtemp_schuur_temp=" + temp + "&humtemp_tuin_humid=" + humd2 + "&humtemp_tuin_temp=" + temp2 + "&tijd=" + "14:25:00" + "&waterdruk=" + waterdruk + "";

    Serial.print(httpRequestData);
    Serial.println();
    client.println("POST http://someserver.nl/post-esp-data.php?" + httpRequestData + " HTTP/1.1" );
    client.println();

    delay(10000);
  }

Many thanks for any help!

Scoobie

const char* serverName = "http://someserver.nl/post-esp-data.php";

That is NOT a server name. A server does NOT have a protocol. A server is NOT a php script.

Your server's name MIGHT be "someserver.nl".

    float humd2 = sht20[2].readHumidity();
    float temp2 = sht20[2].readTemperature();

Array indices start at 0. A 2 element array does not have a position 2.

    client.println("POST http://someserver.nl/post-esp-data.php?" + httpRequestData +

A POST request does not involve a protocol OR a server. You have already connected to the server.

    client.println("POST post-esp-data.php?" + httpRequestData +...

You are NOT structuring your POST request properly. You have it structured like a GET request.

PaulS:
That is NOT a server name. A server does NOT have a protocol. A server is NOT a php script.

Your server's name MIGHT be "someserver.nl".

Thanks!! I removed the line. I copied most of the code from other code and left some loose code behind it seems.

PaulS:

    float humd2 = sht20[2].readHumidity();

float temp2 = sht20[2].readTemperature();



Array indices start at 0. A 2 element array does not have a position 2.

It works like this ? I'll change the indices anyhow :slight_smile:

PaulS:

    client.println("POST http://someserver.nl/post-esp-data.php?" + httpRequestData +

A POST request does not involve a protocol OR a server. You have already connected to the server.

I am not connected to the server (someserver.nl) that has the php code on it. I want to send it to the code by use of POST.
I have used Curl to test my php file and that command is: curl -X POST -i 'http://someserver.nl/post-esp-data.php?...
I have to point the POST command to the server that has the php file on it... How else?

PaulS:

    client.println("POST post-esp-data.php?" + httpRequestData +...

You are NOT structuring your POST request properly. You have it structured like a GET request.

How should i structure this?

Thanks for your help!!!!

It works like this ?

Is that a question or a statement?

If it is a statement, that means that you simply haven't found what you are writing over that your shouldn't be.

I have used Curl to test my php file and that command is:

And you don't suppose that Curl takes the URL apart?

How should i structure this?

I heard that Mr. Google is back from vacation this morning.

@Paul:

i see what you are trying to say. Believe me when is say i have spent talking to Mr google and looking on this forum for three days in a row now. As stated: i am a beginner at this. And this beginner is completely lost. That is why i use this forum to get back on track.

I do thank you for your pointers and the help you give here. But not everyone is a couchpotato that asks (evidentely) simple questions...

I do hope someone else remembers his/her starting days being frustrated :o of the fact that all the code you've read and tried doesn't work?

The code i am using is in this tutorial. When i use the httpclient i get errors, i assume that this library is not compatible with the uno wifi rev2 and so i am trying to get around this by typing a post command fully...

Perhaps i am writing gibberish, that would explain a lot! A link of where i can read the right stuff will really be enough.

Please help me in the right direction.

Scoobie

I get the same responses too, just go check google, or did you read this or that how to use this forum posting.

Oh well.

The following library can be installed using the IDE Library manager and includes a simple POST example. I tried the example with a MKR1010/WiFiNINA and it works. It should work on other boards using the WiFiNINA library (and maybe Adafruit's airlift fork).

The simple POST example modified to work with a public HTTP test server named httpbin.org.

/*
  Simple POST client for ArduinoHttpClient library
  Connects to server once every 30 seconds, sends a POST request
  and a request body

  created 14 Feb 2016
  modified 22 Jan 2019
  by Tom Igoe
  
  this example is in the public domain

  Modified to work against httpbin.org. The equivalent POST using curl is:
  
  curl --data "arg1=value1&arg2=value2" --verbose http://httpbin.org/post

  The response body contains JSON showing the values and headers received by the httpbin
  server. Example follows.
  
making POST request
Status code: 200
Response: {
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "arg1": "value1", 
    "arg2": "value2"
  }, 
  "headers": {
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Arduino/2.2.0"
  }, 
  "json": null, 
  "url": "https://httpbin.org/post"
}
 */
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

const char serverName[] = "httpbin.org";  // server name
int port = 80;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(115200);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

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

void loop() {
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded";
  String postData = "arg1=value1&arg2=value2";

  client.post("/post", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  String response = client.responseBody();
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait 30 seconds");
  delay(30000);
}

gbafamiliy,

from the bottem of my heart: THANK YOU !!!!

This is what is the situation now... it WORKS!

#include <ArduinoHttpClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h" 
#include <Wire.h>
#include "DFRobot_SHT20.h"
extern "C" {
#include "utility/twi.h" 
}
#define TCAADDR 0x70   

char ssid[] = SECRET_SSID; 
char pass[] = SECRET_PASS;

int waterdruk =1;
String apiKeyValue = "ABC";
const char serverName[] = "someserver.nl";  // server name
int port = 80;

DFRobot_SHT20 sht20[2];            

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverName, port);

int status = WL_IDLE_STATUS;
void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; 
  }
   while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);  
    // WiFi.config(IPAddress(192, 168, 1, 91)); 
    delay(10000);
  }
 while (!Serial);
    delay(1000);
    Wire.begin();

 }

void loop() {

    //sensor1: schuur
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 1);
    Wire.endTransmission(); 
    sht20[1].initSHT20();
    delay(100);
    float humd = sht20[1].readHumidity();
    float temp = sht20[1].readTemperature();

    //sensor2: tuin
    Wire.beginTransmission(TCAADDR);
    Wire.write(1 << 2);
    Wire.endTransmission(); 
    sht20[2].initSHT20();
    delay(100);
    float humd2 = sht20[2].readHumidity();
    float temp2 = sht20[2].readTemperature();
    
    String httpRequestData = "api_key=" + apiKeyValue + "&datum=" + "2019-06-11" + "&humtemp_schuur_humid=" + humd + "&humtemp_schuur_temp=" + temp + "&humtemp_tuin_humid=" + humd2 + "&humtemp_tuin_temp=" + temp2 + "&tijd=" + "14:25:00" + "&waterdruk=" + waterdruk + "";
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded";
  String postData = httpRequestData;

  client.post("/post-esp-data.php", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  String response = client.responseBody();
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait 10 seconds");
  delay(60000);
  }

I know the code is not tidy yet, but it WORKS !I also made a mistake with trying to get a fixed ip adress , that was't working...

Pffffff.. 3.5 days... Thanks to you i can get on with my project.

Greetings Scoobie

Thank you for pointing me where i needed to be!!!