2017/02/08
Example Sketches For MakerSwitch Tail ESP8266 WiFi Switch Using Dynamic and Static IP
I have made a batch of wifi controlled switches called MakerSwitch Tail kit built-in with a high quality transformer HLK-PM01 from Hi-Link, a relay circuit, an ESP8266 D1 mini module and a plastic enclosure inspired from PowerSwitch Tail and Sonoff. It use GPIO16 to control the 10A relay in it, so the rated power is 1100W for 110VAC or 2200W for 220VAC. I have run the codes using both dynamic IP and static IP without fail.
Be careful, when re-entering the web page, the status may be wrong. Modify the example code to fit your need.
I have also developed the sketch built-in the kit for sale in Taiwan acting as MQTT client integrating MQTT protocol and ESP8266 SmartConfig.
Photos
Sketch for Dynamic IP
#include <ESP8266WiFi.h>
const char* ssid = "your-ssid"; // use ssid of your router
const char* password = "your-password"; // use password for your router
int ledPin = 16; // GPIO16
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// 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.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();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// 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.print("Led pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
// use text to control led or switch
// client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on GPIO16 ON<br>");
// client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on GPIO16 OFF<br>");
// use button to control led or switch
client.println("<a href=\"/LED=ON\"><button>ON</button></a> <a href=\"/LED=OFF\"><button>OFF</button></a></p>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
Sketch for Static IP
#include <ESP8266WiFi.h>
const char* ssid = "your-ssid"; // use ssid of your router
const char* password = "your-password"; // use password for your router
int ledPin = 16; // GPIO16
WiFiServer server(80);
IPAddress ip(192, 168, 1, 99); // the desired IP address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
WiFi.config(ip, gateway, subnet);
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();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// 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.print("Led pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
// use text to control led or switch
client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on GPIO16 ON<br>");
client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on GPIO16 OFF<br>");
// use button to control led or switch
// client.println("<a href=\"/LED=ON\"><button>ON</button></a> <a href=\"/LED=OFF\"><button>OFF</button></a></p>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
Labels:
Config,
Dynamic,
ESP8266,
Example,
IP,
MakerSwitch,
PowerSwitch,
Sketch,
Smart,
SmartConfig,
Static,
Switch,
Tail,
WiFi
2016/08/15
Part List Of MakerDolly 1200mm Camera Slider Mechanical Kit
Labels:
Camera,
Dolly,
Kit,
List,
MakerDolly,
Mechanical,
Part,
Slider
2016/07/31
Adafruit Max6675 Library For Thermocouple Need To Be Modified To Run On ESP8266
Test the sketch "Wiring Thermocouple Max6675 on ESP8266 12E NodeMCU" of 14Core, and find Adafruit MAX6675 library installed using Library Manager is not compatible with ESP8266.
By replacing max6675.h and max6675.cpp with the ones from SirUli on github, copying other files on the link and commenting out the statements relating display functions, the sketch of 14Core has been compiled and run without fail on NodeMCU Dev Kit 1.0.
Also the serialthermocoupl.pde example sketch runs without fail.
I think the tutorials of 14Core had better specify from where the library they downloaded.
Wiring from 14Core
Output
Sketch
#include <max6675.h>
// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <ESP_Adafruit_SSD1306.h>
// #define OLED_RESET 4
int ktcSO = 12;
int ktcCS = 13;
int ktcCLK = 14;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
// Adafruit_SSD1306 display(OLED_RESET);
void setup() {
Serial.begin(115200);
Serial.println("Max6675 test");
delay(500);
// display.begin(SSD1306_SWITCHCAPVCC, 0x78>>1);
// display.display();
// delay(2000);
// display.clearDisplay();
}
void loop() {
// basic readout test
//
// display.setTextSize(2);
// display.setTextColor(WHITE);
// display.setCursor(52,10);
// display.print((char)223);
// display.print("C = ");
// display.print(ktc.readCelsius());
// display.print((char)223);
// display.print("\t F = ");
// display.setCursor(52,30);
// display.println(ktc.readFahrenheit());
float DC = ktc.readCelsius();
// Read temperature as Celsius
float DF = ktc.readFahrenheit();
Serial.print("C = ");
Serial.print(ktc.readCelsius());
Serial.print("\t F = ");
Serial.println(ktc.readFahrenheit());
//
delay(1000);
// displayData();
}
void displayData(){
// display.setTextSize(1);
// display.setCursor(5,1);
// display.println("14CORE|THERMOCOUPLE");
// display.setTextSize(2);
// display.setTextColor(WHITE);
// display.setCursor(18,20);
// display.print((char)248);
// display.print("C:");
// display.print(ktc.readCelsius());
// display.setCursor(18,40);
// display.print((char)248);
// display.print("F:");
// display.println(ktc.readFahrenheit());
// display.display();
// display.clearDisplay();
}
By replacing max6675.h and max6675.cpp with the ones from SirUli on github, copying other files on the link and commenting out the statements relating display functions, the sketch of 14Core has been compiled and run without fail on NodeMCU Dev Kit 1.0.
Also the serialthermocoupl.pde example sketch runs without fail.
I think the tutorials of 14Core had better specify from where the library they downloaded.
Wiring from 14Core
Output
Sketch
#include <max6675.h>
// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <ESP_Adafruit_SSD1306.h>
// #define OLED_RESET 4
int ktcSO = 12;
int ktcCS = 13;
int ktcCLK = 14;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
// Adafruit_SSD1306 display(OLED_RESET);
void setup() {
Serial.begin(115200);
Serial.println("Max6675 test");
delay(500);
// display.begin(SSD1306_SWITCHCAPVCC, 0x78>>1);
// display.display();
// delay(2000);
// display.clearDisplay();
}
void loop() {
// basic readout test
//
// display.setTextSize(2);
// display.setTextColor(WHITE);
// display.setCursor(52,10);
// display.print((char)223);
// display.print("C = ");
// display.print(ktc.readCelsius());
// display.print((char)223);
// display.print("\t F = ");
// display.setCursor(52,30);
// display.println(ktc.readFahrenheit());
float DC = ktc.readCelsius();
// Read temperature as Celsius
float DF = ktc.readFahrenheit();
Serial.print("C = ");
Serial.print(ktc.readCelsius());
Serial.print("\t F = ");
Serial.println(ktc.readFahrenheit());
//
delay(1000);
// displayData();
}
void displayData(){
// display.setTextSize(1);
// display.setCursor(5,1);
// display.println("14CORE|THERMOCOUPLE");
// display.setTextSize(2);
// display.setTextColor(WHITE);
// display.setCursor(18,20);
// display.print((char)248);
// display.print("C:");
// display.print(ktc.readCelsius());
// display.setCursor(18,40);
// display.print((char)248);
// display.print("F:");
// display.println(ktc.readFahrenheit());
// display.display();
// display.clearDisplay();
}
IPs And Domain Names of NTP Servers Available Publicly In Taiwan
From time to time I need an NTP server available publicly in Taiwan for reference to compare with my homebrew MakerNTP (posted on Dec. 24, 2015) Stratum 1 GPS-based NTP server, so I list some useful NTP servers.
1. time.stdtime.gov.tw: 118.163.81.61
2. tick.stdtime.gov.tw: 118.163.81.62
3. watch.stdtime.gov.tw: 118.163.81.63
4. tock.stdtime.gov.tw: 211.22.103.157
5. clock.stdtime.gov.tw: 211.22.103.158
6. tw.pool.ntp.org: A. 103.18.128.60 B. 117.56.223.235 C. 59.124.29.241 D. 59.125.122.217
1. time.stdtime.gov.tw: 118.163.81.61
2. tick.stdtime.gov.tw: 118.163.81.62
3. watch.stdtime.gov.tw: 118.163.81.63
4. tock.stdtime.gov.tw: 211.22.103.157
5. clock.stdtime.gov.tw: 211.22.103.158
6. tw.pool.ntp.org: A. 103.18.128.60 B. 117.56.223.235 C. 59.124.29.241 D. 59.125.122.217
2016/07/13
Sketch Displaying Temperature and Humidity on Serial Monitor Using ESP8266 NodeMCU Dev Kit 1.0 and DHT11
Output
Sketch
// Modified by Befun Hung on Jul. 13, 2016 with Arduino IDE supporting ESP8266
// Runs on NodeMCU Dev Kit 1.0 without fails with DHT11 breakout
// Original source code from 14core.com commented out OLED part and correct typos
// "Wiring DHT11, DHT22, with OLED Screen on ESP8266 12E"
// Wiring diagram show data pin of DHT connect D6 (GPIO 12)
// While the sketch define DHTPIN 14 (D5)
// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <ESP_Adafruit_SSD1306.h>
#include "ESP8266WiFi.h"
#include "DHT.h"
// #define OLED_RESET 4
// Adafruit_SSD1306 display(OLED_RESET);
// #define DHTPIN 12 // Connected to Pin D6 in NodeMCU
#define DHTPIN 14 // Connected to Pin D5 in NodeMCU
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
/*
byte img [] = {
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf0, 0x0, 0x0,
0x0, 0x0, 0x3f, 0xfc, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x1, 0xfc,
0x3f, 0x80, 0x0, 0x0, 0x3, 0xe0, 0x7, 0xc0, 0x0, 0x0, 0x7, 0x80, 0x1, 0xe0, 0x0,
0x0, 0xff, 0x0, 0x0, 0xff, 0x0, 0x3, 0xfe, 0x0, 0x0, 0x7f, 0xc0, 0x7, 0xe2, 0x0,
0x0, 0x4f, 0xe0, 0x7, 0x80, 0x0, 0x0, 0x1, 0xe0, 0xf, 0x0, 0x0, 0x0, 0x0, 0xf0,
0x1e, 0x0, 0x0, 0x0, 0x0, 0x70, 0xe, 0x0, 0x0, 0x0, 0x0, 0x78, 0xc, 0x0, 0x0,
0x0, 0x0, 0x30, 0xc, 0x0, 0x0, 0x0, 0x0, 0x30, 0xc, 0x0, 0x0, 0x0, 0x0, 0x30,
0xe, 0x0, 0x0, 0x0, 0x0, 0x70, 0x1e, 0x0, 0x0, 0x0, 0x0, 0x70, 0xf, 0x0, 0x0,
0x0, 0x0, 0xf0, 0x7, 0x80, 0x0, 0x0, 0x1, 0xe0, 0x7, 0xe3, 0x80, 0x1, 0xc7, 0xe0,
0x3, 0xff, 0xc0, 0x3, 0xff, 0xc0, 0x0, 0xff, 0xf0, 0xf, 0xff, 0x0, 0x0, 0x20, 0xfc,
0x3f, 0x4, 0x0, 0x0, 0x0, 0x3f, 0xfc, 0x0, 0x0, 0x0, 0x0, 0xf, 0xf0, 0x0, 0x0,
0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe, 0x0,
0x0, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1f, 0x0, 0x0, 0x0, 0x0,
0x0, 0x1f, 0x0, 0x0, 0x20, 0x0, 0x0, 0x1f, 0x0, 0x0, 0x20, 0x0, 0x0, 0xe, 0x0,
0x0, 0x70, 0x0, 0x0, 0x0, 0x1, 0x80, 0x70, 0x0, 0x0, 0x0, 0x1, 0x80, 0xf8, 0x0,
0x0, 0x0, 0x3, 0xc0, 0xf8, 0x0, 0x0, 0x0, 0x3, 0xc0, 0xf8, 0x0, 0x0, 0x0, 0x3,
0xc0, 0x70, 0x0, 0x0, 0x0, 0x3, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xc0, 0x0, 0x0,
0x0, 0x0, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
};
*/
void setup() {
Serial.begin(115200);
Serial.println("DHTxx test!");
// dht begin
dht.begin();
// set display
// display.begin(SSD1306_SWITCHCAPVCC, 0x78>>1);
// display.display();
// delay(2000);
// display.clearDisplay();
}
/*
void showTemp(float temp,float hud) {
display.drawBitmap(0, 5, img, 48, 50, 1);
display.setTextSize(1);
display.setCursor(3,0);
display.println("Temperature/Humidity");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(52,10);
display.print(temp);
display.println("C");
display.setCursor(52,30);
display.print(hud);
display.println("%");
display.setTextSize(1);
display.setCursor(52,50);
display.println("14CORE.COM");
display.display();
display.clearDisplay();
}
*/
void loop() {
delay(2000); // Wait a few seconds between measurements.
float h = dht.readHumidity();
// Serial.print("h= ");
// Serial.println(h);
float t = dht.readTemperature(); // Read temperature as Celsius
// Serial.print("t= ");
// Serial.println(t);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
Serial.println();
return;
}
Serial.print("Humidity: "); // show in serial monitor
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: "); // show in serial monitor
Serial.print(t);
Serial.print(" *C \n");
// showTemp(t,h); // show temp
}
2016/06/22
Install MQTT Broker Mosquitto on Raspberry Pi
I have installed the MQTT Broker Mosquitto on my Raspberry Pi Model B+ with Raspbian Jessie pre-installed following the procedure described below a few days ago. The version of Mosquitto installed is 1.4.9 supporting both MQTT protocol 3.1 and 3.1.1.
It is stated on module page of NodeMCU documentation website ( https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/ )that:
"The client adheres to version 3.1.1 of the MQTT protocol. Make sure that your broker supports and is correctly configured for version 3.1.1. The client is backwards incompatible with brokers running MQTT 3.1."
1. Update your packages:
sudo apt-get update
sudo apt-get upgrade
2. To use the new repository, import the repository package signing key:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo rm mosquitto-repo.gpg.key
3. Make the repository available to apt:
cd /etc/apt/sources.list.d/
4. Install the packages list for your version of Raspbian.
For Wheezy (old version)
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
For Jessie (latest version)
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
5. Update apt information and install Mosquitto:
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
It is stated on module page of NodeMCU documentation website ( https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/ )that:
"The client adheres to version 3.1.1 of the MQTT protocol. Make sure that your broker supports and is correctly configured for version 3.1.1. The client is backwards incompatible with brokers running MQTT 3.1."
1. Update your packages:
sudo apt-get update
sudo apt-get upgrade
2. To use the new repository, import the repository package signing key:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo rm mosquitto-repo.gpg.key
3. Make the repository available to apt:
cd /etc/apt/sources.list.d/
4. Install the packages list for your version of Raspbian.
For Wheezy (old version)
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
For Jessie (latest version)
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
5. Update apt information and install Mosquitto:
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
Additional Board Manager URLs For Arduino IDE
Here's my collection of additional board manager URLs for Arduino IDE version 1.6.4 or greater - don't use 1.6.2!
ESP8266
http://arduino.esp8266.com/stable/package_esp8266com_index.json
MediaTek LinkIt Smart 7688 DUO
http://download.labs.mediatek.com/package_mtk_linkit_smart_7688_test_index.json
MediaTek LinkIt One
http://download.labs.mediatek.com/package_mtk_linkitone_index.json
Realtek Ameba
https://github.com/Ameba8195/Arduino/raw/master/release/package_realtek.com_ameba_index.json
Subscribe to:
Posts (Atom)








