最終更新日:2023/3/11
インクルードするライブラリですが、Ethernet3を使用します。このライブラリは安定していると思います。
TM1637用のポートは確保します。
オリジナルのスケッチは圧力センサSCP1000を使用していますが、一般的とは云えないと考えました。
多分、ボッシュ社のBME280が良いと思いM5StackのENV2を検討しました。しかしENV2は生産が終わっており、現状ENV3になっています。ここで採用されているQMP6988はライブラリを見つけるのが大変です。
/* SCP1000 Barometric Pressure Sensor Display Serves the output of a Barometric Pressure Sensor as a web page. Uses the SPI library. For details on the sensor, see: http://www.sparkfun.com/commerce/product_info.php?products_id=8161 http://www.vti.fi/en/support/obsolete_products/pressure_sensors/ This sketch adapted from Nathan Seidle's SCP1000 example for PIC: http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip Circuit: SCP1000 sensor attached to pins 6,7, and 11 - 13: DRDY: pin 6 CSB: pin 7 MOSI: pin 11 MISO: pin 12 SCK: pin 13 created 31 July 2010 by Tom Igoe */ #include <Ethernet3.h> // the sensor communicates using SPI, so include the library: #include <SPI.h> // assign a MAC address for the ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // assign an IP address for the controller: IPAddress ip(192, 168, 1, 20); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); //Sensor's memory register addresses: const int PRESSURE = 0x1F; //3 most significant bits of pressure const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure const int TEMPERATURE = 0x21; //16 bit temperature reading // pins used for the connection with the sensor // the others you need are controlled by the SPI library): const int dataReadyPin = 6; const int chipSelectPin = 7; float temperature = 0.0; long pressure = 0; long lastReadingTime = 0; void setup() { // start the SPI library: SPI.begin(); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); // initalize the data ready and chip select pins: pinMode(dataReadyPin, INPUT); pinMode(chipSelectPin, OUTPUT); Serial.begin(9600); //Configure SCP1000 for low noise configuration: writeRegister(0x02, 0x2D); writeRegister(0x01, 0x03); writeRegister(0x03, 0x02); // give the sensor and Ethernet shield time to set up: delay(1000); //Set the sensor to high resolution mode tp start readings: writeRegister(0x03, 0x0A); } void loop() { // check for a reading no more than once a second. if (millis() - lastReadingTime > 1000) { // if there's a reading ready, read it: // don't do anything until the data ready pin is high: if (digitalRead(dataReadyPin) == HIGH) { getData(); // timestamp the last time you got a reading: lastReadingTime = millis(); } } // listen for incoming Ethernet connections: listenForEthernetClients(); } void getData() { Serial.println("Getting reading"); //Read the temperature data int tempData = readRegister(0x21, 2); // convert the temperature to celsius and display it: temperature = (float)tempData / 20.0; //Read the pressure data highest 3 bits: byte pressureDataHigh = readRegister(0x1F, 1); pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0 //Read the pressure data lower 16 bits: unsigned int pressureDataLow = readRegister(0x20, 2); //combine the two parts into one 19-bit number: pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4; Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" degrees C"); Serial.print("Pressure: " + String(pressure)); Serial.println(" Pa"); } void listenForEthernetClients() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("Got a client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); // print the current readings, in HTML format: client.print("Temperature: "); client.print(temperature); client.print(" degrees C"); client.println("<br />"); client.print("Pressure: " + String(pressure)); client.print(" Pa"); client.println("<br />"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } } //Send a write command to SCP1000 void writeRegister(byte registerName, byte registerValue) { // SCP1000 expects the register name in the upper 6 bits // of the byte: registerName <<= 2; // command (read or write) goes in the lower two bits: registerName |= 0b00000010; //Write command // take the chip select low to select the device: digitalWrite(chipSelectPin, LOW); SPI.transfer(registerName); //Send register location SPI.transfer(registerValue); //Send value to record into register // take the chip select high to de-select: digitalWrite(chipSelectPin, HIGH); } //Read register from the SCP1000: unsigned int readRegister(byte registerName, int numBytes) { byte inByte = 0; // incoming from the SPI read unsigned int result = 0; // result to return // SCP1000 expects the register name in the upper 6 bits // of the byte: registerName <<= 2; // command (read or write) goes in the lower two bits: registerName &= 0b11111100; //Read command // take the chip select low to select the device: digitalWrite(chipSelectPin, LOW); // send the device the register you want to read: int command = SPI.transfer(registerName); // send a value of 0 to read the first byte returned: inByte = SPI.transfer(0x00); result = inByte; // if there's more than one byte returned, // shift the first byte then get the second byte: if (numBytes > 1) { result = inByte << 8; inByte = SPI.transfer(0x00); result = result | inByte; } // take the chip select high to de-select: digitalWrite(chipSelectPin, HIGH); // return the result: return(result); } |
|
/* * 2023/03/14 T.Wanibe * SCP1000 Barometric Pressure Sensor Display * 気圧センサーの出力を Web ページとして提供します。 * W5500-EVB-PICOをターゲットとしています * 回路: * ピン 16..21に接続されたイーサネットchip * I2C0にM5Env3を接続し圧力を取得するように変更しています。 * M5Env3の温度はSHT30(0x44) 圧力はQMP6988(0x70)です * ライブラリはM5が提供するものではなく、センサデバイス単体用のものを使います。 * 最大1044480バイトのフラッシュメモリのうち、スケッチが62172バイト(5%)を使っています。 * 最大262144バイトのRAMのうち、グローバル変数が7944バイト(3%)を使っていて、ローカル変数で254200バイト使うことができます。 */ #include <Ethernet3.h> #include <SPI.h> #include <Wire.h> #include "Adafruit_SHT31.h" #include "QMP6988.h" //UNIT_ENV3に含まれています。 #define SPI_SCK 18 #define SPI_RX 16 #define SPI_TX 19 #define SPI_CS 17 #define NICReset 20 #define HTTPport 80 #define TELNETport 23 #define SerialRate 115200 // コントローラの MAC アドレスと IP アドレスを以下に入力します。 // IP アドレスは、ローカル ネットワークによって異なります。 // ゲートウェイとサブネットはオプションです。 byte mac[] = {0x00,0x08,0xDC,0x54,0x4D,0xE0}; //WIZNET byte ip[] = {192, 168, 0, 210}; byte dns_server[] = {192, 168, 0, 1}; byte gateway[] = {0, 0, 0, 0}; byte subnet[] = {255, 255, 255, 0}; float tmp = 0.0; //なぜかNaNが認められない float hum = 0.0; float pressure = 0.0; float tmpOnPico = 0.0; int count = 0; // イーサネット サーバー ライブラリの初期化 // 使用する IP アドレスとポート(ポート 80 は HTTP のデフォルトです): EthernetServer server(80); Adafruit_SHT31 sht30(&Wire1); QMP6988 qmp6988; //---------------- void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); Wire1.setSDA(6); //RP2040にはI2C0の組み合わせが6通りあるので設定しておくのが無難です。 Wire1.setSCL(7); Wire.begin(); //ENV IIのgroveコネクタ接続pin設定 sht30.begin(0x44); // sht30 I2C address: 0x44 or 0x45 qmp6988.init(0x70,&Wire1); //qmp6988.setpPowermode(QMP6988_NORMAL_MODE); //qmp6988.setFilter(QMP6988_FILTERCOEFF_OFF); // start the SPI library: pinMode(SPI_CS,OUTPUT); pinMode(NICReset,OUTPUT); // イーサネット接続とサーバーを開始します。 SPI.setSCK(SPI_SCK); SPI.setRX(SPI_RX); SPI.setTX(SPI_TX); SPI.setCS(SPI_CS); SPI.begin(); // Ethernet.init(pin)を使用してCSピンを設定できます Ethernet.setCsPin(SPI_CS); Ethernet.setRstPin(NICReset); digitalWrite(NICReset,LOW); delay(10); digitalWrite(NICReset,HIGH); Ethernet.init(SPI_CS); // イーサネット デバイスを初期化する Ethernet.begin(mac, ip); server.begin(); // Open serial communications and wait for port to open: Serial.begin(SerialRate); while (!Serial) { ; // シリアルポートが接続されるのを待ちます。 } Serial.print("server is at "); Serial.println(Ethernet.localIP()); } //------------- void loop() { delay(1000); if(count++ % 2){ digitalWrite(LED_BUILTIN, HIGH); }else{ digitalWrite(LED_BUILTIN, LOW); } tmpOnPico = analogReadTemp(); pressure = qmp6988.calcPressure()/100; //qmp6988のデータを取得します。 tmp = sht30.readTemperature(); //SHT30のデータを取得します。 hum = sht30.readHumidity(); //SHT30から得られた湿度を代入します。 Serial.printf("OnPicoTemp: %2.1f[℃]\nTemp: %2.1f[℃]\nHumi: %2.0f[%%]\nPressure:%5.0f[hPa]\n", tmpOnPico,tmp, hum, pressure); // 着信クライアントをリッスンする EthernetClient client = server.available(); if (client) { Serial.println("new client"); // http リクエストは空白行で終了します boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // 行末まで到達し (改行文字を受信)、行が空白の場合、http 要求は終了しているので、応答を送信できます if (c == '\n' && currentLineIsBlank) { // 標準の http 応答ヘッダーを送信する client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // 接続は応答の完了後に閉じられます client.println("Refresh: 5"); // ページは 5 秒ごとに自動的に更新されます。 client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.print("pressure is "); client.print(pressure); client.print("[hPa]"); client.println("<br />"); client.print("Temp is "); client.print(tmp); client.print("[°C]"); client.println("<br />"); client.print("humi is "); client.print(hum); client.print("[%]"); client.println("<br />"); client.println("</html>"); break; } if (c == '\n') { // あなたは新しい行を始めています currentLineIsBlank = true; }else if (c != '\r') { // 現在の行の文字を取得しました currentLineIsBlank = false; } } } // Web ブラウザにデータを受信する時間を与える delay(1); // 接続を閉じます: client.stop(); Serial.println("client disconnected"); } } |
本ソフトウエアは、あなたに対して何も保証しません。本ソフトウエアの関係者(他の利用者も含む)は、あなたに対して一切責任を負いません。
あなたが、本ソフトウエアを利用(コンパイル後の再利用など全てを含む)する場合は、自己責任で行う必要があります。本ソフトウエアの著作権はToolsBoxに帰属します。
本ソフトウエアをご利用の結果生じた損害について、ToolsBoxは一切責任を負いません。
ToolsBoxはコンテンツとして提供する全ての文章、画像等について、内容の合法性・正確性・安全性等、において最善の注意をし、作成していますが、保証するものではありません。
ToolsBoxはリンクをしている外部サイトについては、何ら保証しません。
ToolsBoxは事前の予告無く、本ソフトウエアの開発・提供を中止する可能性があります。
Microsoft、Windows、WindowsNTは米国Microsoft Corporationの米国およびその他の国における登録商標です。
Windows Vista、Windows XPは、米国Microsoft Corporation.の商品名称です。
LabVIEW、National Instruments、NI、ni.comはNational Instrumentsの登録商標です。
I2Cは、NXP Semiconductors社の登録商標です。
その他の企業名ならびに製品名は、それぞれの会社の商標もしくは登録商標です。
すべての商標および登録商標は、それぞれの所有者に帰属します。