未分類」カテゴリーアーカイブ

M5Stack + MHZ19C + WiFi (Local/http)

TODO あとでちゃんと書きなおす(余裕があれば)

飛ばす方 ソース

#include <M5Stack.h>
#include <MHZ19.h>
#include <WiFi.h>
#include <HTTPClient.h>

#define RX 16
#define TX 17
#define INTERVAL 6

const char* ssid     = "XXXXXXX";
const char* password = "XXXXXXX";

MHZ19 myMHZ19;

HardwareSerial  mySerial(1);

void setup(){

  M5.begin();
  M5.Power.begin();
  M5.Lcd.setTextSize(5);
  M5.Lcd.print("start");

  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.print(".");
  }
  M5.Lcd.println("\r\n connected");
  M5.Lcd.print(WiFi. localIP());

  mySerial.begin(9600,SERIAL_8N1,RX,TX);
  delay(100);
  myMHZ19.begin(mySerial);
  myMHZ19.autoCalibration(false);

  delay(2000);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
    displayCo2(co2);
    doPost(co2);
  } else {
     M5.Lcd.print(".");
  }
  count++;
  delay(1000);
}

void displayCo2(int co2) {
  M5.Lcd.clear(); 
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.println("CO2 ppm");
  M5.Lcd.print(co2);
}

void doPost(int co2){
  char json[30];
  sprintf(json, "{\"co2\": \"%d\"}", co2);
  Serial.println(json);
  
  HTTPClient http;
  http.begin("http://192.168.1.3:8081/");
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.POST(json);
  delay(1000);
  Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  http.end();
     
}

受けるほう ソース index.ts (雰囲気)

import express from 'express'
import moment from 'moment'


const app: express.Express = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'))

app.listen(8081, () => {
  console.log('Running at Port 8081...')
})


app.post('/', (req, res)=> {
  console.log("call")

  // console.log(moment().format())
  console.log(req.body )
  res.send('OK')
})

app.use((req, res) => {
  res.sendStatus(404)
})

URLの指定で末尾の/が抜けているとM5Stackがエラー&リブートを繰り返した。結構はまった。

試験用の配線は前のブログ参照。まだ、試験用でMHZへの電力がたぶんたりてないはず。

MH-Z19CとM5Stack(basic)の接続

(2021.4.27追記)MHZ19というライブラリを使っていますがMHZ19B用に調整された処理が入っているのでCを使う場合は、MHZ19_uartというライブラリの方が良さそうです。

githubにソースあげておきました。(github)

とり急ぎ、MH-Z19CとM5Stackを直結で動かす。機能は表示のみ。電圧は満たしていないのでソフトがプログラムが動く確認までの目的。電源まわりはきちんと作り直さないと数値は大体。(それでも多分うちのかDM306以外の一万円未満のセンサーよりはよいきがする。これで5/1時点でCO2-miniかうちの以外の1万円未満センサーよりは性能良い)

MH-Z19Cは5Vプラマイ0.1Vを要求して守らないと値が不安定になる。「ゆうても大丈夫だろ」と雑にやるとずれます。

配線は、左がM5Stackで右がMH-Z19Cで以下のようにつなぐ。16,17はプログラムで指定しているので変更可能。

  • 5V からVIN
  • G から GND
  • 16 から TX
  • 17 から RX

プログラム。いつもどおりMITライセンスです(ライブラリはLGPL使っているので書き込んだらLGPL)

#include <M5Stack.h>
#include <MHZ19.h>

#define RX 16
#define TX 17
#define INTERVAL 6

MHZ19 myMHZ19;

HardwareSerial  mySerial(1);

void setup(){

  M5.begin();
  M5.Power.begin();
  M5.Lcd.setTextSize(5);
  M5.Lcd.print("start");

  mySerial.begin(9600,SERIAL_8N1,RX,TX);
  delay(100);
  myMHZ19.begin(mySerial);
  myMHZ19.autoCalibration(false);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
  }
  displayCo2(co2, (count % 2));
  count++;
  delay(1000);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
    displayCo2(co2 );
  } else {
     M5.Lcd.print(".");
  }
  count++;
  delay(1000);
}

void displayCo2(int co2) {
  M5.Lcd.clear(); 
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.println("CO2 ppm");
  M5.Lcd.print(co2);
}

EPEA-CO2-NDIR-04 Rev3.0.9のソース

基本、自分用メモ

~1/22 出荷分

#include <FaBoLCDmini_AQM0802A.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MHZ19.h>

#define SRX 0
#define STX 1
#define INTERVAL 6

MHZ19 myMHZ19;

SoftwareSerial softSerial(SRX, STX);
FaBoLCDmini_AQM0802A lcd;

void setup()
{

  Wire.begin();
  lcd.begin();
  displayLCD("Start");
  delay(1000);
  
  softSerial.begin(9600);
  delay(100);
  myMHZ19.begin(softSerial);
  myMHZ19.autoCalibration(false);
  delay(1000);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
  }
  displayCo2(co2, (count % 2));
  count++;
  delay(1000);
}

void displayLCD(String message) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(message);
}

void displayCo2(int co2, bool isPresiod) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CO2 ppm");
  lcd.setCursor(0, 1);
  lcd.print(co2);
  if (isPresiod) {
    lcd.print(" .");
  }
}

1/22〜

#include <FaBoLCDmini_AQM0802A.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MHZ19.h>

#define SRX 0
#define STX 1
#define INTERVAL 6

MHZ19 myMHZ19;

SoftwareSerial softSerial(SRX, STX);
FaBoLCDmini_AQM0802A lcd;

void setup()
{
  checkLED(1000);
  
  Wire.begin();
  lcd.begin();
  displayLCD("Start");
  delay(1000);
  
  checkLED(500);
  
  softSerial.begin(9600);
  delay(100);
  myMHZ19.begin(softSerial);
  myMHZ19.autoCalibration(false);
  delay(1000);
}

void checkLED(int mills) {
  int PIN = 13;
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN, HIGH);
  delay(1000);
  digitalWrite(PIN, LOW);
  delay(mills);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
  }
  displayCo2(co2, (count % 2));
  count++;
  delay(1000);
}

void displayLCD(String message) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(message);
}

void displayCo2(int co2, bool isPresiod) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CO2 ppm");
  lcd.setCursor(0, 1);
  lcd.print(co2);
  if (isPresiod) {
    lcd.print(" .");
  }
}

2/22~

#include <FaBoLCDmini_AQM0802A.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MHZ19.h>

#define SRX 0
#define STX 1
#define INTERVAL 6

MHZ19 myMHZ19;

SoftwareSerial softSerial(SRX, STX);
FaBoLCDmini_AQM0802A lcd;

void setup()
{
  checkLED(1000);
  
  Wire.begin();
  delay(100);
  lcd.begin();
  delay(100);
  displayLCD("Start");
  delay(1000);
  
  checkLED(500);
  
  softSerial.begin(9600);
  delay(100);
  myMHZ19.begin(softSerial);
  myMHZ19.autoCalibration(false);
  delay(1000);
}

void checkLED(int mills) {
  int PIN = 13;
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN, HIGH);
  delay(1000);
  digitalWrite(PIN, LOW);
  delay(mills);
}

int count = 0;
int co2 = 0;
void loop()
{
  if ((count % INTERVAL) == 0 ) {
    co2 = myMHZ19.getCO2();
  }
  displayCo2(co2, (count % 2));
  count++;
  delay(1000);
}

void displayLCD(String message) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(message);
}

void displayCo2(int co2, bool isPresiod) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CO2 ppm");
  lcd.setCursor(0, 1);
  lcd.print(co2);
  if (isPresiod) {
    lcd.print(" .");
  }
}

AWSでPSQLが入っていなかったのでyumインストール

AWSでPSQLが入っていなかったのでyumインストール。

結論としては特にはまるポイントもなく通常通りでいけた。

