Code examples!
Posted: 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!
Current PUT request data needed to be sent are something along the lines of:
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #define SEALEVELPRESSURE_HPA (1013.25)
- Adafruit_BME280 bme; // I2C
- unsigned long delayTime;
- void setup() {
- Wire.begin();
- Serial.begin(115200);
- while(!Serial); // time to get serial running
- Serial.println(F("BME280 test"));
- unsigned status;
- // default settings
- status = bme.begin(0x76);
- // You can also pass in a Wire library object like &Wire2
- //status = bme.begin(0x76, &Wire2)
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
- Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
- Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
- Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
- Serial.print(" ID of 0x60 represents a BME 280.\n");
- Serial.print(" ID of 0x61 represents a BME 680.\n");
- while (1) delay(10);
- }
- Serial.println("-- Default Test --");
- delayTime = 1000;
- Serial.println();
- }
- void loop() {
- printValues();
- delay(delayTime);
- }
- void printValues() {
- Serial.print("Temperature = ");
- Serial.print(bme.readTemperature());
- Serial.println(" *C");
- Serial.print("Pressure = ");
- Serial.print(bme.readPressure() / 100.0F);
- Serial.println(" hPa");
- Serial.print("Approx. Altitude = ");
- Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
- Serial.println(" m");
- Serial.print("Humidity = ");
- Serial.print(bme.readHumidity());
- Serial.println(" %");
- Serial.println();
- }
Current PUT request data needed to be sent are something along the lines of:
- // send the HTTP PUT request
- client.print(F("GET /sensors/write_data.php"));
- client.print("?temperatur=");
- client.print(bme.readTemperature());
- client.print("&fuktighet=");
- client.print(bme.readHumidity());
- client.print("&rssi=");
- client.print(rssi);
- client.println(F(" HTTP/1.0"));
- client.println(F("Host: my-website.net"));
- client.println("Connection: close");
- client.println();
- client.stop();