-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aruna , need some help. #23
Comments
can you share your code? You must be blocking the loop() |
Ok, I’ll get it to you tomorrow.
Dennis Kelly
[email protected]
… On Oct 9, 2017, at 9:49 PM, Aruna Tennakoon ***@***.***> wrote:
can you share your code? You must be blocking the loop()
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Aruna, just remember I'm no C++ guy so I've pieced this together the best I
could. The sketch labeled blinds contains your original emulator but they
both do the same thing. Once the run time on the stepper or the servo is
increased to about 3 revolutions, it starts having problems.
Thank you so very much for your help.
Dennis Kelly [email protected]
Stepper code-
#include <ESP8266WiFi.h>
#include "Stepper.h"
#include "WemoSwitch.h"
#include "WemoManager.h"
#include "CallbackFunction.h"
int Pin0 = D1;//definition digital 11 pins as pin to control the IN1
(ULN24L01)
int Pin1 = D2;//definition digital 10 pins as pin to control the IN2
(ULN24L01)
int Pin2 = D3;//definition digital 9 pins as pin to control the IN3
(ULN24L01)
int Pin3 = D4;
int _step = 200;
int _speed = 1;
// prototypes
boolean connectWifi();
//on/off callbacks
void lightOn();
void lightOff();
//------- Replace the following! ------
char ssid[] = "BelDen"; // your network SSID (name)
char password[] = "xxx*$#"; // your network key
WemoManager wemoManager;
WemoSwitch *light = NULL;
const int ledPin = BUILTIN_LED;
void setup()
{
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
wemoManager.begin();
// Format: Alexa invocation name, local port no, on callback, off callback
light = new WemoSwitch("test", 80, lightOn, lightOff);
wemoManager.addDevice(*light);
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
pinMode(Pin0, OUTPUT);//Set digital 8 port mode, the OUTPUT for the output
pinMode(Pin1, OUTPUT);//Set digital 9 port mode, the OUTPUT for the output
pinMode(Pin2, OUTPUT);//Set digital 10 port mode, the OUTPUT for the
output
pinMode(Pin3, OUTPUT);//Set digital 11 port mode, the OUTPUT for the
output
delay(10);
digitalWrite(ledPin, HIGH); // Wemos BUILTIN_LED is active Low, so high
is off
}
void Step(int _step)//Stepper motor rotation
{
if(_step>=0){ // Stepper motor forward
for(int i=0;i<_step;i++){
setStep(1, 0, 0, 1);
delay(_speed);
setStep(1, 0, 0, 0);
delay(_speed);
setStep(1, 1, 0, 0);
delay(_speed);
setStep(0, 1, 0, 0);
delay(_speed);
setStep(0, 1, 1, 0);
delay(_speed);
setStep(0, 0, 1, 0);
delay(_speed);
setStep(0, 0, 1, 1);
delay(_speed);
setStep(0, 0, 0, 1);
delay(_speed);
}
}else{ // Stepper motor backward
for(int i=_step;i<0;i++){
setStep(0, 0, 0, 1);
delay(_speed);
setStep(0, 0, 1, 1);
delay(_speed);
setStep(0, 0, 1, 0);
delay(_speed);
setStep(0, 1, 1, 0);
delay(_speed);
setStep(0, 1, 0, 0);
delay(_speed);
setStep(1, 1, 0, 0);
delay(_speed);
setStep(1, 0, 0, 0);
delay(_speed);
setStep(1, 0, 0, 1);
delay(_speed);
}
}
}
void setStep(int a, int b, int c, int d)
{
digitalWrite(Pin0, a);
digitalWrite(Pin1, b);
digitalWrite(Pin2, c);
digitalWrite(Pin3, d);
}
void loop()
{
wemoManager.serverLoop();
}
void lightOn() {
Serial.print("Switch 1 turn on ...");
digitalWrite(ledPin, LOW);
Speed(15);//Stepper motor speed = 15 fast (note:speed from 1 to 15)
Step(512);//Stepper motor forward 512 steps ---- 360 angle
}
void lightOff() {
Serial.print("Switch 1 turn off ...");
digitalWrite(ledPin, HIGH);
Speed(15); //Stepper motor speed = 1 slow (note:speed from 1 to 15)
Step(-512);//Stepper motor backward 512 steps ---- 360 angle
}
void Speed(int stepperspeed)//set Stepper speed
{
_speed = 15 - stepperspeed;
if( _speed<1){
_speed = 1;
}
if( _speed>15){
_speed = 15;
}
}
…___________________________________________________________________________
Servo Code-
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <WiFiUdp.h>
#include <functional>
#include <Servo.h>
Servo myServo;
// Declare function prototypes
bool connectUDP();
void prepareIds();
void respondToSearch();
void startHttpServer();
const char* ssid = "BelDen";
const char* password = "xxx*$#";
// Change these to whatever you'd prefer:
String device_name = "Blinds"; // Name of device
int servoPin = D4; // Pin to toggle
bool debug = false; // If you want debug messages
bool squawk = true; // For on/off messages
// Some UDP / WeMo specific variables:
WiFiUDP UDP;
IPAddress ipMulti(239, 255, 255, 250);
unsigned int portMulti = 1900; // local port to listen on
ESP8266WebServer HTTP(80);
String serial; // Where we save the string of the UUID
String persistent_uuid; // Where we save some socket info with the UUID
// Buffer to save incoming packets:
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
void setup() {
// Begin Serial:
Serial.begin(115200);
// Setup the pin for output:
pinMode(servoPin, OUTPUT);
// digitalWrite(relayPin, LOW); // Start with light off
// Set the UUIDs and socket information:
prepareIds();
// Get settings from WiFi Manager:
WiFiManager wifiManager;
// wifiManager.resetSettings(); // Uncomment this to test WiFi Manager
function
wifiManager.setAPCallback(configModeCallback);
wifiManager.autoConnect();
// Wait til WiFi is connected properly:
int counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
counter++;
}
Serial.println("Connected to WiFi");
// Connect to UDP:
bool udpConnected = connectUDP();
if (udpConnected){
startHttpServer(); // Start the HTTP Server
}
}
void loop() {
HTTP.handleClient();
delay(1);
// If there are packets, we parse them:
int packetSize = UDP.parsePacket();
if(packetSize) {
if (debug) {
Serial.println("");
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = UDP.remoteIP();
for (int i =0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(UDP.remotePort());
}
int len = UDP.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
String request = packetBuffer;
if(request.indexOf('M-SEARCH') > 0) {
if(request.indexOf("urn:Belkin:device:**") > 0) {
if (debug) {
Serial.println("Responding to search request ...");
}
respondToSearch();
}
}
}
delay(10);
}
void prepareIds() {
uint32_t chipId = ESP.getChipId();
char uuid[64];
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff);
serial = String(uuid);
persistent_uuid = "Socket-1_0-" + serial;
}
bool connectUDP(){
boolean state = false;
Serial.println("Connecting to UDP");
if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
Serial.println("Connection successful");
state = true;
}
else{
Serial.println("Connection failed");
}
return state;
}
void startHttpServer() {
HTTP.on("/index.html", HTTP_GET, [](){
if (debug) {
Serial.println("Got Request index.html ...\n");
}
HTTP.send(200, "text/plain", "Hello World!");
});
HTTP.on("/upnp/control/basicevent1", HTTP_POST, []() {
if (debug) {
Serial.println("########## Responding to /upnp/control/basicevent1 ...
##########");
}
//for (int x=0; x <= HTTP.args(); x++) {
// Serial.println(HTTP.arg(x));
//}
String request = HTTP.arg(0);
if (debug) {
Serial.print("request:");
Serial.println(request);
}
if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
if (squawk) {
Serial.println("Open Blinds Request");
}
myServo.attach(servoPin);
myServo.write(180);
delay(3000);
myServo.detach();
}
if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
if (squawk) {
Serial.println("Close Blnds Request");
}
myServo.attach(servoPin);
myServo.write(0);
delay(3000);
myServo.detach();
}
HTTP.send(200, "text/plain", "");
});
HTTP.on("/eventservice.xml", HTTP_GET, [](){
if (debug) {
Serial.println(" ########## Responding to eventservice.xml ...
########\n");
}
String eventservice_xml = "<?scpd xmlns=\"urn:Belkin:service-1-0\"?>"
"<actionList>"
"<action>"
"<name>SetBinaryState</name>"
"<argumentList>"
"<argument>"
"<retval/>"
"<name>BinaryState</name>"
"<relatedStateVariable>BinaryState</relatedStateVariable>"
"<direction>in</direction>"
"</argument>"
"</argumentList>"
"<serviceStateTable>"
"<stateVariable sendEvents=\"yes\">"
"<name>BinaryState</name>"
"<dataType>Boolean</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"<stateVariable sendEvents=\"yes\">"
"<name>level</name>"
"<dataType>string</dataType>"
"<defaultValue>0</defaultValue>"
"</stateVariable>"
"</serviceStateTable>"
"</action>"
"</scpd>\r\n"
"\r\n";
HTTP.send(200, "text/plain", eventservice_xml.c_str());
});
HTTP.on("/setup.xml", HTTP_GET, [](){
if (debug) {
Serial.println(" ########## Responding to setup.xml ...
########\n");
}
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2],
localIP[3]);
String setup_xml = "<?xml version=\"1.0\"?>"
"<root>"
"<device>"
"<deviceType>urn:Belkin:device:controllee:1</deviceType>"
"<friendlyName>"+ device_name +"</friendlyName>"
"<manufacturer>Belkin International Inc.</manufacturer>"
"<modelName>Emulated Socket</modelName>"
"<modelNumber>3.1415</modelNumber>"
"<UDN>uuid:"+ persistent_uuid +"</UDN>"
"<serialNumber>221517K0101769</serialNumber>"
"<binaryState>0</binaryState>"
"<serviceList>"
"<service>"
"<serviceType>urn:Belkin:service:basicevent:1</serviceType>"
"<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
"<controlURL>/upnp/control/basicevent1</controlURL>"
"<eventSubURL>/upnp/event/basicevent1</eventSubURL>"
"<SCPDURL>/eventservice.xml</SCPDURL>"
"</service>"
"</serviceList>"
"</device>"
"</root>\r\n"
"\r\n";
HTTP.send(200, "text/xml", setup_xml.c_str());
if (debug) {
Serial.print("Sending :");
Serial.println(setup_xml);
}
});
HTTP.begin();
if (debug) {
Serial.println("HTTP Server started ..");
}
}
void respondToSearch() {
if (debug) {
Serial.println("");
Serial.print("Sending response to ");
Serial.println(UDP.remoteIP());
Serial.print("Port : ");
Serial.println(UDP.remotePort());
}
IPAddress localIP = WiFi.localIP();
char s[16];
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
String response =
"HTTP/1.1 200 OK\r\n"
"CACHE-CONTROL: max-age=86400\r\n"
"DATE: Tue, 14 Dec 2016 02:30:00 GMT\r\n"
"EXT:\r\n"
"LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
"01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
"SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
"ST: urn:Belkin:device:**\r\n"
"USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
"X-User-Agent: redsonic\r\n\r\n";
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(response.c_str());
UDP.endPacket();
if (debug) {
Serial.println("Response sent !");
}
}
void configModeCallback(WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println("Soft AP's IP Address:");
Serial.println(WiFi.softAPIP());
Serial.println("WiFi Manager: Please connect to AP:");
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.println("To setup WiFi Configuration");
}
On Mon, Oct 9, 2017 at 9:52 PM, Dennis Kelly ***@***.***> wrote:
Ok, I’ll get it to you tomorrow.
Dennis Kelly
***@***.***
On Oct 9, 2017, at 9:49 PM, Aruna Tennakoon ***@***.***>
wrote:
can you share your code? You must be blocking the loop()
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Adq9boKh-Zei51NtkqjEaOYkN5mdF_nNks5sqttTgaJpZM4PzP_d>
.
--
Dennis W. Kelly
[email protected]
|
your problem is you are using delay(). Delay will block the main loop and stops everything (except interrupts) try using a timer to control the stepper. https://playground.arduino.cc/Code/AvoidDelay |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying open and close my vertical blinds with your wemo emulator. Everything works fine until I increase the run time on either a servo or stepper. Alexa will tell me something went wrong and do another run. I've added a 6 second delay after test lights on and off in the single switch example and the same thing happens. Help
The text was updated successfully, but these errors were encountered: