Submitting data via POST - madavi vs json

Hi,
Long story short - I’m rebuilding my sensor as a esphome device and stuck on posting temperature to madavi - no issues with PM. Sending to sensor.community & openSenseMap works like a charm.

Am I missing some prefixes or smth?

Working piece of code posting PM:

      - http_request.post:
          url: https://api-rrd.madavi.de/data.php
          request_headers:
            X-Pin: "1"
            X-Sensor: "esp8266-6179941"
            Content-Type: "application/json"
          body: !lambda |-
            return "{\"software_version\": \"esphome-custom\","
                  "\"sensordatavalues\": ["
                  "{\"value_type\": \"SDS_P1\", \"value\": \"" + to_string(id(latest_PM10)) + "\"},"
                  "{\"value_type\": \"SDS_P2\", \"value\": \"" + to_string(id(latest_PM25)) + "\"}"
                  "]}";

Not working piece of code posting T+H:

     - http_request.post:
         url: https://api-rrd.madavi.de/data.php
         request_headers:
           X-Pin: "7"
           X-Sensor: "esp8266-6179941"
           Content-Type: "application/json"
         body: !lambda |-
           return "{\"software_version\": \"esphome-custom\","
                 "\"sensordatavalues\": ["
                 "{\"value_type\": \"temperature)\", \"value\": \"" + to_string(id(latest_temperature)) + "\"},"
                 "{\"value_type\": \"humidity)\", \"value\": \"" + to_string(id(latest_humidity)) + "\"}"
                 "]}";

Madavi thinks my sensor is DHT22 - as far as I know, this type doesn’t need prefixes. (Tried with “SHT3x_temperature” and others). Any clues?

Here what I found:

SC server:

url: https://api.sensor.community/v1/push-sensor-data/
method: POST
content_type: ‘application/json’
headers:
X-Pin: 1
X-Sensor: Board ID

{
  "software_version": "<WHAT YOU WANT>", 
   "sensordatavalues":[
    {"value_type":"P1","value":"<PM10>"},
    {"value_type":"P2","value":"<PM2.5>"}
  ]
} 

url: https://api.sensor.community/v1/push-sensor-data/
method: POST
content_type: ‘application/json’
headers:
X-Pin: 11
X-Sensor: Board ID

{
  "software_version": "<WHAT YOU WANT>", 
   "sensordatavalues":[
     {"value_type":"temperature","value":"<TEMP>"},
     {"value_type":"pressure","value":"<PRESS>"},
     {"value_type":"humidity","value":"<HUMI>"}
  ]
} 

Madavi server:
url: https://api-rrd.madavi.de/data.php
method: POST
content_type: ‘application/json’
headers:
X-Pin: 1
X-Sensor: Board ID

{
  "software_version": "<WHAT YOU WANT>", 
   "sensordatavalues":[
    {"value_type":"SDS_P1","value":"<PM10>"},
    {"value_type":"SDS_P2","value":"<PM2.5>"}
  ]
} 

url: https://api-rrd.madavi.de/data.php
method: POST
content_type: ‘application/json’
headers:
X-Pin: 11
X-Sensor: Board ID

{
  "software_version": "<WHAT YOU WANT>", 
   "sensordatavalues":[
     {"value_type":"BME280_temperature","value":"<TEMP>"},
     {"value_type":"BME280_pressure","value":"<PRESS>"},
     {"value_type":"BME280_humidity","value":"<HUMI>"}
  ]
} 
  • HPM/PMS/SDS011/SPS30 => Pin 1
  • BME280 => Pin 11
  • BMP180/BMP280 => Pin 3
  • DHT22/HTU21D/SHT3x => Pin 7
  • GPS(Neo-6M) => Pin 9
  • DS18B20 => Pin 13
  • DNMS => Pin 15

You are right pin 7 :slight_smile:

In the code:

add_Value2Json(s, F("SHT3X_temperature"), FPSTR(DBG_TXT_TEMPERATURE), last_value_SHT3X_T);
add_Value2Json(s, F("SHT3X_humidity"), FPSTR(DBG_TXT_HUMIDITY), last_value_SHT3X_H);

Should be this. See the case. It should be case sensitive.

Unfortunately, adding “SHT3X_” does nothing.
Similar code for the DHT22 doesn’t use any prefix:

			add_Value2Json(s, F("temperature"), FPSTR(DBG_TXT_TEMPERATURE), last_value_DHT_T);
			add_Value2Json(s, F("humidity"), FPSTR(DBG_TXT_HUMIDITY), last_value_DHT_H);

BTW. Do you think the sensor in the legend (madavi) can be changed or am I stuck to DHT?

@ricki-z Help! It should work with SHT3X_…

Got it! Thanks for help. I changed 2 things:

  1. Formatting in my json - it shouldn’t change a thing cuz PM data was transmitted with no issues before
  2. Updated data transmission to send values with precision up to two decimal places for temperature and single decimal place for humidity. Before that, I was sending values like “22.9382615”.

My Graph on madavi changed a bit - it answers one of my questions :smiley:

In case anyone struggle with esphome, here is my full script.

esphome:
  name: stacja-pogodowa-2
  friendly_name: stacja-pogodowa-2
  on_boot: 
    priority: 500
    then:
    - delay: 15s #got some issues with powering up, it fixes them for me

esp8266:
  board: nodemcu

# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: "Sexxxxxxxxxxxxxxxxxxxxxxxxxx0="

ota:
  - platform: esphome
    password: "3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Stacja-Pogodowa-2"
    password: "FxxxxxxxxxJ"

