1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Arduino で空気品質(+CO2濃度)を測定して json サーバーとして値を返す

Last updated at Posted at 2021-02-15

Arduino で空気品質(+CO2濃度)を測定して json サーバーとして値を返します。

サンプルコードを実行して、http://192.168.0.117/ にアクセスしますと以下のような値が取得できます。
Screenshot from 2021-02-15 21-43-35.png
使用機材

項目 機材名
使用しているArduino arduino nano
書き込みブートローダ atmega328old
LAN シールド ENC28J60
空気品質センサー MQ135

GPIO端子の接続設定

|センサー端子|Arduino端子|
|---|---|---|
|A0|A0|
|Vcc|3.3V|
|GND|GND|

ソースコードはこちら

SensorJsonServer.ino
# include <UIPEthernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE  };  
IPAddress ip(192, 168, 0, 117);
IPAddress myDns(192, 168, 0, 1);                        

EthernetServer server(80);

int valueA0;

void setup(){

  Serial.begin(9600);      // sets the serial port to 9600

  // start ip address
  Ethernet.begin(mac, ip , myDns );

  // start server
  server.begin();

}

void loop(){

  EthernetClient client = server.available();

  if (client){  
    
    boolean currentLineIsBlank = true;

    while (client.connected()){
      
      if (client.available()){
        
        char c = client.read();

        if (c == '\n' && currentLineIsBlank){

          valueA0 = analogRead(A0);
          Serial.print(valueA0, DEC);
          Serial.println("ppm");
          
          float mq135_ro = mq135_getro( ( 2000.0 * ( 1023 - valueA0 ) / valueA0 ) , 399 );
          Serial.println( mq135_ro );

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: application/json");
          client.println();
          /*
          const char index_html[] PROGMEM = R"rawliteral(
[
  {"id" : 0, "name" : "MQ135" , "gpio" : "A0" , "value" : "valueA0" , "unit" : "ppm" }
]
)rawliteral";
          client.print(index_html);
          */
          client.println("[");
          client.println("  {\"id\" : 0, \"sensor\" : \"MQ135\" , \"gpio\" : \"A0\" , \"type\" : \"air_quality\" , \"value\" : " +  String( valueA0 )      + " , \"unit\" : \"ppm\" } , " );
          client.println("  {\"id\" : 1, \"sensor\" : \"MQ135\" , \"gpio\" : \"A0\" , \"type\" : \"co2_percent\" , \"value\" : " +  String(mq135_ro , 0 )  + " , \"unit\" : \"%\"   }" );
          client.println("]");
          break;
        
        }

        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r'){
          currentLineIsBlank = false;
        }

      }

    }

     delay(10);

    client.stop();
    //Serial.println("   Disconnected\n");

  }

}

//
long mq135_getro(long resvalue, double ppm) {
  return (long)( resvalue * exp( log( 116.6020682 / ppm ) /  -2.769034857  ) );
}

php で読み取れることを確認します。

php ソースコード

json_print.php
<?php

$json     = file_get_contents( "http://192.168.0.117/" );
$json_arr = json_decode( $json , true );
var_dump( $json_arr );

実行

php json_print.php ;

結果

array(2) {
  [0]=>
  array(6) {
    ["id"]=>
    int(0)
    ["sensor"]=>
    string(5) "MQ135"
    ["gpio"]=>
    string(2) "A0"
    ["type"]=>
    string(11) "air_quality"
    ["value"]=>
    int(1014)
    ["unit"]=>
    string(3) "ppm"
  }
  [1]=>
  array(6) {
    ["id"]=>
    int(1)
    ["sensor"]=>
    string(5) "MQ135"
    ["gpio"]=>
    string(2) "A0"
    ["type"]=>
    string(11) "co2_percent"
    ["value"]=>
    int(26)
    ["unit"]=>
    string(1) "%"
  }
}

CO2濃度も一応記述していますが、自信が無いので詳しいかたご教授お願いいたしますm(_ _)m

参考リンク

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?