Read GPIO via HTTP

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
rjackr
New user
Posts: 6
Joined: 01 Feb 2016, 11:41

Read GPIO via HTTP

#1 Post by rjackr » 01 Feb 2016, 11:55

There is a way to set GPIO like:
http://<ESP IP address>/control?cmd=PWM,<pin>,<level>

How to read state or PWM <level> of GPIO (to synchronize with application)?

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Read GPIO via HTTP

#2 Post by tozett » 01 Feb 2016, 12:43

you mean reading PWM from the GPIO-PIN into the ESP
(like on an arduino here, with programcode.ino: http://www.benripley.com/diy/arduino/th ... h-arduino/)
and than get this result via a HTTP-url-grab?

maybe it is _not_ (yet) in the ESPEasy firmware?

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Read GPIO via HTTP

#3 Post by tozett » 01 Feb 2016, 12:54

i cannot read source-code well, but partly whats possible to get with http-request is here to see in the plugin..
(a dimmer is mentioned, but....)

https://github.com/ESP8266nu/ESPEasy/bl ... /_C008.ino

rjackr
New user
Posts: 6
Joined: 01 Feb 2016, 11:41

Re: Read GPIO via HTTP

#4 Post by rjackr » 01 Feb 2016, 13:12

Consider this scenario:
1. Client set PWM level by http://<ESP IP address>/control?cmd=PWM,<pin>,<level>
2. After reset client or other client want to initialize self state by request to ESPEasy: "Hi, what is your PWM level of GPIOx?", and ESPEasy should respond with info about level.

I just want to receive <level> form ESPEasy that previous was set by http://<ESP IP address>/control?cmd=PWM,<pin>,<level>

User avatar
costo
Normal user
Posts: 507
Joined: 21 Nov 2015, 15:03
Location: SW-Netherlands, NB

Re: Read GPIO via HTTP

#5 Post by costo » 01 Feb 2016, 13:43

rjackr wrote:There is a way to set GPIO like:
http://<ESP IP address>/control?cmd=PWM,<pin>,<level>

How to read state or PWM <level> of GPIO (to synchronize with application)?
You can't.

PWM is a integer number which is sent to ESPEasy and transformed into a PWM signal on a certain pin.
I assume this number is stored somewhere in a hardware register inside the ESP. There is no support to read the content of this register.
On a Arduino it is not so difficult to access hardware registers of the ATMEL processor. In the ESP libraries there is no support for accessing the ESP8266 hardware directly so you have to keep track of registervalues somewhere in the program or application and resend it to synchronise.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Read GPIO via HTTP

#6 Post by tozett » 01 Feb 2016, 14:05

workaround...?
grab it from syslog?
one has to configure syslog/rsyslog. create a rule to store/send the http-"SET"-request or send it to the other ESP...
not really elegant...but..

or set/get it via MQTT....?

rjackr
New user
Posts: 6
Joined: 01 Feb 2016, 11:41

Re: Read GPIO via HTTP

#7 Post by rjackr » 01 Feb 2016, 14:16

I thought there is any simple way to do it.
It would be a good feature. Complete solution. :idea:

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Read GPIO via HTTP

#8 Post by tozett » 01 Feb 2016, 14:31

there is a "node-feature". i only saw it in the gui, maybe somtime it is used for this?
to have a "swarm"-esp#s ... whoooowwwww
i started a thread with a christmas-wishlist here, maybe a point to add ... 8-)

rjackr
New user
Posts: 6
Joined: 01 Feb 2016, 11:41

Re: Read GPIO via HTTP

#9 Post by rjackr » 01 Feb 2016, 20:57

This is modification I made to support command: http://<ESP IP address>/control?cmd=GETPWM,<pin>
I can't Attach file, and this is diff to _P001_Switch.ino file from BUILD 76
This can be added to GitHub.

Code: Select all

@@ -6,6 +6,9 @@
 #define PLUGIN_ID_001         1
 #define PLUGIN_NAME_001       "Switch input"
 #define PLUGIN_VALUENAME1_001 "Switch"
+#define NO_GPIOS              17
+
+uint16_t pwm_level[17] = {0,};
 
 boolean Plugin_001(byte function, struct EventStruct *event, String& string)
 {
@@ -181,7 +184,7 @@ boolean Plugin_001(byte function, struct EventStruct *event, String& string)
         if (tmpString.equalsIgnoreCase("GPIO"))
         {
           success = true;
-          if (event->Par1 >= 0 && event->Par1 <= 16)
+          if (event->Par1 >= 0 && event->Par1 < NO_GPIOS)
           {
             pinMode(event->Par1, OUTPUT);
             digitalWrite(event->Par1, event->Par2);
@@ -199,10 +202,14 @@ boolean Plugin_001(byte function, struct EventStruct *event, String& string)
         if (tmpString.equalsIgnoreCase("PWM"))
         {
           success = true;
-          if (event->Par1 >= 0 && event->Par1 <= 1023)
+          if (event->Par1 >= 0 && event->Par1 < NO_GPIOS && event->Par2 >= 0 && event->Par2 <= PWMRANGE)
           {
             pinMode(event->Par1, OUTPUT);
             analogWrite(event->Par1, event->Par2);
+
+            if (event->Par1 < NO_GPIOS)
+              pwm_level[event->Par1] = event->Par2;
+
             if (printToWeb)
             {
               printWebString += F("GPIO ");
@@ -212,12 +219,42 @@ boolean Plugin_001(byte function, struct EventStruct *event, String& string)
               printWebString += F("<BR>");
             }
           }
+          else
+          {
+                printWebString += F("Set pin in [0..");
+                printWebString += NO_GPIOS-1;
+                printWebString += F("] and level in [0..");
+                printWebString += PWMRANGE;
+                printWebString += F("]");
+          }
+        }
+
+        if (tmpString.equalsIgnoreCase("GETPWM"))
+        {
+          success = true;
+          if (printToWeb)
+          {
+            if (event->Par1 >= 0 && event->Par1 < NO_GPIOS)
+            {
+                printWebString += F("GPIO ");
+                printWebString += event->Par1;
+                printWebString += F(" PWM level ");
+                printWebString += pwm_level[event->Par1];
+            }
+            else
+            {
+                printWebString += F("Set pin in [0..");
+                printWebString += NO_GPIOS-1;
+                printWebString += F("]");
+            }
+            printWebString += F("<BR>");
+          }
         }
 
         if (tmpString.equalsIgnoreCase("Pulse"))
         {
           success = true;
-          if (event->Par1 >= 0 && event->Par1 <= 1023)
+          if (event->Par1 >= 0 && event->Par1 < NO_GPIOS)
           {
             pinMode(event->Par1, OUTPUT);
             digitalWrite(event->Par1, event->Par2);


marcfon
Normal user
Posts: 11
Joined: 17 Sep 2016, 10:50

Re: Read GPIO via HTTP

#10 Post by marcfon » 02 Oct 2016, 15:03

It's quite an old topic but I found on this forum that there is a way to ask for the PWM level. Just use http://espeasy/control?cmd=status,gpio,

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests