นี่คือตัวอย่างโค๊ดทำการ Blink LED บน บอร์ด ARDUINO
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// initialize digital pin 13 as an output.
pinMode(2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
if(digitalRead(2)== HIGH){
Serial.println("1");
}else{
Serial.println("0");
}
}
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
if(digitalRead(2)== HIGH){
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
}else{
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
}
}
ขั้นตอนการทำให้ Node MCU ของเราสามารถเชื่อมต่อกับ AP แบบ DHCP
1. เขียนโค๊ดลง Arduino IDE และระบุ ssid กับ password ของ internet ที่เราต้องการให้เชื่อมต่อเป็น Access point
#include <ESP8266WiFi.h>
#include <OneWire.h>
const char* ssid = "Dok";
const char* password = "123456789";
#define Relay1 D1
#define Relay2 D2
OneWire ds(D4);
float celsius, fahrenheit;
WiFiServer server(5678);
void setup() {
Serial.begin(115200);
delay(10);
//dht.begin();
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
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");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
if (req.indexOf("/r1off.html") != -1)
{
digitalWrite(Relay1, HIGH);
}
else if (req.indexOf("/r1on.html") != -1)
{
digitalWrite(Relay1, LOW);
}
else if (req.indexOf("/r2on.html") != -1)
{
digitalWrite(Relay2, LOW);
}
else if (req.indexOf("/r2off.html") != -1)
{
digitalWrite(Relay2, HIGH);
}
else if (req.indexOf("/Temp_.html") != -1)
{
float h = TempDS18B20();
if (h < 90000.0) {
float t = h * 1.8 + 32.0;
client.flush();
String humi = "<h3>Temp = ";
humi += String(h) + " C</h3>\r\n";
humi += "<h3>Temp = ";
humi += String(t) + " F</h3>\r\n";
Serial.println(h);
Serial.println(t);
client.print(humi);
}
}
else if (req.indexOf("/") != -1)
{
client.flush();
String web = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
web += "<html>\r\n";
web += "<head>\r\n";
web += "<style>\r\n";
web += ".button-red,.button-blue\r\n";
web += "{color: white;\r\n";
web += "border-radius: 9px;\r\n";
web += "font-family:Arial;\r\n";
web += "font-size:25px;\r\n";
web += "padding:50px 80px;\r\n";
web += "}\r\n";
web += ".button-red:hover:active,.button-blue:hover:active\r\n";
web += "{position:relative;top:3px;color: yellow;}\r\n";
web += ".button-red {background: rgb(202, 60, 60);}\r\n";
web += ".button-blue {background: rgb(100,116,255);}\r\n";
web += "h1{color:black;font-family:Arial;font-size:40px;text-align:center;}\r\n";
web += "</style>\r\n";
web += "<script>\r\n";
web += "var ajax = null;\r\n";
web += "if (window.XMLHttpRequest)\r\n";
web += "{ajax =new XMLHttpRequest();}\r\n";
web += "else\r\n";
web += "{ajax=new ActiveXObject(\"Microsoft.XMLHTTP\");}\r\n";
web += "function ajaxLoad(method ,URL,displayElementId,sendData)\r\n";
web += "{\r\n";
web += "if(!ajax){alert(\"not support\");return;}\r\n";
web += "ajax.open(method,URL,true);\r\n";
web += "ajax.onreadystatechange = function()\r\n";
web += "{\r\n";
web += "if(ajax.readyState == 4 && ajax.status==200)\r\n";
web += "{\r\n";
web += "var ajax_result = ajax.responseText;\r\n";
web += "var el = document.getElementById(displayElementId);\r\n";
web += "el.innerHTML = ajax_result;\r\n";
web += "}\r\n";
web += "}\r\n";
web += "ajax.send(sendData);\r\n";
web += "}\r\n";
web += "function update_temp_humi()\r\n";
web += "{\r\n";
web += "var rand = Math.random();\r\n";
web += "var URL = \"/Temp_.html\";\r\n";
web += "ajaxLoad(\"GET\",URL,\'temp_humi\',null);\r\n";
web += "}\r\n";
web += "</script>\r\n";
web += "</head>";
web += "<body>";
web += "<h1>Demo Web Server Ajax</h1>";
web += "<h2>Temp C and F</h2>";
web += "<div id=\"temp_humi\">";
web += "<h3>Temp = xx C</h3>";
web += "<h3>Temp = xx F</h3>";
web += "</div>";
web += "<div><p><input class=\"button-red\" type=\"button\" value=\"Relay 1 On\" onclick=\"Relay(\'r1on\')\">";
web += "<input class=\"button-blue\" type=\"button\" value=\"Relay 1 Off\" onclick=\"Relay(\'r1off\')\"></P>";
web += "<p><input class=\"button-red\" type=\"button\" value=\"Relay 2 On\" onclick=\"Relay(\'r2on\')\">";
web += "<input class=\"button-blue\" type=\"button\" value=\"Relay 2 Off\" onclick=\"Relay(\'r2off\')\"></P>";
web += "</div>";
web += "<script>";
web += "function Relay(state){ajaxLoad(\"GET\",state+\'.html\',null,null);}";
web += "setInterval(\"update_temp_humi()\",2000);";
web += "</script>";
web += "</body>";
web += "</html>";
client.print(web);
return;
}
}
float TempDS18B20(void)
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
//Serial.println("No more addresses.");
//Serial.println();
ds.reset_search();
delay(250);
return 99999.9;
}
//Serial.print("ROM =");
for ( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
//Serial.println("CRC is not valid!");
return 99999.9;
}
//Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
////Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
//Serial.println("Device is not a DS18x20 family device.");
return 99999.9;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
//Serial.print(" Data = ");
//Serial.print(present,HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
//Serial.print(data[i], HEX);
//Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
//Serial.println();
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
return ((float)raw / 16.0);
}
2. ทำการต่อ LED เข้ากับ ESP8266 แลัว อัพโหลดโค๊ด จากนั้นนำไอพี ของ ESP8266 ใส่ค้นหาลงใน Browser เช่นเดียวกับ Web server input ถ้าทำถูกต้องจะได้ผลลัพท์อย่างนี้