
在修嵌入式的時候有想過專題要做四軸飛機後來想想,太大了,分享一下xbox360手把去控制
arduino serial 參數發送 移植!!
xboxcontroler-ardunino
-- Rui Santos | |
-- Complete project details at http://randomnerdtutorials.com | |
wifi.setmode(wifi.STATION) | |
wifi.sta.config("abcdefg","00000000") | |
led1 = 2 | |
led2 = 3 | |
gpio.mode(led1, gpio.OUTPUT) | |
gpio.mode(led2, gpio.OUTPUT) | |
srv=net.createServer(net.TCP) | |
print(wifi.sta.getip()) | |
srv:listen(80,function(conn) | |
conn:on("receive", function(client,request) | |
local buf = ""; | |
buf = buf.."HTTP/1.1 200 OK\n\n" | |
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP"); | |
if(method == nil)then | |
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP"); | |
end | |
local _GET = {} | |
if (vars ~= nil)then | |
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do | |
_GET[k] = v | |
end | |
end | |
if(_GET.pin == "ON1")then | |
gpio.write(led1, gpio.HIGH); | |
elseif(_GET.pin == "OFF1")then | |
gpio.write(led1, gpio.LOW); | |
elseif(_GET.pin == "ON2")then | |
gpio.write(led2, gpio.HIGH); | |
elseif(_GET.pin == "OFF2")then | |
gpio.write(led2, gpio.LOW); | |
end | |
client:send(buf); | |
client:close(); | |
collectgarbage(); | |
end) | |
end) |
#include <Wire.h> | |
const byte SLAVE_ADDRESS = 0x15; | |
void setup() { | |
Wire.begin(0x15); // join i2c bus with address #8 | |
Wire.onReceive(receiveEvent); // register event | |
Serial.begin(9600); // start serial for output | |
} | |
void loop() { | |
delay(100); | |
} | |
// function that executes whenever data is received from master | |
// this function is registered as an event, see setup() | |
void receiveEvent(int howMany) { | |
while (1 < Wire.available()) { // loop through all but the last | |
char c = Wire.read(); // receive byte as a character | |
Serial.print(c); // print the character | |
} | |
int x = Wire.read(); // receive byte as an integer | |
Serial.println(x); // print the integer | |
} |
id=0 | |
sda=1 | |
scl=2 | |
ARDUINO_I2C = 0x15 | |
tmp=0 | |
i2c.setup(id,sda,scl,i2c.SLOW) | |
print("I2C demo") | |
while 1 do | |
i2c.start(id) | |
i2c.address(id, 0x15,i2c.TRANSMITTER) | |
i2c.write(id,tmp) | |
i2c.stop(id) | |
print("write",tmp) | |
tmr.delay(1000000) | |
if tmp ==34 then | |
tmp=0 | |
else | |
tmp=tmp+1 | |
end | |
end |
/* | |
* IRremoteESP8266: IRrecvDemo - demonstrates receiving IR codes with IRrecv | |
* An IR detector/demodulator must be connected to the input RECV_PIN. | |
* Version 0.1 Sept, 2015 | |
* Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009, Copyright 2009 Ken Shirriff, http://arcfn.com | |
*/ | |
#include <IRremoteESP8266.h> | |
int RECV_PIN = 2; //an IR detector/demodulatord is connected to GPIO pin 2 | |
int tmp=255; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
int counter=0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); // Start the receiver | |
pinMode(16, OUTPUT); | |
pinMode(5, OUTPUT); | |
pinMode(4, OUTPUT); | |
} | |
void loop() { | |
if (irrecv.decode(&results)) { | |
counter++; | |
if(counter==1) | |
{ | |
Serial.println(results.value, HEX); | |
if(results.value==2780520712){ | |
digitalWrite(16, tmp); | |
digitalWrite(5, 0); | |
digitalWrite(4, 0);} | |
if(results.value==1275988228){ | |
digitalWrite(16,0); | |
digitalWrite(5, tmp); | |
digitalWrite(4, 0);} | |
if(results.value==1440861668){ | |
digitalWrite(16, 0); | |
digitalWrite(5, 0); | |
digitalWrite(4, tmp);} | |
Serial.println("44444"); | |
} | |
else if(counter==3) | |
{counter=0;} | |
irrecv.resume(); // Receive the next value | |
} | |
} |
// WiFi Weather Station | |
// Required libraries | |
#include <ESP8266WiFi.h> | |
// Libraries | |
#include <dht.h> | |
// Pin | |
#define dht_dpin 16 //定義訊號要從Pin A0 進來 | |
#define sensorPin 0 | |
dht DHT; | |
// WiFi parameters | |
int value = 0; | |
int counter = 0; | |
String tmp=""; | |
String tmp2=""; | |
const char* ssid = "AP"; | |
const char* password = "123456789"; | |
// Create an instance of the server | |
WiFiServer server(80); | |
void setup() { | |
// Start Serial | |
Serial.begin(115200); | |
delay(10); | |
// Connect to WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
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.println(WiFi.localIP()); | |
//光敏 | |
pinMode(0, INPUT); | |
} | |
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 req = client.readStringUntil('\r'); | |
Serial.println(req); | |
client.flush(); | |
counter++; | |
if(counter%2==0) | |
{ | |
value = analogRead(0); | |
if(value<204){ | |
tmp2="Very bright"; | |
Serial.println("Very bright"); | |
} | |
else if (value > 204 && value <=408){ | |
Serial.println("Ordinary bright"); | |
tmp2="Ordinary bright"; | |
} | |
else if (value > 408 && value <=612){ | |
Serial.println("Medium light"); | |
tmp2="Medium light"; | |
} | |
else if (value > 612 && value <=816){ | |
Serial.println("Dark"); | |
tmp2="Dark"; | |
} | |
else if (value > 816 && value <=1020){ | |
Serial.println("Very Dark"); | |
tmp2="Very Dark"; | |
} | |
Serial.println( tmp2 ); | |
DHT.read11(dht_dpin); //去library裡面找DHT.read11 | |
// Prepare the response | |
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; | |
s += "<head>"; | |
s += "<meta http-equiv=\"refresh\" content=\"1\">"; | |
s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"; | |
//s += "<meta http-equiv=\"refresh\" content=\"60\" />"; | |
s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>"; | |
s += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">"; | |
s += "<style>body{font-size: 12px;} .voffset {margin-top: 30px;}</style>"; | |
s += "</head>"; | |
s += "<div class=\"container\">"; | |
s += "<h1>WiFi Weather Station</h1>"; | |
s += "<div class=\"row voffset\">"; | |
tmp+= "<div class=\"col-md-3\">Temperature: </div><div class=\"col-md-3\">" + String(DHT.temperature) + "    light: "+tmp2+ "</div>"; | |
tmp+="<div class=\"col-md-3\">Humidity: </div><div class=\"col-md-3\">" + String(DHT.humidity) + "</div>"+"</br>"; | |
s += tmp; | |
s += "</div>"; | |
s += "</div>"; | |
// Send the response to the client | |
client.print(s); | |
delay(1); | |
Serial.println("Client disconnected"); | |
// The client will actually be disconnected | |
// when the function returns and 'client' object is detroyed | |
} | |
if(counter>=16) | |
{ | |
counter=0; | |
tmp=""; | |
} | |
} |
#include <SPI.h> | |
#include <MFRC522.h> | |
#define SS_PIN D4 | |
#define RST_PIN D8 | |
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class | |
MFRC522::MIFARE_Key key; | |
// Init array that will store new NUID | |
byte nuidPICC[4]; | |
byte ansidPICC[4] = {0xE5, 0x0F, 0x23, 0x77}; | |
void setup() { | |
Serial.begin(9600); | |
SPI.begin(); // Init SPI bus | |
rfid.PCD_Init(); // Init MFRC522 | |
for (byte i = 0; i < 6; i++) { | |
key.keyByte[i] = 0xFF; | |
} | |
pinMode(D1, OUTPUT); | |
pinMode(D2, OUTPUT); | |
pinMode(D3, OUTPUT); | |
Serial.println(F("This code scan the MIFARE Classsic NUID.")); | |
Serial.print(F("Using the following key:")); | |
printHex(key.keyByte, MFRC522::MF_KEY_SIZE); | |
} | |
void loop() { | |
// Look for new cards | |
if ( ! rfid.PICC_IsNewCardPresent()) | |
return; | |
// Verify if the NUID has been readed | |
if ( ! rfid.PICC_ReadCardSerial()) | |
return; | |
Serial.print(F("PICC type: ")); | |
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); | |
Serial.println(rfid.PICC_GetTypeName(piccType)); | |
// Check is the PICC of Classic MIFARE type | |
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && | |
piccType != MFRC522::PICC_TYPE_MIFARE_1K && | |
piccType != MFRC522::PICC_TYPE_MIFARE_4K) { | |
Serial.println(F("Your tag is not of type MIFARE Classic.")); | |
return; | |
} | |
if (rfid.uid.uidByte[0] != nuidPICC[0] || | |
rfid.uid.uidByte[1] != nuidPICC[1] || | |
rfid.uid.uidByte[2] != nuidPICC[2] || | |
rfid.uid.uidByte[3] != nuidPICC[3] ) { | |
Serial.println(F("A new card has been detected.")); | |
// Store NUID into nuidPICC array | |
for (byte i = 0; i < 4; i++) { | |
nuidPICC[i] = rfid.uid.uidByte[i]; | |
} | |
Serial.println(F("The NUID tag is:")); | |
Serial.print(F("In hex: ")); | |
printHex(rfid.uid.uidByte, rfid.uid.size); | |
Serial.println(); | |
Serial.print(F("In dec: ")); | |
printDec(rfid.uid.uidByte, rfid.uid.size); | |
Serial.println(); | |
} | |
else Serial.println(F("Card read previously.")); | |
// Halt PICC | |
rfid.PICC_HaltA(); | |
// Stop encryption on PCD | |
rfid.PCD_StopCrypto1(); | |
} | |
/** | |
Helper routine to dump a byte array as hex values to Serial. | |
*/ | |
void printHex(byte *buffer, byte bufferSize) { | |
int ans_counter = 0; | |
for (byte i = 0; i < bufferSize; i++) { | |
Serial.print(buffer[i] < 0x10 ? " 0" : " "); | |
Serial.print(buffer[i], HEX); | |
if (buffer[i] == ansidPICC[i]) | |
ans_counter++; | |
} | |
if ( ans_counter == 4) | |
{ | |
digitalWrite(D1, 1); | |
digitalWrite(D3, 1); | |
delay(300); | |
digitalWrite(D1, 0); | |
digitalWrite(D3, 0); | |
} | |
else if ( ans_counter == 0) | |
{ | |
for (int i = 0; i < 2; i++) { | |
digitalWrite(D2, 1); | |
digitalWrite(D3, 1); | |
delay(50); | |
digitalWrite(D2, 0); | |
digitalWrite(D3, 0); | |
delay(50); | |
} | |
} | |
} | |
/** | |
Helper routine to dump a byte array as dec values to Serial. | |
*/ | |
void printDec(byte *buffer, byte bufferSize) { | |
for (byte i = 0; i < bufferSize; i++) { | |
Serial.print(buffer[i] < 0x10 ? " 0" : " "); | |
Serial.print(buffer[i], DEC); | |
} | |
} |
#include <ESP8266WebServer.h> | |
int sensorPin = 2; | |
int value = 0; | |
const char* ssid = "AP"; | |
const char* password = "123456789"; | |
WiFiServer server(80); | |
void setup() { | |
Serial.begin(115200); | |
// prepare GPIO2 | |
pinMode(0, INPUT); | |
pinMode(2, OUTPUT); | |
digitalWrite(2, 0); | |
// Connect to WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
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.println(WiFi.localIP()); | |
} | |
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 req = client.readStringUntil('\r'); | |
Serial.println(req); | |
client.flush(); | |
// Match the request | |
int val; | |
if (req.indexOf("/LED=ON") != -1) | |
val = 1; | |
else if (req.indexOf("/LED=OFF") != -1) | |
val = 0; | |
else if (req.indexOf("/LED=check") != -1) | |
val = -1; | |
else { | |
Serial.println("invalid request"); | |
client.stop(); | |
return; | |
} | |
// Set GPIO2 according to the request | |
client.flush(); | |
// Prepare the response | |
client.println("<html>"); | |
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>"); | |
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a>"); | |
client.println("<a href=\"/LED=check\"\"><button>Led Status</button></a><br />"); | |
client.println("</html>"); | |
// Send the response to the client | |
delay(1); | |
Serial.println("Client disonnected"); | |
String tmp=""; | |
value = analogRead(0); | |
//Serial.println(value, DEC); | |
if(val==1 ){ | |
tmp="Very bright"; | |
digitalWrite(2, val); | |
} | |
else if(val==0 ){ | |
tmp="Very Dark"; | |
digitalWrite(2, val); | |
} | |
else if (val==-1){ | |
if(value<204){ | |
tmp="Very bright"; | |
Serial.println("Very bright"); | |
digitalWrite(2, 0); | |
val = 1; | |
} | |
else if (value > 204 && value <=408){ | |
Serial.println("Ordinary bright"); | |
tmp="Ordinary bright"; | |
digitalWrite(2, 0); | |
val = 1; | |
} | |
else if (value > 408 && value <=612){ | |
Serial.println("Medium light"); | |
tmp="Medium light"; | |
digitalWrite(2, 0); | |
val = 1; | |
} | |
else if (value > 612 && value <=816){ | |
Serial.println("Dark"); | |
tmp="Dark"; | |
digitalWrite(2, 1); | |
val = 0; | |
} | |
else if (value > 816 && value <=1020){ | |
Serial.println("Very Dark"); | |
tmp="Very Dark"; | |
digitalWrite(2, 1); | |
val = 0; | |
} | |
} | |
String s = "<html>\r\nGPIO is now "; | |
s += (val)?"high":"low "; | |
s += " Light Power:"+tmp; | |
s += "</html>\n"; | |
client.print(s); | |
// The client will actually be disconnected | |
// when the function returns and 'client' object is detroyed | |
} |
const int trig = 5; | |
const int echo = 4; | |
int buzzerPin = 3;//蜂鳴器 | |
const int inter_time = 100; | |
int time = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode (trig, OUTPUT); | |
pinMode (echo, INPUT); | |
pinMode (buzzerPin, OUTPUT); | |
} | |
void loop() { | |
float duration, distance; | |
digitalWrite(trig, HIGH); | |
delayMicroseconds(100); | |
digitalWrite(trig, LOW); | |
duration = pulseIn (echo, HIGH); | |
distance = (duration/2)/29; | |
Serial.print("Data:"); | |
Serial.print (time/100); | |
Serial.print(", d = "); | |
Serial.print(distance); | |
if(distance<10){ | |
digitalWrite (buzzerPin, HIGH); | |
delay (5); | |
digitalWrite (buzzerPin, LOW); | |
delay (5); | |
} | |
else | |
{ | |
digitalWrite (buzzerPin, HIGH); | |
delay (100); | |
digitalWrite (buzzerPin, LOW); | |
delay (100); | |
} | |
Serial.println(" cm"); | |
time = time + inter_time; | |
delay(inter_time); | |
} |