#include <WiFi.h>
#include <ESPmDNS.h>
#include <FastLED.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiUDP.h>
static WiFiUDP wifiUdp;
static const char *kRemoteIpadr = "192.168.x.255"; //ブロードキャスト
static const int kRmoteUdpPort = 1234; //送信先のポート
// 使用するWi-Fiとそのパスワードに書き換えてください
const char* ssid = "WiFI SSID";
const char* password = "WiFI PASS";
// ポート
WiFiServer server(80);
IPAddress ip(192, 168, x, xxx); // 固定IP
IPAddress gateway(192,168, x, 1); //
IPAddress subnet(255, 255, 0, 0); //
// HTTPリクエストを格納する変数
String header;
// 値の設定に使用する変数
String valueString = String(0);
int pos1 = 0;
int pos2 = 0;
//neopixel
#define PIN 14
#define NUMPIXELS 48
CRGB leds[NUMPIXELS];
String response;
int rxPicState = 0;
#define ONE_WIRE_BUS 15 // 温度センサデータ線
#define SENSER_BIT 13 // 精度の設定bit
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float x = 0.0;
char temp[10];
int reading = 0;
float voltage = 0.0;
float pH = 0.0;
portTickType Delay10 = 10000 / portTICK_RATE_MS;
void tempPh(void *pvParameters) {
while(1){
// 温度
sensors.requestTemperatures();
x = sensors.getTempCByIndex(0);
dtostrf(x, 6, 2, temp);
Serial.print(temp);
//pH
reading = analogRead(33);
voltage = ((long)reading * 3.3) / 4096;
pH = -4.7284 * voltage + 19.229;
Serial.print("\t");
Serial.println(pH);
wifiUdp.beginPacket(kRemoteIpadr, kRmoteUdpPort);
wifiUdp.print(String(temp));
wifiUdp.write(',');
wifiUdp.print(String(pH));
wifiUdp.endPacket();
vTaskDelay(Delay10);
}
}
void setup() {
Serial.begin(115200);
Serial2.begin(1152000, SERIAL_8N1, 13, 12);
WiFi.config(ip, gateway, subnet); // Set fixed IP address
delay(10);
// Wi-Fiに接続
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
static const int kLocalPort = 7000; //自身のポート
wifiUdp.begin(kLocalPort);
// ローカルIP表示
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.begin();
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUMPIXELS);
FastLED.clear();
FastLED.show();
sensors.begin();
sensors.setResolution(SENSER_BIT);
//水温・ph測定 送信 タスク
xTaskCreatePinnedToCore(
tempPh
, "tempPh" // A name just for humans
, 4096 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 1 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL
, 0);
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
…金魚水槽システム画面のhtml記述…
}
}