Showing posts with label Switch. Show all posts
Showing posts with label Switch. Show all posts
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
2014/11/09
Arduino RF Wireless Message Display Using I2C LCD Keypad Shield
The function of this sketch is same as the sketch posted on May. 11, 2014, but using I2C LCD Keypad Shield as the output device.
Sketch
/*
SimpleReceiveI2CLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
Modified by Befun Hung on Nov. 09, 2014
Use I2C LCD Keypad Shield as the output
*/
#include <VirtualWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// #define BACKLIGHT_PIN 13
LiquidCrystal_I2C lcd(0x20); //Set the LCD I2C address
// LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
void setup()
{
// pinMode(BACKLIGHT_PIN, OUTPUT);
// digitalWrite(BACKLIGHT_PIN, HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2); //initialize the lcd
// Print a message to the LCD.
lcd.home(); //go home
lcd.print("Device is ready!");
//
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
lcd.clear();
Serial.print(counter);
Serial.print(": ");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(counter);
lcd.print(":");
lcd.setCursor(0,1);
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
lcd.print(char(message[i]));
}
Serial.println();
counter++;
}
}
Sketch
/*
SimpleReceiveI2CLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
Modified by Befun Hung on Nov. 09, 2014
Use I2C LCD Keypad Shield as the output
*/
#include <VirtualWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// #define BACKLIGHT_PIN 13
LiquidCrystal_I2C lcd(0x20); //Set the LCD I2C address
// LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
void setup()
{
// pinMode(BACKLIGHT_PIN, OUTPUT);
// digitalWrite(BACKLIGHT_PIN, HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2); //initialize the lcd
// Print a message to the LCD.
lcd.home(); //go home
lcd.print("Device is ready!");
//
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
lcd.clear();
Serial.print(counter);
Serial.print(": ");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(counter);
lcd.print(":");
lcd.setCursor(0,1);
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
lcd.print(char(message[i]));
}
Serial.println();
counter++;
}
}
2014/10/29
Arduino Sketch for Address Settable RF Wireless Power Supply Socket/Switch
Updated Sketch (Nov. 15,2014)
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// This sketch uses VirtualWire
// Connect the Receiver data pin to Arduino pin 11
// Modified by Befun Hung on Nov. 14, 2014 by combining sketches on May. 11, 2014 (Arduino RF Wireless Message Display)
// and Jul. 30, 2014 (Use DIP Switch To Set Arduino Device Net ID And Node ID for Wireless Communication - Updated Sketch)
// and removing LCD Keypad Shield display function as a new sketch for Arduino addressable RF wireless power supply socket
// Modified by Befun Hung on Nov. 12, 2014 to use
// 4 digital pins(D2-D5) to set node ID1 and
// 4 digital pins(D6-D9) to set node ID2 by using 10K pull-up resistors.
// Totally 8x16x16=2,048 devices can be used for wireless communication
// For practical use 8x10x10=800 devices can be controlled using digital key of 3x4 telephone keypad
// Wire and DIP switch connections from high bit to low bit are A1, A2, A3, D2, D3, D4, D5, D6, D7, D8 and D9
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D13 is used for singal output
// Don't run this sketch on Arduino Pro Mini, it may reboot repeatedly owing to bootloader not supporting watchdog
#include <VirtualWire.h>
// include avr/wdt for watchdog reset
#include <avr/wdt.h>
// include <EEPROM.h> to save switch staus to EEPROM
#include <EEPROM.h>
// keys to turn switch on and off
#define keyOn '*'
#define keyOff '#'
//Create and Define Global Variables
int bits=4, netID, nodeID1, nodeID2;
int dipNet[] = {15, 16, 17}; // DIP Switch Pins for Net ID
int dipNode1[] = {2, 3, 4, 5}; // DIP Switch Pins for Node ID1
int dipNode2[] = {6, 7, 8, 9}; // DIP Switch Pins for Node ID2
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// digital pin 13 connected with a 1K resistor and a LED
int led = 13;
// Address of EEPROM for saving switch status and variable to store value
int address = 5;
int value;
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode1[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID1 = node1Address(bits);
// delay(100);
Serial.print("Node ID1: ");
Serial.println(nodeID1);
for(i = 0; i<bits; i++){
pinMode(dipNode2[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID2 = node2Address(bits);
// delay(100);
Serial.print("Node ID2: ");
Serial.println(nodeID2);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
//initialize the digital 13 pin as output
pinMode(led, OUTPUT);
// When the receiver is unstable and reboot, reading stored status from EEPROM
value = EEPROM.read(address);
if (value == '1')
{
digitalWrite(led, HIGH);
}
if (value == '0')
{
digitalWrite(led, LOW);
}
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print(counter);
Serial.print(": ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
// if length of received message is less than transmitter sent meaning the receiver is unstable
if (messageLength < 4)
{
software_Reboot();
}
else
{
// netID and nodeID are numbers and message is charactor array, so convention is required
if ((message[0] == (netID + '0')) && (message[1] == (nodeID1 + '0')) && (message[2] == (nodeID2 + '0')))
{
if (message[3] == keyOn)
{
digitalWrite(led, HIGH);
EEPROM.write(address, '1');
}
else
{
if (message[3] == keyOff)
{
digitalWrite(led, LOW);
EEPROM.write(address, '0');
}
}
}
}
Serial.println();
counter++;
}
}
// Software reboot function
void software_Reboot()
{
wdt_enable(WDTO_15MS);
while (1)
{
}
}
//Create Net Address from DIP Switch (3 positions used A1-A3)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D2-D5)
byte node1Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode1[i])); // read the input pin
}
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D6-D9)
byte node2Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode2[i])); // read the input pin
}
return j; //return address
}
Sketch
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// This sketch uses VirtualWire
// Connect the Receiver data pin to Arduino pin 11
// Modified by Befun Hung on Oct. 29, 2014 by combining sketches on May. 11, 2014 (Arduino RF Wireless Message Display)
// and Jul. 30, 2014 (Use DIP Switch To Set Arduino Device Net ID And Node ID for Wireless Communication)
// and removing LCD Keypad Shield display function as a new sketch for Arduino addressable RF wireless power socket
// The sketch uses 3 analog pins(A1-A3) to set net ID and 8 digital pins(D2-D9) to set node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// When using a 3x4 telephone keypad, 8x10(0-9)=80 devices can be used
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D13 is used for singal output
#include <VirtualWire.h>
// include avr/wdt for watchdog reset
#include <avr/wdt.h>
// include <EEPROM.h> to save switch staus to EEPROM
#include <EEPROM.h>
// keys to turn switch on and off
#define keyOn '1'
#define keyOff '0'
//Create and Define Global Variables
int bits=8;
byte netID, nodeID;
int dipNet[] = {15, 16, 17}; //DIP Switch Pins for Net ID
int dipNode[] = {2, 3, 4, 5, 6, 7, 8, 9}; //DIP Switch Pins for Node ID
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// digital pin 13 connected with a 1K resistor and a LED
int led = 13;
// Address of EEPROM for saving switch status and variable to store value
int address = 5;
int value;
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode[i], INPUT); // sets the digital pin 2-9 as input
}
nodeID = nodeAddress(bits);
Serial.print("Node ID: ");
Serial.println(nodeID);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
//initialize the digital 13 pin as output
pinMode(led, OUTPUT);
// When the receiver is unstable and reboot, reading stored status from EEPROM
value = EEPROM.read(address);
if (value == '1')
{
digitalWrite(led, HIGH);
}
if (value == '0')
{
digitalWrite(led, LOW);
}
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print(counter);
Serial.print(": ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
// if length of received message is less than transmitter sent meaning the receiver is unstable
if (messageLength < 3)
{
software_Reboot();
}
else
{
// netID and nodeID are numbers and message is charactor array, so convention is required
if ((message[0] == (netID + '0')) && (message[1] == (nodeID + '0')))
{
if (message[2] == keyOn)
{
digitalWrite(led, HIGH);
EEPROM.write(address, '1');
}
else
{
if (message[2] == keyOff)
{
digitalWrite(led, LOW);
EEPROM.write(address, '0');
}
}
}
}
Serial.println();
counter++;
}
}
// Software reboot function
void software_Reboot()
{
wdt_enable(WDTO_15MS);
while (1)
{
}
}
//Create Net Address from DIP Switch (3 positions used)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node Address from DIP Switch (8 positions used D2 - D9)
byte nodeAddress(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode[i])); // read the input pin
}
return j; //return address
}
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// This sketch uses VirtualWire
// Connect the Receiver data pin to Arduino pin 11
// Modified by Befun Hung on Nov. 14, 2014 by combining sketches on May. 11, 2014 (Arduino RF Wireless Message Display)
// and Jul. 30, 2014 (Use DIP Switch To Set Arduino Device Net ID And Node ID for Wireless Communication - Updated Sketch)
// and removing LCD Keypad Shield display function as a new sketch for Arduino addressable RF wireless power supply socket
// Modified by Befun Hung on Nov. 12, 2014 to use
// 4 digital pins(D2-D5) to set node ID1 and
// 4 digital pins(D6-D9) to set node ID2 by using 10K pull-up resistors.
// Totally 8x16x16=2,048 devices can be used for wireless communication
// For practical use 8x10x10=800 devices can be controlled using digital key of 3x4 telephone keypad
// Wire and DIP switch connections from high bit to low bit are A1, A2, A3, D2, D3, D4, D5, D6, D7, D8 and D9
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D13 is used for singal output
// Don't run this sketch on Arduino Pro Mini, it may reboot repeatedly owing to bootloader not supporting watchdog
#include <VirtualWire.h>
// include avr/wdt for watchdog reset
#include <avr/wdt.h>
// include <EEPROM.h> to save switch staus to EEPROM
#include <EEPROM.h>
// keys to turn switch on and off
#define keyOn '*'
#define keyOff '#'
//Create and Define Global Variables
int bits=4, netID, nodeID1, nodeID2;
int dipNet[] = {15, 16, 17}; // DIP Switch Pins for Net ID
int dipNode1[] = {2, 3, 4, 5}; // DIP Switch Pins for Node ID1
int dipNode2[] = {6, 7, 8, 9}; // DIP Switch Pins for Node ID2
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// digital pin 13 connected with a 1K resistor and a LED
int led = 13;
// Address of EEPROM for saving switch status and variable to store value
int address = 5;
int value;
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode1[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID1 = node1Address(bits);
// delay(100);
Serial.print("Node ID1: ");
Serial.println(nodeID1);
for(i = 0; i<bits; i++){
pinMode(dipNode2[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID2 = node2Address(bits);
// delay(100);
Serial.print("Node ID2: ");
Serial.println(nodeID2);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
//initialize the digital 13 pin as output
pinMode(led, OUTPUT);
// When the receiver is unstable and reboot, reading stored status from EEPROM
value = EEPROM.read(address);
if (value == '1')
{
digitalWrite(led, HIGH);
}
if (value == '0')
{
digitalWrite(led, LOW);
}
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print(counter);
Serial.print(": ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
// if length of received message is less than transmitter sent meaning the receiver is unstable
if (messageLength < 4)
{
software_Reboot();
}
else
{
// netID and nodeID are numbers and message is charactor array, so convention is required
if ((message[0] == (netID + '0')) && (message[1] == (nodeID1 + '0')) && (message[2] == (nodeID2 + '0')))
{
if (message[3] == keyOn)
{
digitalWrite(led, HIGH);
EEPROM.write(address, '1');
}
else
{
if (message[3] == keyOff)
{
digitalWrite(led, LOW);
EEPROM.write(address, '0');
}
}
}
}
Serial.println();
counter++;
}
}
// Software reboot function
void software_Reboot()
{
wdt_enable(WDTO_15MS);
while (1)
{
}
}
//Create Net Address from DIP Switch (3 positions used A1-A3)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D2-D5)
byte node1Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode1[i])); // read the input pin
}
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D6-D9)
byte node2Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode2[i])); // read the input pin
}
return j; //return address
}
Sketch
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// This sketch uses VirtualWire
// Connect the Receiver data pin to Arduino pin 11
// Modified by Befun Hung on Oct. 29, 2014 by combining sketches on May. 11, 2014 (Arduino RF Wireless Message Display)
// and Jul. 30, 2014 (Use DIP Switch To Set Arduino Device Net ID And Node ID for Wireless Communication)
// and removing LCD Keypad Shield display function as a new sketch for Arduino addressable RF wireless power socket
// The sketch uses 3 analog pins(A1-A3) to set net ID and 8 digital pins(D2-D9) to set node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// When using a 3x4 telephone keypad, 8x10(0-9)=80 devices can be used
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D13 is used for singal output
#include <VirtualWire.h>
// include avr/wdt for watchdog reset
#include <avr/wdt.h>
// include <EEPROM.h> to save switch staus to EEPROM
#include <EEPROM.h>
// keys to turn switch on and off
#define keyOn '1'
#define keyOff '0'
//Create and Define Global Variables
int bits=8;
byte netID, nodeID;
int dipNet[] = {15, 16, 17}; //DIP Switch Pins for Net ID
int dipNode[] = {2, 3, 4, 5, 6, 7, 8, 9}; //DIP Switch Pins for Node ID
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// digital pin 13 connected with a 1K resistor and a LED
int led = 13;
// Address of EEPROM for saving switch status and variable to store value
int address = 5;
int value;
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode[i], INPUT); // sets the digital pin 2-9 as input
}
nodeID = nodeAddress(bits);
Serial.print("Node ID: ");
Serial.println(nodeID);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
//initialize the digital 13 pin as output
pinMode(led, OUTPUT);
// When the receiver is unstable and reboot, reading stored status from EEPROM
value = EEPROM.read(address);
if (value == '1')
{
digitalWrite(led, HIGH);
}
if (value == '0')
{
digitalWrite(led, LOW);
}
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print(counter);
Serial.print(": ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
// if length of received message is less than transmitter sent meaning the receiver is unstable
if (messageLength < 3)
{
software_Reboot();
}
else
{
// netID and nodeID are numbers and message is charactor array, so convention is required
if ((message[0] == (netID + '0')) && (message[1] == (nodeID + '0')))
{
if (message[2] == keyOn)
{
digitalWrite(led, HIGH);
EEPROM.write(address, '1');
}
else
{
if (message[2] == keyOff)
{
digitalWrite(led, LOW);
EEPROM.write(address, '0');
}
}
}
}
Serial.println();
counter++;
}
}
// Software reboot function
void software_Reboot()
{
wdt_enable(WDTO_15MS);
while (1)
{
}
}
//Create Net Address from DIP Switch (3 positions used)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node Address from DIP Switch (8 positions used D2 - D9)
byte nodeAddress(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode[i])); // read the input pin
}
return j; //return address
}
2014/07/30
Use DIP Switch To Set Arduino Device Net ID And Node ID for Wireless Communication
To use one remote control to many switch radio frequency wireless power supply sockets, each one must has its unique ID to identify itself and act according commands received from the remote control.
In the example sketch,A2-A4 A1-A3 are set to 1, 0, 1; D2-D9 are set to 0,0,0,0,1,0,1,0. So the net ID is calculated as 1*4+0*2+1*1=5; node ID is calculated as 0*128+0*64+0*32+0*16+1*8+0*4+1*2+0*1=10.
Updated on Nov. 12, 2014
Use the same DIP setting above, the output will be :
Net ID: 5
Node ID1: 0
Node ID2:10
Photo of Connections
Output
Update Sketch (Nov. 12, 2014)
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// Modified by Befun Hung on Jul. 30, 2014 to use
// 3 analog pins(A1-A3) to set net ID
// 8 digital pins(D2-D9) to set Node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// For practical use 8x10=80 devices can be controlled using digital key of 3x4 telephone keypad
// Modified by Befun Hung on Nov. 12, 2014 to use
// 4 digital pins(D2-D5) to set node ID1 and
// 4 digital pins(D6-D9) to set node ID2 by using 10K pull-up resistors.
// Totally 8x16x16=2,048 devices can be used for wireless communication
// For practical use 8x10x10=800 devices can be controlled using digital key of 3x4 telephone keypad
// Wire and DIP switch connections from high bit to low bit are A1, A2, A3, D2, D3, D4, D5, D6, D7, D8 and D9
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D10-D13 are reserved for SPI
//Create and Define Global Variables
int bits=4, netID, nodeID1, nodeID2;
int dipNet[] = {15, 16, 17}; // DIP Switch Pins for Net ID
int dipNode1[] = {2, 3, 4, 5}; // DIP Switch Pins for Node ID1
int dipNode2[] = {6, 7, 8, 9}; // DIP Switch Pins for Node ID2
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode1[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID1 = node1Address(bits);
// delay(100);
Serial.print("Node ID1: ");
Serial.println(nodeID1);
for(i = 0; i<bits; i++){
pinMode(dipNode2[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID2 = node2Address(bits);
// delay(100);
Serial.print("Node ID2: ");
Serial.println(nodeID2);
}
void loop()
{
}
//Create Net Address from DIP Switch (3 positions used A1-A3)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D2-D5)
byte node1Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode1[i])); // read the input pin
}
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D6-D9)
byte node2Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode2[i])); // read the input pin
}
return j; //return address
}
Sketch
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// Modified by Befun Hung on Jul. 30, 2014 to use 3 analog pins(A1-A3) to set net ID and 8 digital pins(D2-D9) to set node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D10-D13 are reserved for SPI
//Create and Define Global Variables
int bits=8, netID, nodeID;
int dipNet[] = {15, 16, 17}; //DIP Switch Pins for Net ID
int dipNode[] = {2, 3, 4, 5, 6, 7, 8, 9}; //DIP Switch Pins for Node ID
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode[i], INPUT); // sets the digital pin 2-9 as input
}
nodeID = nodeAddress(bits);
// delay(100);
Serial.print("Node ID: ");
Serial.println(nodeID);
}
void loop()
{
}
//Create Net Address from DIP Switch (3 positions used)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node Address from DIP Switch (8 positions used D2 - D9)
byte nodeAddress(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode[i])); // read the input pin
}
return j; //return address
}
In the example sketch,
Updated on Nov. 12, 2014
Use the same DIP setting above, the output will be :
Net ID: 5
Node ID1: 0
Node ID2:10
Photo of Connections
Output
Update Sketch (Nov. 12, 2014)
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// Modified by Befun Hung on Jul. 30, 2014 to use
// 3 analog pins(A1-A3) to set net ID
// 8 digital pins(D2-D9) to set Node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// For practical use 8x10=80 devices can be controlled using digital key of 3x4 telephone keypad
// Modified by Befun Hung on Nov. 12, 2014 to use
// 4 digital pins(D2-D5) to set node ID1 and
// 4 digital pins(D6-D9) to set node ID2 by using 10K pull-up resistors.
// Totally 8x16x16=2,048 devices can be used for wireless communication
// For practical use 8x10x10=800 devices can be controlled using digital key of 3x4 telephone keypad
// Wire and DIP switch connections from high bit to low bit are A1, A2, A3, D2, D3, D4, D5, D6, D7, D8 and D9
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D10-D13 are reserved for SPI
//Create and Define Global Variables
int bits=4, netID, nodeID1, nodeID2;
int dipNet[] = {15, 16, 17}; // DIP Switch Pins for Net ID
int dipNode1[] = {2, 3, 4, 5}; // DIP Switch Pins for Node ID1
int dipNode2[] = {6, 7, 8, 9}; // DIP Switch Pins for Node ID2
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode1[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID1 = node1Address(bits);
// delay(100);
Serial.print("Node ID1: ");
Serial.println(nodeID1);
for(i = 0; i<bits; i++){
pinMode(dipNode2[i], INPUT); // sets the digital pin 2-5 as input
}
nodeID2 = node2Address(bits);
// delay(100);
Serial.print("Node ID2: ");
Serial.println(nodeID2);
}
void loop()
{
}
//Create Net Address from DIP Switch (3 positions used A1-A3)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D2-D5)
byte node1Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode1[i])); // read the input pin
}
return j; //return address
}
//Create Node1 Address from DIP Switch (4 positions used D6-D9)
byte node2Address(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode2[i])); // read the input pin
}
return j; //return address
}
Sketch
// Released by abocanegra on Dec. 11, 2009 using 4 pins, "DIP Switch 8 Position to Address Sensor" in Arduino Forum
// Modified by Befun Hung on Jul. 30, 2014 to use 3 analog pins(A1-A3) to set net ID and 8 digital pins(D2-D9) to set node ID by using 10K pull-up resistors.
// Totally 8x256=2,048 devices can be used for wireless communication
// A0 is reserved for analog sensor or digital sensor/actuator
// A4-A5 are reserved for I2C
// D0-D1 are reserved for RX/TX
// D10-D13 are reserved for SPI
//Create and Define Global Variables
int bits=8, netID, nodeID;
int dipNet[] = {15, 16, 17}; //DIP Switch Pins for Net ID
int dipNode[] = {2, 3, 4, 5, 6, 7, 8, 9}; //DIP Switch Pins for Node ID
void setup()
{
Serial.begin(9600);
int i;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
netID = netAddress();
// delay(100);
Serial.print("Net ID: ");
Serial.println(netID);
for(i = 0; i<bits; i++){
pinMode(dipNode[i], INPUT); // sets the digital pin 2-9 as input
}
nodeID = nodeAddress(bits);
// delay(100);
Serial.print("Node ID: ");
Serial.println(nodeID);
}
void loop()
{
}
//Create Net Address from DIP Switch (3 positions used)
byte netAddress(){
int i,j=0;
//Get the switches state
j = (j << 1) | (! digitalRead(A1)); // read the input pin
j = (j << 1) | (! digitalRead(A2)); // read the input pin
j = (j << 1) | (! digitalRead(A3)); // read the input pin
return j; //return address
}
//Create Node Address from DIP Switch (8 positions used D2 - D9)
byte nodeAddress(int k){
int i,j=0;
//Get the switches state
for(i=0; i<k; i++){
j = (j << 1) | (! digitalRead(dipNode[i])); // read the input pin
}
return j; //return address
}
2014/05/11
Arduino RF Wireless Message Display
As I am building the RF wireless power socket for home automation, I need a stand along tool to display any wireless received message. During the lab, I use Arduino serial monitor to monitor the message received once a second, error always happens at different time point as following screen shot shows. After disconnecting the Arduino Uno from PC to monitor message received by using LCD keypad shield as a stand along system, the error condition gone.
The RF wireless message display can be used to display the time on the NTP synchronized RTC clock (my projects during Aug 2013), so no additional time adjustment at display side is needed.
Parts Used
1. Arduino Uno R3
2. cheaphousetek LCD Keypad Shield
3. RF Receiver (described in Arduino Cookbook 14.1)
Connection
1. Stack LCD Key Keypad Upon Arduino Uno R3
2. RF Receiver Data Pin - Arduino Uno D11
3. RF Receiver Vcc - Arduino Uno 5V
4. RF Receiver Gnd - Arduino Uno Gnd
Photo
The sketch runs correctly with output to LCD shield for 247,610 times.
Sketch
/*
SimpleReceiveLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
// include the library code:
#include <LiquidCrystal.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
lcd.clear();
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
lcd.clear();
Serial.print(counter);
Serial.print(": ");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(counter);
lcd.print(":");
lcd.setCursor(0,1);
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
lcd.print(char(message[i]));
}
Serial.println();
counter++;
}
}
The RF wireless message display can be used to display the time on the NTP synchronized RTC clock (my projects during Aug 2013), so no additional time adjustment at display side is needed.
Parts Used
1. Arduino Uno R3
2. cheaphousetek LCD Keypad Shield
3. RF Receiver (described in Arduino Cookbook 14.1)
Connection
1. Stack LCD Key Keypad Upon Arduino Uno R3
2. RF Receiver Data Pin - Arduino Uno D11
3. RF Receiver Vcc - Arduino Uno 5V
4. RF Receiver Gnd - Arduino Uno Gnd
Photo
The sketch runs correctly with output to LCD shield for 247,610 times.
Sketch
/*
SimpleReceiveLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
// include the library code:
#include <LiquidCrystal.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
lcd.clear();
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
lcd.clear();
Serial.print(counter);
Serial.print(": ");
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print(counter);
lcd.print(":");
lcd.setCursor(0,1);
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
lcd.print(char(message[i]));
}
Serial.println();
counter++;
}
}
2012/11/16
Freeduino/Arduino Web Controlled (Configured) Automatic Switches
Inspired by Freeduino/Arduino Web Sensors And Switches on cheaphousetek blog on Oct. 20, 2012, go a step forward, automatically controll the switches by setting the sensors criteria. It comes out the sketch at the bottom of this post.
By setting the lower limits, upper limits and switch on/off using a browser, one can monitor the conditions of all switches remotely.
To control the web server outside the local area network, the NAT function of IP router should be enabled. To secure your web server, the SSH or VNC connection should be established.
Pins Used
Sensors: A2, A3
Switches: D2, D3, D4, D5, D6, D7, D8, D9
Output
Sketch
// Freeduino/Arduino Web Controlled Automatic Switches version 1.0
// By Befun Hung on Nov. 16 2012
// Modified From Freeduino/Arduino Web Sensors And Switches At cheaphousetek Blog
// The Sketch Works On Arduino IDE 1.02
#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
#define sensorPinStart 2
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(100);
String minValue, maxValue;
int defaultMinValue[] = {55, 56, 57, 58, 59, 60, 61, 62, 63};
int defaultMaxValue[] = {60, 61, 62, 63, 64, 65, 66, 67, 68};
String defaultOnValue[] = {"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low"};
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 }; //pin num 0 in arry is not used
int numofleds = 8; //number of switches
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);
void setup()
{
delay(250);
Serial.begin(9600);
Ethernet.begin(mac, ip,gateway,subnet);
server.begin();
//set pin mode
for (int j = 1; j < (numofleds + 1); j++)
{
pinMode(led[j], OUTPUT);
}
Serial.println("Serial READY");
Serial.println("Ethernet READY");
Serial.println("Server READY");
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (inString.length() < 35)
{
inString.concat(c);
}
if (c == '\n' && current_line_is_blank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<body><form method=get>");
client.println("<center><p><h1>Freeduino/Arduino Web Controlled Automatic Switches</h1></p><hr></center>");
client.println("<h2>");
// Sensor Reading and Setting -----------------------------------------------------------------------------
client.println("Sensor Reading And Setting:<br>");
for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
{
minValue = String("Low") + analogChannel;
// fixValue = String("Fix") + analogChannel;
maxValue = String("High") + analogChannel;
Led = String("RelayOn") + analogChannel;
int sensorReading = analogRead(analogChannel);
if (inString.indexOf(minValue+"=Dn") > 0) defaultMinValue[analogChannel] -= 1;
if (inString.indexOf(minValue+"=Up") > 0) defaultMinValue[analogChannel] += 1;
if (inString.indexOf(maxValue+"=Dn") > 0) defaultMaxValue[analogChannel] -= 1;
if (inString.indexOf(maxValue+"=Up") > 0) defaultMaxValue[analogChannel] += 1;
if (inString.indexOf(Led+"=High") > 0) defaultOnValue[analogChannel] = "Low";
if (inString.indexOf(Led+"=Low") > 0) defaultOnValue[analogChannel] = "High";
client.print("Input A");
client.print(analogChannel);
client.print(":");
client.print(sensorReading);
client.print(" Low");
// client.print(analogChannel);
client.print(":");
client.print(defaultMinValue[analogChannel]);
client.print("<input type=submit name="+minValue+" value=Dn><input type=submit name="+minValue+" value=Fix><input type=submit name="+minValue+" value=Up>");
client.print(" High");
// client.print(analogChannel);
client.print(":");
client.print(defaultMaxValue[analogChannel]);
client.print("<input type=submit name="+maxValue+" value=Dn><input type=submit name="+maxValue+" value=Fix><input type=submit name="+maxValue+" value=Up>");
client.print(" On");
// client.print(analogChannel);
client.print(":");
client.print("<input type=submit name="+Led+" value="+defaultOnValue[analogChannel]+">");
client.println("<br>");
}
// Switch Monitoring -------------------------------------------------------------------------------------------
client.println("<p></p>Switches Status:<br>");
for (int i=1; i < (noOfSensors+1) ; i++)
{
Led = String("Switch") + i;
int sensorReading = analogRead(sensorPinStart+i-1);
if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
digitalWrite(led[i], HIGH);
value[i] = "on";
}
if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
digitalWrite(led[i], LOW);
value[i] = "off";
}
if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
digitalWrite(led[i], HIGH);
value[i] = "on";
}
if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
digitalWrite(led[i], LOW);
value[i] = "off";
}
client.println(Led+" <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
}
client.println("</form><hr></h2>");
client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
client.println("</html></body>");
break;
}
if (c == '\n')
{
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r')
{
// we've gotten a character on the current line
current_line_is_blank = false;
}
} // End of if (client.available)
} // End of while (client.connected())
// give the web browser time to receive the data
delay(1);
inString = "";
client.stop();
} //End of if (client)
}
By setting the lower limits, upper limits and switch on/off using a browser, one can monitor the conditions of all switches remotely.
To control the web server outside the local area network, the NAT function of IP router should be enabled. To secure your web server, the SSH or VNC connection should be established.
Pins Used
Sensors: A2, A3
Switches: D2, D3, D4, D5, D6, D7, D8, D9
Output
Sketch
// Freeduino/Arduino Web Controlled Automatic Switches version 1.0
// By Befun Hung on Nov. 16 2012
// Modified From Freeduino/Arduino Web Sensors And Switches At cheaphousetek Blog
// The Sketch Works On Arduino IDE 1.02
#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
#define sensorPinStart 2
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(100);
String minValue, maxValue;
int defaultMinValue[] = {55, 56, 57, 58, 59, 60, 61, 62, 63};
int defaultMaxValue[] = {60, 61, 62, 63, 64, 65, 66, 67, 68};
String defaultOnValue[] = {"Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low"};
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 }; //pin num 0 in arry is not used
int numofleds = 8; //number of switches
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);
void setup()
{
delay(250);
Serial.begin(9600);
Ethernet.begin(mac, ip,gateway,subnet);
server.begin();
//set pin mode
for (int j = 1; j < (numofleds + 1); j++)
{
pinMode(led[j], OUTPUT);
}
Serial.println("Serial READY");
Serial.println("Ethernet READY");
Serial.println("Server READY");
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (inString.length() < 35)
{
inString.concat(c);
}
if (c == '\n' && current_line_is_blank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<body><form method=get>");
client.println("<center><p><h1>Freeduino/Arduino Web Controlled Automatic Switches</h1></p><hr></center>");
client.println("<h2>");
// Sensor Reading and Setting -----------------------------------------------------------------------------
client.println("Sensor Reading And Setting:<br>");
for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
{
minValue = String("Low") + analogChannel;
// fixValue = String("Fix") + analogChannel;
maxValue = String("High") + analogChannel;
Led = String("RelayOn") + analogChannel;
int sensorReading = analogRead(analogChannel);
if (inString.indexOf(minValue+"=Dn") > 0) defaultMinValue[analogChannel] -= 1;
if (inString.indexOf(minValue+"=Up") > 0) defaultMinValue[analogChannel] += 1;
if (inString.indexOf(maxValue+"=Dn") > 0) defaultMaxValue[analogChannel] -= 1;
if (inString.indexOf(maxValue+"=Up") > 0) defaultMaxValue[analogChannel] += 1;
if (inString.indexOf(Led+"=High") > 0) defaultOnValue[analogChannel] = "Low";
if (inString.indexOf(Led+"=Low") > 0) defaultOnValue[analogChannel] = "High";
client.print("Input A");
client.print(analogChannel);
client.print(":");
client.print(sensorReading);
client.print(" Low");
// client.print(analogChannel);
client.print(":");
client.print(defaultMinValue[analogChannel]);
client.print("<input type=submit name="+minValue+" value=Dn><input type=submit name="+minValue+" value=Fix><input type=submit name="+minValue+" value=Up>");
client.print(" High");
// client.print(analogChannel);
client.print(":");
client.print(defaultMaxValue[analogChannel]);
client.print("<input type=submit name="+maxValue+" value=Dn><input type=submit name="+maxValue+" value=Fix><input type=submit name="+maxValue+" value=Up>");
client.print(" On");
// client.print(analogChannel);
client.print(":");
client.print("<input type=submit name="+Led+" value="+defaultOnValue[analogChannel]+">");
client.println("<br>");
}
// Switch Monitoring -------------------------------------------------------------------------------------------
client.println("<p></p>Switches Status:<br>");
for (int i=1; i < (noOfSensors+1) ; i++)
{
Led = String("Switch") + i;
int sensorReading = analogRead(sensorPinStart+i-1);
if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
digitalWrite(led[i], HIGH);
value[i] = "on";
}
if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "High")) {
digitalWrite(led[i], LOW);
value[i] = "off";
}
if ((sensorReading < defaultMinValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
digitalWrite(led[i], HIGH);
value[i] = "on";
}
if ((sensorReading > defaultMaxValue[sensorPinStart+i-1]) && (defaultOnValue[sensorPinStart+i-1] == "Low")) {
digitalWrite(led[i], LOW);
value[i] = "off";
}
client.println(Led+" <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
}
client.println("</form><hr></h2>");
client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
client.println("</html></body>");
break;
}
if (c == '\n')
{
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r')
{
// we've gotten a character on the current line
current_line_is_blank = false;
}
} // End of if (client.available)
} // End of while (client.connected())
// give the web browser time to receive the data
delay(1);
inString = "";
client.stop();
} //End of if (client)
}
Labels:
Arduino,
Automatic,
Browser,
Chrome,
Configured,
Controlled,
Controller,
Ethernet,
Freeduino,
IP router,
NAT,
router,
Sensor,
SSH,
Switch,
Switched,
VNC,
W5100,
W5100 Ethernet Shield,
Web
2012/10/20
Freeduino/Arduino Web Sensors And Switches
Arduino Uno/Duemilanove is an useful gadget, combined with an W5100 Ethernet Shield , linked with some analog sensors and relays by wires, one can easily implements an simple remote web sensor monitoring and switch controlling system using an browser.
Pins Used
W5100 Ethernet Shield -- D10, D11, D12, D13
Analog Sensors -- A0, A1, A2, A3, A4, A5
Switches -- D2, D3, D4, D5, D6, D7, D8, D9
Output On Chrome Browser
Sketch
// modified from Instructables Arduino Led Server and Arduino Example WebServer
// by Befun Hung on Oct. 20, 2012
// the sketch works on Arduino IDE 1.01
#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
#define sensorPinStart 0 //change to 2 if using Arduino Ethernet Shield 05 or 06
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(35);
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8,9 }; //Led pins num 0 in arry is not used
int numofleds = 8; //numofleds
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);
String data;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip,gateway,subnet);
server.begin();
//set pin mode
for (int j = 1; j < (numofleds + 1); j++)
{
pinMode(led[j], OUTPUT);
}
Serial.println("Serial READY");
Serial.println("Ethernet READY");
Serial.println("Server READY");
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (inString.length() < 35)
{
inString.concat(c);
}
if (c == '\n' && current_line_is_blank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<body><form method=get>");
client.println("<center><p><h1>Freeduino/Arduino Web Sensor And Switch</h1></p><hr></center>");
client.println("<h2>");
client.println("Sensor Reading:<br>");
for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
{
int sensorReading = analogRead(analogChannel);
client.print("Analog Input ");
client.print(analogChannel);
client.print(" : ");
client.print(sensorReading);
client.println("<br>");
}
client.println("<p></p>Switches Status:<br>");
for(int i=1;i < (numofleds + 1) ;i++)
{
Led = String("Switch") + i;
if(inString.indexOf(Led+"=off")>0 || inString.indexOf("all=on")>0)
{
Serial.println(Led+"on");
digitalWrite(led[i], HIGH);
value[i] = "on";
}
else if(inString.indexOf(Led+"=on")>0 || inString.indexOf("all=off")>0 )
{
Serial.println(Led+"off");
digitalWrite(led[i], LOW);
value[i] = "off";
}
client.println(Led+" <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
}
client.println("<p></p>Switches Action:<br>");
client.println("All <input type=submit name=all value=on><input type=submit name=all value=off>");
client.println("</form><hr></h2>");
client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
client.println("</html></body>");
break;
}
if (c == '\n')
{
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r')
{
// we've gotten a character on the current line
current_line_is_blank = false;
}
} // End of if (client.available)
} // End of while (client.connected())
// give the web browser time to receive the data
delay(1);
inString = "";
client.stop();
} //End of if (client)
}
Pins Used
W5100 Ethernet Shield -- D10, D11, D12, D13
Analog Sensors -- A0, A1, A2, A3, A4, A5
Switches -- D2, D3, D4, D5, D6, D7, D8, D9
Output On Chrome Browser
Sketch
// modified from Instructables Arduino Led Server and Arduino Example WebServer
// by Befun Hung on Oct. 20, 2012
// the sketch works on Arduino IDE 1.01
#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
#define sensorPinStart 0 //change to 2 if using Arduino Ethernet Shield 05 or 06
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(35);
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8,9 }; //Led pins num 0 in arry is not used
int numofleds = 8; //numofleds
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);
String data;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip,gateway,subnet);
server.begin();
//set pin mode
for (int j = 1; j < (numofleds + 1); j++)
{
pinMode(led[j], OUTPUT);
}
Serial.println("Serial READY");
Serial.println("Ethernet READY");
Serial.println("Server READY");
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (inString.length() < 35)
{
inString.concat(c);
}
if (c == '\n' && current_line_is_blank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<body><form method=get>");
client.println("<center><p><h1>Freeduino/Arduino Web Sensor And Switch</h1></p><hr></center>");
client.println("<h2>");
client.println("Sensor Reading:<br>");
for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
{
int sensorReading = analogRead(analogChannel);
client.print("Analog Input ");
client.print(analogChannel);
client.print(" : ");
client.print(sensorReading);
client.println("<br>");
}
client.println("<p></p>Switches Status:<br>");
for(int i=1;i < (numofleds + 1) ;i++)
{
Led = String("Switch") + i;
if(inString.indexOf(Led+"=off")>0 || inString.indexOf("all=on")>0)
{
Serial.println(Led+"on");
digitalWrite(led[i], HIGH);
value[i] = "on";
}
else if(inString.indexOf(Led+"=on")>0 || inString.indexOf("all=off")>0 )
{
Serial.println(Led+"off");
digitalWrite(led[i], LOW);
value[i] = "off";
}
client.println(Led+" <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
}
client.println("<p></p>Switches Action:<br>");
client.println("All <input type=submit name=all value=on><input type=submit name=all value=off>");
client.println("</form><hr></h2>");
client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
client.println("</html></body>");
break;
}
if (c == '\n')
{
// we're starting a new line
current_line_is_blank = true;
}
else if (c != '\r')
{
// we've gotten a character on the current line
current_line_is_blank = false;
}
} // End of if (client.available)
} // End of while (client.connected())
// give the web browser time to receive the data
delay(1);
inString = "";
client.stop();
} //End of if (client)
}
Labels:
Arduino,
Browser,
Freeduino,
Off,
On,
On/Off,
Relay,
Remote,
Sensor,
Sensor Monitoring,
Switch,
Web,
Web Controlled,
Web Monitoring
Subscribe to:
Posts (Atom)









