내 처음 게시하지만, 정말 붙어있어 도움이 필요합니다.로컬 호스트 arduino 장치를 제어하는 Python 명령
저는 현재 라우터를 사용하여 원격 Arduino 장치를 제어하려는 홈 오토메이션 시스템에서 작업하고 있습니다.
그 부분이 모두 완료되었으며, 켜거나 끌 수있는 브라우저를 사용하여 링크에 연결할 수 있습니다. 나는 심지어 그것으로 응용 프로그램을 만들었습니다.
현재 음성 인식 코드를 작업하고 있습니다. 코드를 가져 와서 음성으로 변환하고 음성 인식 부분은 SpeechRecognition 라이브러리와 잘 작동합니다. 일단 Python이 코드를 이해하면 명령을 내리고 url을 사용하십시오.
http://www.google.com과 같은 URL에 액세스하려고하면 URL이 "http://192.168.0.227/LED=ON14"이지만이 프로그램을 사용할 때 프로그램이 멈추고 시간이 초과되면 이미 여러 가지 방법으로 제어하려고했지만 그 중 아무 것도 시도하지 않은 것 같습니다. 그 (것)들은 작동하는 것을 보인다.
import urllib2
sock = urllib2.urlopen("http://192.168.0.227/LED=ON14")
htmlSource = sock.read()
sock.close()
print htmlSource
이
는 응답이 끝난 내 최신 시험> File "<stdin>", line 1, in <module> File
> "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
> return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 431, in open
> response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 449, in _open
> '_open', req) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
> result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
> return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1194, in do_open
> h.request(req.get_method(), req.get_selector(), req.data, headers) File "/usr/lib/python2.7/httplib.py", line 1039, in request
> self._send_request(method, url, body, headers) File "/usr/lib/python2.7/httplib.py", line 1073, in _send_request
> self.endheaders(body) File "/usr/lib/python2.7/httplib.py", line 1035, in endheaders
> self._send_output(message_body) File "/usr/lib/python2.7/httplib.py", line 879, in _send_output
> self.send(msg) File "/usr/lib/python2.7/httplib.py", line 841, in send
> self.connect() File "/usr/lib/python2.7/httplib.py", line 822, in connect
> self.timeout, self.source_address) File "/usr/lib/python2.7/socket.py", line 562, in create_connection
> sock.connect(sa) File "/usr/lib/python2.7/socket.py", line 224, in meth
> return getattr(self._sock,name)(*args)
했다. 내 아두 이노 코드가 ESP8266입니다
은 다음과 같습니다
#include <ESP8266WiFi.h>
const char* ssid = "--";
const char* password = "--";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(14, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(15, OUTPUT);
digitalWrite(14, LOW);digitalWrite(12, LOW);digitalWrite(13, LOW);digitalWrite(15, LOW); digitalWrite(2,LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress ip(192, 168, 0, 106);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);
// Static IP Setup Info Here...
WiFi.config(ip,dns, gateway, subnet); //If you need Internet Access You should Add DNS also...
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
int value = LOW;
// Return the response
/*client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><br>");
client.println("<a href=\"/LED=ON14\"\"><button>Turn On 4 </button></a>");
client.println("<a href=\"/LED=OFF14\"\"><button>Turn Off 4</button></a><br />");
client.println("<a href=\"/LED=ON12\"\"><button>Turn On 2 </button></a>");
client.println("<a href=\"/LED=OFF12\"\"><button>Turn Off 2</button></a><br />");
client.println("<a href=\"/LED=ON13\"\"><button>Turn On 3 </button></a>");
client.println("<a href=\"/LED=OFF13\"\"><button>Turn Off 3</button></a><br />");
client.println("<a href=\"/LED=ON15\"\"><button>Turn On 1 </button></a>");
client.println("<a href=\"/LED=OFF15\"\"><button>Turn Off 1</button></a><br />");
client.println("</html>"); */
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Led pin is now: ");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
// Match the request
if (request.indexOf("/ON4") != -1) {
digitalWrite(14, LOW);
value = HIGH;
}
if (request.indexOf("/OFF4") != -1) {
digitalWrite(14, HIGH);
value = LOW;
}
if (request.indexOf("/ON3") != -1) {
digitalWrite(13, LOW);
value = HIGH;
}
if (request.indexOf("/OFF3") != -1) {
digitalWrite(13, HIGH);
value = LOW;
}
if (request.indexOf("/ON2") != -1) {
digitalWrite(12, LOW);
value = HIGH;
}
if (request.indexOf("OFF2") != -1) {
digitalWrite(12, HIGH);
value = LOW;
}
if (request.indexOf("/ON1") != -1) {
digitalWrite(15, LOW);
value = HIGH;
}
if (request.indexOf("/OFF1") != -1) {
digitalWrite(15, HIGH);
value = LOW;
}
}
arduino/esp8266 코드도 게시 할 수 있습니까? 라스베리가 "200 OK"헤더 응답을 얻지 못할 수도 있습니다. – XerXeX
게시물을 편집 한 결과 HTML 코딩에 대해 많이 알지 못합니다. –