ตัวอย่่างโปรแกรม NodeMCU V2 สั่งเปิดปิดไฟผ่าน GPIO โดยให้ ESP8266 เป็น Web Server
#include <ESP8266WiFi.h>
const char* ssid = "Wifi SSID";
const char* password = "Wifi Password";
int led1=1;
int led2=2;
int led3=3;
int led4=4;
int led5=5;
int led6=16;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
digitalWrite(led1, 0);
digitalWrite(led2, 0);
digitalWrite(led3, 0);
digitalWrite(led4, 0);
digitalWrite(led5, 0);
digitalWrite(led6, 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("/gpio/") != -1){
int start = req.indexOf("/gpio/");
int eidx = req.indexOf(" HTTP");
String nval = req.substring(start+6,eidx);
val = nval.toInt();
} else {
val = 0;
// Serial.println("invalid request");
// client.stop();
// return;
}
// Set GPIO2 according to the request
digitalWrite(led1, (((val == 1) !=0))?!digitalRead(led1):digitalRead(led1));
digitalWrite(led2, (((val == 2) !=0))?!digitalRead(led2):digitalRead(led2));
digitalWrite(led3, (((val == 3) !=0))?!digitalRead(led3):digitalRead(led3));
digitalWrite(led4, (((val == 4) !=0))?!digitalRead(led4):digitalRead(led4));
digitalWrite(led5, (((val == 5) !=0))?!digitalRead(led5):digitalRead(led5));
digitalWrite(led6, (((val == 6) !=0))?!digitalRead(led6):digitalRead(led6));
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Access-Control-Allow-Origin:*\r\nAccess-Control-Allow-Methods:POST, GET\r\nAccess-Control-Allow-Headers:AUTHORISED\r\n";
s += "Content-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
s += "\r\n<head>";
s += "<style>";
s += "a{";
s += " display:inline-block;text-align:center;height:60px; width:60px; border: black solid 4px;";
s += " font-size:2em; padding:0.6em; border-radius:1.2em;";
s += " magin:0.5em;";
s += "}\r\n";
s += ".on{ background-color:red; }\r\n";
s += ".off{ background-color:white;}\r\n";
s += "</style>";
s+= "\r\n</head>";
s+= "<body>";
s += "<h1>";
s += "<a href=\"/gpio/1\" class=\"" ;
s += digitalRead(led1)?"on":"off";
s += "\">1</a> ";
s += "<a href=\"/gpio/2\" class=\"" ;
s += digitalRead(led2)?"on":"off";
s += "\">2</a> ";
s += "<a href=\"/gpio/3\" class=\"" ;
s += digitalRead(led3)?"on":"off";
s += "\">3</a> ";
s += "<a href=\"/gpio/4\" class=\"" ;
s += digitalRead(led4)?"on":"off";
s += "\">4</a> ";
s += "<a href=\"/gpio/5\" class=\"" ;
s += digitalRead(led5)?"on":"off";
s += "\">5</a> ";
s += "<a href=\"/gpio/6\" class=\"" ;
s += digitalRead(led6)?"on":"off";
s += "\">6</a> ";
s += "</h1>";
s += "\r\nGPIO is now ";
s += (val)?"high":"low";
s += "<br/><br/>[" + req +"]<br/>" + val;
s += "</body>";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
วันพฤหัสบดีที่ 27 สิงหาคม พ.ศ. 2558
วันพุธที่ 26 สิงหาคม พ.ศ. 2558
ทำหุนยนต์ ให้เด็กๆเล่นกัน NodMCU คุมผ่าน WiFi ^_^
// เผื่อใครใจร้อนอยากลองก่อนครับ ^_^
//Code by AndronoIDE
// I will explain in detail later ^_^
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "Your WiFi SSID";
const char* password = "Your WiFi Password";
const char* host = "IP your computer";
char message[255];
unsigned int localPort = 8080;
WiFiUDP Udp;
// Pin layout is NodeMCU V.2
int pin1_motor_R = 1; //gpio01 --> IN1 motor controller Right Motor
int pin2_motor_R = 2; //gpio02 --> IN2 motor controller Right Motor
int pin1_motor_L = 3; //gpio03 --> IN3 motor controller Left Motor
int pin2_motor_L = 4; //gpio04 --> IN4 motor controller Left Motor
// Direction
int FW=0,BK=1,ST=2;
int dir=FW;
// Turn Status
int t=0;
int TL=0, TR=1;
int tdir=0;
int tcount=0;
void setup() {
Serial.begin(115200);
pinMode(pin1_motor_R , OUTPUT);
pinMode(pin2_motor_R, OUTPUT);
pinMode(pin1_motor_L, OUTPUT);
pinMode(pin2_motor_L, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(",");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Udp.begin(localPort);
Stop();
Serial.println("Setup done");
delay(2000);
}
void loop(){
int packetSize = Udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(message, 255);
Serial.println(message);
checkcommand();
delay(5);
}
}
void forward()
{
Serial.println("forward");
dir=FW;
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void back()
{
Serial.println("back");
dir=BK;
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void left()
{
Serial.println("left");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void right()
{
Serial.println("Right");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void turnR()
{
Serial.println("Turn0");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void turnL()
{
Serial.println("Turn1");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void Stop()
{
Serial.println("Stop");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void checkcommand()
{
if (*message=='f')
{
forward();
}else if (*message=='t')
{
if(tdir==TL){
turnR();
}else{
turnL();
}
delay(50);
Stop();
}
else if (*message=='b')
{
back();
}
else if (*message=='r')
{
tdir=TR;
right();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnR();
tcount=0;
}
}
}
else if (*message=='l')
{
tdir=TL;
left();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnL();
tcount=0;
}
}
}
else
{
dir=ST;
tcount=0;
Stop();
}
}
//Code by AndronoIDE
// I will explain in detail later ^_^
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "Your WiFi SSID";
const char* password = "Your WiFi Password";
const char* host = "IP your computer";
char message[255];
unsigned int localPort = 8080;
WiFiUDP Udp;
// Pin layout is NodeMCU V.2
int pin1_motor_R = 1; //gpio01 --> IN1 motor controller Right Motor
int pin2_motor_R = 2; //gpio02 --> IN2 motor controller Right Motor
int pin1_motor_L = 3; //gpio03 --> IN3 motor controller Left Motor
int pin2_motor_L = 4; //gpio04 --> IN4 motor controller Left Motor
// Direction
int FW=0,BK=1,ST=2;
int dir=FW;
// Turn Status
int t=0;
int TL=0, TR=1;
int tdir=0;
int tcount=0;
void setup() {
Serial.begin(115200);
pinMode(pin1_motor_R , OUTPUT);
pinMode(pin2_motor_R, OUTPUT);
pinMode(pin1_motor_L, OUTPUT);
pinMode(pin2_motor_L, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(",");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Udp.begin(localPort);
Stop();
Serial.println("Setup done");
delay(2000);
}
void loop(){
int packetSize = Udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(message, 255);
Serial.println(message);
checkcommand();
delay(5);
}
}
void forward()
{
Serial.println("forward");
dir=FW;
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void back()
{
Serial.println("back");
dir=BK;
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void left()
{
Serial.println("left");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void right()
{
Serial.println("Right");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void turnR()
{
Serial.println("Turn0");
digitalWrite(pin1_motor_R, HIGH);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, HIGH);
}
void turnL()
{
Serial.println("Turn1");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, HIGH);
digitalWrite(pin1_motor_L, HIGH);
digitalWrite(pin2_motor_L, LOW);
}
void Stop()
{
Serial.println("Stop");
digitalWrite(pin1_motor_R, LOW);
digitalWrite(pin2_motor_R, LOW);
digitalWrite(pin1_motor_L, LOW);
digitalWrite(pin2_motor_L, LOW);
}
void checkcommand()
{
if (*message=='f')
{
forward();
}else if (*message=='t')
{
if(tdir==TL){
turnR();
}else{
turnL();
}
delay(50);
Stop();
}
else if (*message=='b')
{
back();
}
else if (*message=='r')
{
tdir=TR;
right();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnR();
tcount=0;
}
}
}
else if (*message=='l')
{
tdir=TL;
left();
delay(200);
if(dir==FW){
forward();
}else if(dir==BK){
back();
}else{
tcount++;
if(tcount!=5){
Stop();
}else{
turnL();
tcount=0;
}
}
}
else
{
dir=ST;
tcount=0;
Stop();
}
}
ป้ายกำกับ:
Diy kids toy,
ESP8266,
NodeMCU,
Robot,
Smart Car
สมัครสมาชิก:
บทความ (Atom)