captive_portal:
    
http_request:
  verify_ssl: false
uart:
  rx_pin: RX
  tx_pin: TX
  baud_rate: 9600

i2c:
  sda: D2
  scl: D1

sensor:
  - platform: sds011
    pm_2_5:
      name: "PM 2.5µm Stacja-2"
      on_value:
        then:
         - lambda: |-
              char buf[8];
              snprintf(buf, sizeof(buf), "%.2f", x);
              id(latest_PM25) = std::string(buf);
    pm_10_0:
      name: "PM 10.µm Stacja-2"
      on_value:
        then:
         - lambda: |-
              char buf[8];
              snprintf(buf, sizeof(buf), "%.2f", x);
              id(latest_PM10) = std::string(buf);
         - script.execute: send_pm_data
    update_interval: 8min

  - platform: hdc1080
    temperature:
      name: "Temperatura Warzymice"
      on_value:
        then:
         - lambda: |-
              char buf[8];
              snprintf(buf, sizeof(buf), "%.2f", x);
              id(latest_temperature) = std::string(buf);
    humidity:
      name: "Wilgotność Warzymice"
      on_value:
        then:
         - lambda: |-
              char buf[8];
              snprintf(buf, sizeof(buf), "%.1f", x);
              id(latest_humidity) = std::string(buf);
         - script.execute: send_env_data
    address: 0x40
    update_interval: 1min

  ##    HPM/PMS/SDS011/SPS30 => Pin 1
  ##    BME280 => Pin 11
  ##    BMP180/BMP280 => Pin 3
  ##    DHT22/HTU21D/SHT3x => Pin 7
  ##    GPS(Neo-6M) => Pin 9
  ##    DS18B20 => Pin 13
  ##    DNMS +> Pin 15

script:
  - id: send_pm_data
    then:
      - delay: 15s
     
      - http_request.post:
          url: https://api.sensor.community/v1/push-sensor-data/
          request_headers:
            X-Pin: "1"
            X-Sensor: "esp8266-6xxx941"
            Content-Type: "application/json"
          body: !lambda |-
            return R"({
              "software_version": "esphome-custom",
              "sensordatavalues": [
                {"value_type": "P1", "value": ")" + id(latest_PM10) + R"("},
                {"value_type": "P2", "value": ")" + id(latest_PM25) + R"("}
              ]
            })";
                  
      - http_request.post:
          url: https://api-rrd.madavi.de/data.php
          request_headers:
            X-Pin: "1"
            X-Sensor: "esp8266-61xxxx1"
            Content-Type: "application/json"
          body: !lambda |-
            return R"({
              "software_version": "esphome-custom",
              "sensordatavalues": [
                {"value_type": "SDS_P1", "value": ")" + id(latest_PM10) + R"("},
                {"value_type": "SDS_P2", "value": ")" + id(latest_PM25) + R"("}
              ]
            })";

      - http_request.post:
          url: https://api.opensensemap.org/boxes/68xxxxxxxxxxxxxxxxxx7d/data
          request_headers:
            Content-Type: "application/json; charset=utf-8"
          body: !lambda |-
            return R"([
              {"sensor": "68xxxxxxxxxxxxxxxxxxxxxx80", "value": ")" + id(latest_PM10) + R"("},
              {"sensor": "68xxxxxxxxxxxxxxxxxxxxxx81", "value": ")" + id(latest_PM25) + R"("}
            ])";

  - id: send_env_data
    then:
      - delay: 10s

      - http_request.post:
          url: https://api.sensor.community/v1/push-sensor-data/
          request_headers:
            X-Pin: "7"
            X-Sensor: "esp8266-6xxxxx1"
            Content-Type: "application/json"
          body: !lambda |-
            return R"({
              "software_version": "esphome-custom",
              "sensordatavalues": [
                {"value_type": "temperature", "value": ")" + id(latest_temperature) + R"("},
                {"value_type": "humidity", "value": ")" + id(latest_humidity) + R"("}
              ]
            })";

      - http_request.post:
          url: https://api-rrd.madavi.de/data.php
          request_headers:
            X-Pin: "7"
            X-Sensor: "esp8266-6xxxxx1"
            Content-Type: "application/json"
          body: !lambda |-
            return R"({
              "software_version": "esphome-custom",
              "sensordatavalues": [
                {"value_type": "SHT3X_temperature", "value": ")" + id(latest_temperature) + R"("},
                {"value_type": "SHT3X_humidity", "value": ")" + id(latest_humidity) + R"("}
              ]
            })";

      - http_request.post:
          url: https://api.opensensemap.org/boxes/68xxxxxxxxxxxxxe7d/data
          request_headers:
            Content-Type: "application/json; charset=utf-8"
          body: !lambda |-
            return R"([
              {"sensor": "68xxxxxxxxxxxxxxxxxxxxxx7e", "value": ")" + id(latest_temperature) + R"("},
              {"sensor": "68xxxxxxxxxxxxxxxxxxxxxx7f", "value": ")" + id(latest_humidity) + R"("}
            ])";

globals:
  - id: latest_temperature
    type: std::string
    restore_value: no
    initial_value: '"unspecified"'

  - id: latest_humidity
    type: std::string
    restore_value: no
    initial_value: '"unspecified"'

  - id: latest_PM10
    type: std::string
    restore_value: no
    initial_value: '"unspecified"'

  - id: latest_PM25
    type: std::string
    restore_value: no
    initial_value: '"unspecified"'