(青字が入力)

$yum search postgresql
Loaded plugins: priorities, update-motd, upgrade-helper
================================================================= N/S matched: postgresql ==================================================================
(中略)
postgresql92.x86_64 : PostgreSQL client programs
postgresql92-contrib.x86_64 : Extension modules distributed with PostgreSQL
postgresql92-devel.x86_64 : PostgreSQL development header files and libraries
postgresql92-docs.x86_64 : Extra documentation for PostgreSQL
postgresql92-libs.i686 : The shared libraries required for any PostgreSQL clients
postgresql92-libs.x86_64 : The shared libraries required for any PostgreSQL clients
postgresql92-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql92-plpython26.x86_64 : The Python2 procedural language for PostgreSQL
postgresql92-plpython27.x86_64 : The Python3 procedural language for PostgreSQL
postgresql92-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql92-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql92-server-compat.x86_64 : PostgreSQL server config files for backward compatibility
postgresql92-test.x86_64 : The test suite distributed with PostgreSQL
postgresql93.x86_64 : PostgreSQL client programs
postgresql93-contrib.x86_64 : Extension modules distributed with PostgreSQL
(中略)

Name and summary matches only, use “search all” for everything.

家の環境の最新クライアント(psql)がpostgresql93.x86_64ぽいので確認

$ yum info postgresql93.x86_64
Loaded plugins: priorities, update-motd, upgrade-helper
Available Packages
Name : postgresql93
Arch : x86_64
Version : 9.3.9
Release : 1.58.amzn1
Size : 4.2 M
Repo : amzn-updates/2015.03
Summary : PostgreSQL client programs
URL : http://www.postgresql.org/
License : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management system (DBMS).
: The base postgresql package contains the client programs that you’ll need to
: access a PostgreSQL DBMS server, as well as HTML documentation for the whole
: system. These client programs can be located on the same machine as the
: PostgreSQL server, or on a remote machine that accesses a PostgreSQL server
: over a network connection. The PostgreSQL server can be found in the
: postgresql-server sub-package.

あっていたのでインストール

$ sudo yum install postgresql93.x86_64

(中略)

Complete!

$ psql -h ホスト名 -U ユーザ名 -d DB名

つながった。

 

効果でてきたかな??

「ボルダリング」のみで京都市より検索したら5番目に表示されていました。日によって変動あるけどこれぐらいの位置に表示されるのはめったになかったのでSEOの効果出てきたかな??

「ボルダリング 京都」でも150番~300番ぐらいとかなり下の方とはいえ店のページが出てくるようになってきた。今のところ、順調にいっているみたい。

 

ボルダリングでの検索結果 seo_20160712_bol

ボルダリング+京都での検索結果 seo_20160712

spam対策

ここ数日198.245.49.39と137.175.15.131から数時間おきにspamコメントが寄せられるうになってきた。

ipは決まっているのでとりあえずiptablesではじくように設定。

iptables -I INPUT -s 198.245.49.39 -j DROP
iptables -I INPUT -s 137.175.15.131 -j DROP
/etc/rc.d/init.d/iptables save
(各IPからのパケットをはじく設定&保存。)

今のところは、他のIPからのスパムはないから出てきたベースで拒否していけばいいけど頻度増えてきたらAkismetとかのプラグインではじいた方がいいのかな。

スタックトレースを頼りに海外から来てくれている人もいるので国でばっさりと絞りたくはないし。

(2013/8/6までにスパムが来たIP)

216.244.85.234
205.164.24.90
58.22.70.108
137.175.15.134
185.25.48.24
198.2.200.9
137.175.105.39
137.175.118.101
137.175.15.133
137.175.68.241
137.175.105.38
114.251.150.133
123.184.133.84
163.125.181.151
137.175.15.132
137.175.118.100
137.175.68.178
71.61.84.216
117.27.138.148
137.175.118.99
198.27.82.114
137.175.15.131
198.245.49.39
198.100.144.195