teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

ソースコード改良

2021/12/19 03:07

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,22 +5,24 @@
5
5
  esp82661台をAP(Webサーバー)、1台をSTAとして、APのhtml上に2つのボタンを用意し、
6
6
  スマホでそのボタンを押すことでLEDそれぞれを制御したいです。
7
7
  現状AのLED(親機)は制御できているのですがBのLED(子機)ができておりません。
8
+ シリアルを出力したところ親機側にて子機との通信ができておりません。。
8
9
  アドバイスをいただきたいです!
9
- ※エラーは出ておりません。
10
10
  ```
11
- ### 該当のソースコード
11
+ ### 該当のソースコード(追記)
12
+
13
+ 以下ソース
12
- /*AP親機*/
14
+ /*親機*/
13
15
  #include <ESP8266WiFi.h>
14
16
  #include <WiFiClient.h>
15
17
  #include <ESP8266WebServer.h>
16
18
  const char *ssid = "LED";
17
- const char *password = "123456789";
19
+ const char *password = "123456789"; //子機
18
- const IPAddress ip(192, 168, 4, 1);
20
+ const IPAddress ip(192, 168, 4, 3); //子機
19
21
  const IPAddress subnet(255, 255, 255, 0);
20
22
  boolean ALED = false;
21
23
  boolean BLED = false;
22
24
  ESP8266WebServer server(80);
23
- WiFiServer Lserver(81);
25
+ WiFiClient client;
24
26
  String form ="<html>"
25
27
  "<head><meta name=viewport content=width=100></head>"
26
28
  "<form action=A_LED><input type=submit value=A></form>"
@@ -38,14 +40,15 @@
38
40
  Serial.println();
39
41
  Serial.print("Configuring access point...");
40
42
  WiFi.softAP(ssid, password);
41
- IPAddress myIP = WiFi.softAPIP(); //デフォルト192.168.4.1
43
+ IPAddress myIP = WiFi.softAPIP();
42
44
  Serial.print("AP IP address: ");
43
45
  Serial.println(myIP);
46
+ WiFi.begin("BLED","123456789");
44
47
  server.begin();
45
- Lserver.begin();
46
48
  server.on("/", handleRoot);
47
49
  server.on("/A_LED",Atika);
48
50
  server.on("/B_LED",Btika);
51
+
49
52
  Serial.println("HTTP server started");
50
53
 
51
54
  pinMode(4, OUTPUT);
@@ -70,36 +73,40 @@
70
73
  server.send(200, "text/html", form);
71
74
  }
72
75
  void B_LED(){
76
+ if(client.connect(ip,80))Serial.println("success");
73
- WiFiClient client = Lserver.available();
77
+ if(client.connected())Serial.println("success");
74
- if(client){
75
- client.connected();
76
- if(client.available()){
77
- BLED = !BLED;
78
+ BLED = !BLED;
78
- client.write(BLED);
79
+ client.write(BLED);
79
- delay(5000);
80
+ delay(1000);
80
- }
81
+ }
81
- }
82
- }
83
-
84
- /*STA子機*/
82
+ /*子機*/
85
83
  #include <ESP8266WiFi.h>
86
- #define STASSID "LED"
87
- #define STAPSK "123456789"
88
- const char* ssid = STASSID;
89
- const char* password = STAPSK;
90
- const IPAddress ip(192,168,4,1);
91
- static WiFiClient client;
92
84
 
85
+ const char* ssid = "BLED";
86
+ const char* password = "123456789";
87
+ const IPAddress ip(192,168,4,3);
88
+ const IPAddress subnet(255,255,255,0);
89
+ WiFiServer server(80);
90
+
91
+ boolean BLED=false;
92
+
93
+ void B_tika();
94
+
93
95
  void setup() {
96
+ delay(1000);
94
97
  Serial.begin(115200);
98
+
95
99
  Serial.println();
96
100
  Serial.println();
97
101
  Serial.print("Connecting to ");
98
102
  Serial.println(ssid);
99
103
 
100
- WiFi.mode(WIFI_STA);
104
+ WiFi.mode(WIFI_AP);
105
+ delay(100);
101
- WiFi.begin(ssid, password);
106
+ WiFi.softAP(ssid,password);
102
-
107
+ WiFi.softAPConfig(ip,ip,subnet);
108
+ IPAddress myIP = WiFi.softAPIP();
109
+
103
110
  while (WiFi.status() != WL_CONNECTED) {
104
111
  delay(500);
105
112
  Serial.print(".");
@@ -108,22 +115,23 @@
108
115
  Serial.println("");
109
116
  Serial.println("WiFi connected");
110
117
  Serial.println("IP address: ");
111
- Serial.println(WiFi.localIP());
118
+ Serial.println(myIP);
112
- if(client.connect(ip,81))Serial.println("success");
113
-
114
119
  pinMode(4, OUTPUT);
115
120
  digitalWrite(4, LOW);
121
+ server.begin();
116
122
  }
117
- boolean BLED=false;
123
+
118
124
  void loop() {
119
- server.handleClient();
120
- if(client.connected()==true){
121
- BLED=client.read();
125
+ B_tika();
122
- digitalWrite(4, BLED);
123
- }
124
126
  }
127
+ void B_tika(){
128
+ WiFiClient client = server.available();
129
+ client.connected();
130
+ BLED=client.read();
131
+ digitalWrite(4,BLED);
132
+ }
125
133
  ```
126
134
  ### 詳細
127
- 子機と親機が通信できていないためと思われます・・・
135
+
128
136
  ### 補足情報(FW/ツールのバージョンなど)
129
137
  Arduino 1.8.16

2

内容の加筆

2021/12/19 03:06

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
  お世話になります!
3
- スマホ1台で、2台のesp8266それぞれのLEDを点灯と消灯をさせてみたいです。
3
+ スマホ1台で、WiFiを使用し直接2台のesp8266それぞれのLEDを点灯と消灯をさせてみたいです。
4
4
  具体的には、
5
5
  esp82661台をAP(Webサーバー)、1台をSTAとして、APのhtml上に2つのボタンを用意し、
6
6
  スマホでそのボタンを押すことでLEDそれぞれを制御したいです。

1

誤字の訂正

2021/12/18 08:09

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ### 前提・実現したいこと
2
- お世話になります
2
+ お世話になります
3
3
  スマホ1台で、2台のesp8266それぞれのLEDを点灯と消灯をさせてみたいです。
4
4
  具体的には、
5
5
  esp82661台をAP(Webサーバー)、1台をSTAとして、APのhtml上に2つのボタンを用意し、