- Thu Feb 08, 2018 12:19 pm
#73808
"No
trouble mate" - I've got it - it's working - from Android app, from command line, from crontab, browser, program. YaaaHooo! Thanks to the person(s) who did some earlier posting on this subject. Owe ya a beer. Thanks
Some of you will look at this and "not like my code" - then modify it. My end goal is to be able to take a standard electrical outlet box, a 110-usb module, this relay board, a duplex outlet and supplement/replace my X10 modules in my hydroponic greenhouse.
Now the STC15F104 (SOP8 = package type) chip works by sending 4 bytes to pin 6(?) saying +voltage on pin 7 or ground to pin 7. But that whole chip, 4k flash, 128 bytes ram .... I'm thinking there's got to be more than just "on"/"off". Status? Delay? There's prob 2 people in the world that know.
My testing: on, sleep 3, off, sleep 3 - works as expected. Changing sleep to 1 works, to .1 works to .01 not so much - think the .01 kinda out runs the relay.
Code: Select all/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server communicates with the relay board via the Serial port
* http://server_ip/on will turn the relay on
* http://server_ip/off will turn the relay off
* There are no Serial.print lines of code
*/
#include <ESP8266WiFi.h>
const char* ssid = "MyCasesenSssID";
const char* password = "mYpassw0rd";
IPAddress ip ( 192, 168, 0, 131 ); //131=sw1, 132=sw2, etc
IPAddress gateway ( 192, 168, 0, 1 );
IPAddress subnet ( 255, 255, 255, 0 );
WiFiServer server (80);
//Hex command to send to serial for close relay
byte relON[] = {0xA0, 0x01, 0x01, 0xA2};
//Hex command to send to serial for open relay
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1};
void setup ()
{
delay (10);
Serial.begin (9600);
WiFi.config (ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Start the server
server.begin();
delay(50);
}
void loop() {
int val;
// Check if a client has connected
WiFiClient client = server.available();
if ( ! client ) {
return;
}
// Wait until the client sends some data
while ( ! client.available () )
{
delay (100);
}
// Read the first line of the request
String req = client.readStringUntil ('\r');
client.flush ();
// Match the request
if (req.indexOf ("/on") != -1)
{
Serial.write (relON, sizeof(relON));
val = 1; // if you want feedback see below
} else {
if (req.indexOf ("/off") != -1)
Serial.write (relOFF, sizeof(relOFF));
val = 0; // if you want feedback
}
client.flush ();
// only if you want feedback - see above
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"on":"off";
s += "</html>\n";
// Send the response to the client
client.print (s);
delay (10);
}