Merge branch 'pwm' into 'master'

Pwm



See merge request !5
This commit is contained in:
Mathieu 2016-09-28 23:14:43 +02:00
commit 3527184ce0
6 changed files with 115 additions and 40 deletions

View File

@ -11,4 +11,5 @@ int MqttDryPublish(int dry);
int MqttIPPublish(const String &ip);
void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value);
bool MqttIsConfigured();

View File

@ -9,6 +9,8 @@ Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity;
Adafruit_MQTT_Publish *mqtt_dry;
Adafruit_MQTT_Publish *mqtt_ip;
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
#define FEED_MAX_SIZE 96
@ -18,20 +20,19 @@ Adafruit_MQTT_Publish *mqtt_ip;
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity"
#define DRY_FEED_FORMAT "/feeds/%s/%s/dry"
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define PWM_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
// Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO;
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
const int pwmWatched[] = CONFIG_CONTROLLED_PWM;
char *mqttId;
Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
bool isMqttConfigured = false;
bool useMqtts = false;
@ -56,10 +57,21 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId);
mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId);
if( NB_ELEMENTS(gpioWatched) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){
SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d",
NB_ELEMENTS(gpioWatched), NB_ELEMENTS(pwmWatched), MAXSUBSCRIPTIONS);
return -1;
}
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioWatched[i]));
mqttGpio[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]);
}
for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) {
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i]));
mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]);
}
return 0;
}
@ -146,11 +158,11 @@ int MqttDhtPublish(float temp, float humidity) {
return 0;
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
char *temp = strstr(subscription->topic, "/gpio/");
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {
char *temp = strstr(subscription->topic, pattern);
if (!temp)
return -1;
String gpioStr(temp + strlen("/gpio/"));
String gpioStr(temp + strlen(pattern));
int idx = gpioStr.indexOf("/");
int gpio = gpioStr.substring(0, idx).toInt();
@ -160,18 +172,18 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) {
return -1;
}
int getGpioWatchedIndex(int gpio) {
for ( uint i = 0; i < NB_ELEMENTS(gpioWatched); i++) {
if (gpio == gpioWatched[i])
return i;
}
return -1;
}
void MqttChangeGpioValue(int gpio, int value) {
pinMode(gpio, OUTPUT);
digitalWrite(gpio, value);
int watchIdx = getGpioWatchedIndex(gpio);
int watchIdx = findIndex(gpio, gpioWatched);
if (watchIdx >= 0 ) {
mqttGpio[watchIdx]->publish(value);
}
}
void MqttChangePWMValue(int gpio, int value) {
analogWrite(gpio, value);
int watchIdx = findIndex(gpio, gpioWatched);
if (watchIdx >= 0 ) {
mqttGpio[watchIdx]->publish(value);
}
@ -181,13 +193,21 @@ void MqttCheckSubscription() {
if (MqttConnect() == 0) {
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt->readSubscription(0))) {
int gpio = getGpioFromSubscription(subscription);
SKETCH_DEBUG_PRINTF("Got Subscription for gpio %d\n", gpio);
if (gpio > 0 && getGpioWatchedIndex(gpio) >= 0) {
int gpio = getGpioFromSubscription(subscription, "/gpio/");
if (gpio > 0 && findIndex(gpio, gpioWatched) >= 0) {
SKETCH_DEBUG_PRINTF("Got Subscription for GPIO %d\n", gpio);
char *value = (char *) subscription->lastread;
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
MqttChangeGpioValue(gpio, atoi(value));
}
gpio = getGpioFromSubscription(subscription, "/pwm/");
if (gpio > 0 && findIndex(gpio, pwmWatched) >= 0) {
SKETCH_DEBUG_PRINTF("Got Subscription for PWM %d\n", gpio);
char *value = (char *) subscription->lastread;
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
MqttChangePWMValue(gpio, atoi(value));
}
}
}
}

View File

@ -1,17 +1,10 @@
const int gpioWebConf[] = CONFIG_WEB_CONTROLLED_GPIO;
const int pwmWebConf[] = CONFIG_CONTROLLED_PWM;
String gpioControlHTML = "";
String pwmControlHTML = "";
void WebHandleRoot() {
String gpioWeb = "";
if (NB_ELEMENTS(gpioWebConf) > 0){
gpioWeb += "<fieldset>"
"<legend>Relay</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioWebConf) ; i++) {
gpioWeb += "Relay " + String(gpioWebConf[i]) + " " + "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&amp;value=1\">ON</a>/";
gpioWeb += "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&amp;value=0\">OFF</a><br/>";
}
gpioWeb += "</fieldset>";
}
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
@ -29,7 +22,7 @@ void WebHandleRoot() {
#ifdef CONFIG_ENABLE_DRY_SENSOR
"Dryness " + String((dryness*100)/1024) + "%<br/>"
#endif
"</fieldset>" + gpioWeb + "<fieldset>"
"</fieldset>" + gpioControlHTML + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>"
"<a href=\"/setup\">Enter Setup</a><br/>"
"<a href=\"/otamode\">Put device in OTA mode</a><br/>"
@ -129,6 +122,16 @@ void WebHandleGpio() {
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
}
void WebHandlePWM() {
if (!server.hasArg("gpio") || !server.hasArg("value")) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
MqttChangePWMValue(server.arg("gpio").toInt(), server.arg("value").toInt());
server.send(200, "text/html", "<h1>PWM" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
}
boolean WebSetIp(IPAddress &addr, const char *id, const char *error) {
if (server.arg(id) != "" && !addr.fromString(server.arg(id).c_str())) {
WebSendError(error);
@ -244,11 +247,47 @@ void WebHandleWifiStatus() {
server.send(200, "text/html", message);
}
void WebBuildGpioControl(){
if (NB_ELEMENTS(gpioWebConf) > 0){
gpioControlHTML += "<fieldset>"
"<legend>Relay</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioWebConf) ; i++) {
gpioControlHTML += "Relay " + String(gpioWebConf[i]) + " " + "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&amp;value=1\">ON</a>/";
gpioControlHTML += "<a href=\"/gpio?gpio=" + String(gpioWebConf[i]) + "&amp;value=0\">OFF</a><br/>";
}
gpioControlHTML += "</fieldset>";
}
}
void WebBuildPwmControl(){
if (NB_ELEMENTS(pwmWebConf) > 0){
pwmControlHTML += "<fieldset>"
"<legend>PWM</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(pwmWebConf) ; i++) {
pwmControlHTML += "PWM " + String(pwmWebConf[i]) + "<br/>";
pwmControlHTML += "<input type=\"range\" min=\"0\" max=\"1023\""
"style=\"background:#eee\""
"onChange=\"setPWM(this.value," + String(pwmWebConf[i]) + ")\" />";
}
pwmControlHTML += "<script type=\"text/javascript\">"
"function setPWM(newValue, gpio){"
" var xmlHttp = new XMLHttpRequest();"
" xmlHttp.open( \"GET\", \"/pwm?gpio=\"+ gpio + \"&value=\" + newValue, true );" // false for synchronous request
" xmlHttp.send( null );}"
"</script>";
pwmControlHTML += "</fieldset>";
}
}
void WebSetupServer(int ) {
WebBuildGpioControl();
WebBuildPwmControl();
server.on("/", WebHandleRoot);
server.on("/setup", WebHandleSetup);
server.on("/save", WebHandleSave);
server.on("/gpio", WebHandleGpio);
server.on("/pwm", WebHandlePWM);
server.on("/otamode", WebHandleOTA);
server.on("/reboot", WebHandleReboot);
server.on("/wifiStatus", WebHandleWifiStatus);

View File

@ -39,6 +39,10 @@
#define CONFIG_SSID_NAME "ESPConfigurator"
#endif
#ifndef CONFIG_CONTROLLED_PWM
#define CONFIG_CONTROLLED_PWM {}
#endif
#ifndef CONFIG_WEB_CONTROLLED_GPIO
#define CONFIG_WEB_CONTROLLED_GPIO {}
#endif

View File

@ -25,9 +25,12 @@
// Disable mDNS can also save power
#define CONFIG_ENABLE_MDNS
//Web controlled GPIO
// Web controlled GPIO
#define CONFIG_WEB_CONTROLLED_GPIO {2}
// GPIO used in PWM
#define CONFIG_CONTROLLED_PWM {}
/* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */
//If this GPIO is LOW at boot, device will enter setup mode

View File

@ -4,3 +4,11 @@
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
int findIndex(int el, const int array[]){
for ( uint i = 0; i < NB_ELEMENTS(array); i++) {
if (el == array[i])
return i;
}
return -1;
}