Code examples!

User avatar
Bluelight
Posts: 2
Joined: Fri May 14, 2021 4:44 pm

Code examples!

Postby Bluelight » Fri May 14, 2021 5:14 pm

Hello! Can someone help me put together code for a PUT request to HTTPS for my ESP32 DevKit, where I just want to send some temperature data? I'm learning code as I go, but have some problems putting it all together. I also want to use HTTPS without the cert or fingerprint. I don't worry to much if a middle man gets my temperature data. I want to use the on-board WiFi controller and it needs to be a PUT request using the URL. The following code is just the sensor posting to the Serial Monitor. I want to send the data to my website using HTTPS with no cert using a PUT request (webserver has a PHP file with a GET request already working). Would be nice if code had comments and was optimized and without memory leaks (since it's C++ which doesn't have garbage collection). I see some examples uses WiFi.h and some use WiFiMulti.h, and some use WiFiClientSecure.h, HTTPClient.h and HTTPClientSecure.h. I don't really know what library is needed for this task. Thanks in advance!
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BME280.h>
  4.  
  5. #define SEALEVELPRESSURE_HPA (1013.25)
  6.  
  7. Adafruit_BME280 bme; // I2C
  8.  
  9. unsigned long delayTime;
  10.  
  11. void setup() {
  12.     Wire.begin();
  13.     Serial.begin(115200);
  14.     while(!Serial);    // time to get serial running
  15.     Serial.println(F("BME280 test"));
  16.  
  17.     unsigned status;
  18.    
  19.     // default settings
  20.     status = bme.begin(0x76);  
  21.     // You can also pass in a Wire library object like &Wire2
  22.     //status = bme.begin(0x76, &Wire2)
  23.     if (!status) {
  24.         Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  25.         Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
  26.         Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  27.         Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  28.         Serial.print("        ID of 0x60 represents a BME 280.\n");
  29.         Serial.print("        ID of 0x61 represents a BME 680.\n");
  30.         while (1) delay(10);
  31.     }
  32.    
  33.     Serial.println("-- Default Test --");
  34.     delayTime = 1000;
  35.  
  36.     Serial.println();
  37. }
  38.  
  39.  
  40. void loop() {
  41.     printValues();
  42.     delay(delayTime);
  43. }
  44.  
  45.  
  46. void printValues() {
  47.     Serial.print("Temperature = ");
  48.     Serial.print(bme.readTemperature());
  49.     Serial.println(" *C");
  50.  
  51.     Serial.print("Pressure = ");
  52.     Serial.print(bme.readPressure() / 100.0F);
  53.     Serial.println(" hPa");
  54.  
  55.     Serial.print("Approx. Altitude = ");
  56.     Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  57.     Serial.println(" m");
  58.  
  59.     Serial.print("Humidity = ");
  60.     Serial.print(bme.readHumidity());
  61.     Serial.println(" %");
  62.  
  63.     Serial.println();
  64. }

Current PUT request data needed to be sent are something along the lines of:
  1.     // send the HTTP PUT request
  2.     client.print(F("GET /sensors/write_data.php"));
  3.     client.print("?temperatur=");
  4.     client.print(bme.readTemperature());
  5.     client.print("&fuktighet=");
  6.     client.print(bme.readHumidity());
  7.     client.print("&rssi=");
  8.     client.print(rssi);
  9.     client.println(F(" HTTP/1.0"));
  10.     client.println(F("Host: my-website.net"));
  11.     client.println("Connection: close");
  12.     client.println();
  13.     client.stop();
Bluelight

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: Code examples!

Postby lbernstone » Sat May 15, 2021 1:25 pm

https://github.com/espressif/arduino-es ... Client.ino

You will need to change the GET to a PUT, and include your form data as a parameter.

User avatar
Bluelight
Posts: 2
Joined: Fri May 14, 2021 4:44 pm

Re: Code examples!

Postby Bluelight » Thu May 27, 2021 6:19 pm

I still don't understand how to do this! I need a "GET" as the website needs to be loaded, and then put the data in the URL. So the ESP32 should connect and load the webpage and then close the connection, where the URL to the page to GET will define the data to be PUT. Like using dweet.io or freeboard.

So the ESP should create a page request with the following example URL: "https://www.mysite.com/sensors/write_data.php?temperatur=24&fuktighet=40"

So far I've learned that this also have to be set to HTTP/1.0, as the newer ones are not supported.

The temperature and humidity is in the URL of a get request, sot that the webpage can get it from the URL.

If not showing in an example, I just need to know where in the ESP32 httpclient example I need to put the temperature and humidity data parameters. And also how to connect to the website using HTTPS without cert or fingerprint. I do not have the option to hardcode this every time it changes, which should be obvious to those who create this library.

I will look at other ways of sending data later, but for now I just want this to work!

A full code example would be very useful and I would very much appreciate it!
Bluelight

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: Code examples!

Postby ullixesp » Fri May 28, 2021 9:06 am

Find an example, which should be running on an ESP32 as is, as attchment to this post:
https://github.com/me-no-dev/ESPAsyncWe ... -849659026

It is made for a specific, different purpose, but the ESP gets data to and from the web.

Who is online

Users browsing this forum: Basalt and 44 guests