je8vgnのアイコン画像
je8vgn 2024年01月28日作成 (2024年01月31日更新)
製作品 製作品 閲覧数 104
je8vgn 2024年01月28日作成 (2024年01月31日更新) 製作品 製作品 閲覧数 104

水槽監視システム Version2の製作

水槽監視システム Version2の製作

はじめに

今回、SPRESENSE™ 活用コンテストをきっかけに以前製作した水槽監視システム(Aquarium Environment Controller)をリプレースしました。

製作

SPRESENSEを使う事により1/5の大きさになりました。また、PICを使用しないのでプログラマブルに設定を変更できます。

回路図

キャプションを入力できます

ブラウザによるアクセス画面

キャプションを入力できます

ソースコード

AEC2

#include <SPI.h> // SDcardReady #include <SDHCI.h> SDClass SD; File myFile; // ----------------------------------------------------------------------- // //Add-onボード用インクルード #include "src/Ethernet.h" #include "src/M24C64.h" // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- // //Add-onボード搭載EEPROMへのアクセス用 M24C64 eep; // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- // //Add-onボード用のSPIはSPI5となるため、本ライブラリ使用時は以下の定義が必要(socket.cpp内にて使用) //参考:https://developer.sony.com/develop/spresense/docs/arduino_developer_guide_ja.html#_spi_%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA #define USE_SPRESENSE_SPI5 // ----------------------------------------------------------------------- // // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x98, 0x01 }; IPAddress ip(192, 168, 0, 135); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); bool isI2C(uint8_t address) { Wire.beginTransmission(address); if (Wire.endTransmission() == 0) { return true; } return false; } void setup() { // ----------------------------------------------------------------------- // // W5500-Ether用の初期化方法 digitalWrite(21, LOW); //W5500_Eth RESET# = HIGH delay(500); digitalWrite(21, HIGH); //W5500_Eth RESET# = HIGH Serial.println("W5500 reset done."); Ethernet.init(19); // use I2S_DIN pin for W5500 CS pin // ----------------------------------------------------------------------- // // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Ethernet WebServer Example"); // ----------------------------------------------------------------------- // // W5500-Ether用のMACアドレス取得処理 // I2C device scan Serial.print(F("I2C Devices(0x) : ")); Wire.begin(); //I2C spec. have reserved address!! these scanning escape it address. for (uint8_t ad = 0x08; ad < 0x77; ad++) { if (isI2C(ad)) { Serial.print(ad, HEX); Serial.write(' '); } } Serial.write('\n'); //EEPROM MAC ADDRESS read eep.init(0x57); Serial.println("MAC read from on board eeprom."); for (int i = 0; i < 6; i++) { mac[i] = eep.read(i); Serial.print(mac[i], HEX); Serial.print(":"); } Serial.println(""); // ----------------------------------------------------------------------- // // start the Ethernet connection and the server: Ethernet.begin(mac, ip); // Check for Ethernet hardware present if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } pinMode(8, INPUT_PULLDOWN); pinMode(9, INPUT_PULLDOWN); pinMode(10, INPUT_PULLDOWN); pinMode(11, INPUT_PULLDOWN); // start the server server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } int ledstatus[5] = { 0, 0, 0, 0, 0 }; void loop() { // listen for incoming clients EthernetClient client = server.available(); String readString; if (client) { Serial.println("new client"); // an http request ends with a blank line bool currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (readString.length() < 100) { readString += c; } if (c == '\n' && currentLineIsBlank) { for (int i = 0; i < 5; i++) { String s1 = "?led"; String s2 = "=on"; String s3 = "=off"; if (readString.indexOf(s1 + i + s2) > 0) { digitalWrite(2 + i, HIGH); ledstatus[i] = 1; } if (readString.indexOf(s1 + i + s3) > 0) { digitalWrite(2 + i, LOW); ledstatus[i] = 0; } } // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Cache-Control: no-cache"); client.println("Connection: close"); client.println(); client.println("<html>"); client.println("<h1>spresense IO Test</h1>"); // スイッチ操作 client.println("<table rules=\"all\"><tr><th></th><th>Status</th><th></th><th></th></<tr>"); for (int i = 0; i < 5; i++) { client.println((String) "<tr><td>Switch" + i + (String) "</td><td>"); if (ledstatus[i] == 1) { client.println("on"); } else { client.println("off"); } client.println("</td>"); client.println((String) "<td><button onclick=\"window.location.href='?led" + i + (String) "=on'\">on</button></td>"); client.println((String) "<td><button onclick=\"window.location.href='?led" + i + (String) "=off'\">off</button></td>"); client.println("</tr>"); } client.println("</table>"); client.println("<br/>"); // 温度表示 client.println("<table rules=\"all\"><tr><th></th><th></th></<tr>"); for (int i = 0; i < 5; i++) { client.println((String) "<tr><td>Sensor" + i + (String) "</td><td>"); int sensorReading = analogRead(A0); float tmp = ((float(sensorReading) * 5.0) / 1024.0) * 100.0; client.println("<td>"); client.println(tmp); client.println("</td>"); client.println("</tr>"); } client.println("</table>"); client.println("<br/>"); // Dpin 8-11 client.println("<table rules=\"all\"><tr><th></th><th></th></<tr>"); for (int i = 0; i < 4; i++) { client.println((String) "<tr><td>IO " + (8 + i) + (String) "</td><td>"); int digitalData = digitalRead(8 + i); client.println("<td>"); if (digitalData == HIGH) { client.println("HIGH"); } else { client.println("LOW"); } client.println("</td>"); client.println("</tr>"); } client.println("</table>"); client.println("<br/>"); client.println("</html>"); 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(1000); client.flush(); // close the connection: client.stop(); Serial.println("client disconnected"); } }
je8vgnのアイコン画像
北海道札幌市在住です。 趣味で電子工作を楽しんでいます。 「生田製作所」 Twitter(@teketek03653145)
ログインしてコメントを投稿する