Question about how to use (with-client) on huzzah esp32 board


#1

Hi

I’m trying to follow the examples for wifi but get stuck on how to call (with-client)

This is what I do :

(wifi-connect "myssid" "mypaswd")

after a while connected 192.168.0.126

(defun println (x s) (princ x s) (princ #\return s) (princ #\newline s))

(wifi-server)
nil

(with-client (s)
  (loop
   (let ((line (read-line s)))
     (when (null line) (return))
     (print line)))
  (println "HTTP/1.1 200 OK" s)
  (println "Content-Type: text/html" s)
  (println "Connection: close" s)
  (println "" s)
  (princ "<!DOCTYPE HTML><html><body><h1>ADC Value: " s)
  (princ (analogread 0) s)
  (println "</h1></body></html>" s)
  (println "" s))

How do I call (with-client) ?
I tried (with-client (“192.168.0.126”))

When I open a browser on this address nothing happens …

Kind regards

Ronny Suy


#2

That example wasn’t very well written. Here’s a clearer version:

Start as you did before:

(wifi-connect "myssid" "mypaswd")

(defun println (x s) (princ x s) (princ #\return s) (princ #\newline s))

Then start a server:

> (wifi-server)
nil

Now define the following function webpage:

(defun webpage ()
  (loop
   (with-client (s)
     (loop
      (let ((line (read-line s)))
        (when (null line) (return))
        (print line)))
    (println "HTTP/1.1 200 OK" s)
    (println "Content-Type: text/html" s)
    (println "Connection: close" s)
    (println "" s)
    (princ "<!DOCTYPE HTML><html><body><h1>ADC Value: " s)
    (princ (analogread 0) s)
    (println "</h1></body></html>" s)
    (println "" s))
   (delay 5000)))

Run webpage by typing:

(webpage)

It will wait in a loop waiting for a client to connect.

Now go to a web browser and enter the address wifi-connect returned earlier.

You should see a page showing the value on ADC 0.

You can exit from webpage by typing ~.

I’ll update the main documentation too; thanks for pointing out that this needed to be improved!


#3

Hi David,

Thanks for your reply.
It works nice now :-)
Thank you for updating the documentation.
I switched to this board because it gives my project great boost so my weather station can work
online on a website. I only have to see for a nice battery management so it can stand alone outside with no cables attached !

Kind regards,

Ronny Suy


#4

Oh David I almost forgot to tell :

Its not possible to read some analog pins once wifi has started on the ESP32

See user manual:

Bottom row:
A0 - this is an analog input A0 and also an analog output DAC2. It can also be used as a GPIO #26. It uses ADC
#2
A1 - this is an analog input A1 and also an analog output DAC1. It can also be used as a GPIO #25. It uses ADC #2
A2 - this is an analog input A2 and also GPI #34. Note it is not an output-capable pin! It uses ADC #1
© Adafruit Industries


Page 9 of 27A3 - this is an analog input A3 and also GPI #39. Note it is not an output-capable pin! It uses ADC #1
A4 - this is an analog input A4 and also GPI #36. Note it is not an output-capable pin! It uses ADC #1
A5 - this is an analog input A5 and also GPIO #4. It uses ADC #2
21 - General purpose IO pin #21
Top row:
13 - This is GPIO #13 and also an analog input A12 on ADC #1. It’s also connected to the red LED next to the USB
port
12 - This is GPIO #12 and also an analog input A11 on ADC #2. This pin has a pull-down resistor built into it, we
recommend using it as an output only, or making sure that the pull-down is not affected during boot.
27 - This is GPIO #27 and also an analog input A10 on ADC #2
33 - This is GPIO #33 and also an analog input A9 on ADC #1. It can also be used to connect a 32 KHz crystal.
15 - This is GPIO #15 and also an analog input A8 on ADC #2
32 - This is GPIO #32 and also an analog input A7 on ADC #1. It can also be used to connect a 32 KHz crystal.
14 - This is GPIO #14 and also an analog input A6 on ADC #2
There’s also an external analog input
A13 - This is general purpose input #35 and also an analog input A13, which is a resistor divider connected to the
VBAT line
Note you can only read analog inputs on ADC #1 once WiFi has started

Ronny


#5

Does anything need to be changed in uLisp because of this?


#6

No certainly not !

I changed the (analogread 0) to (digitalread 27) in your example you’ve sent me earlier today
This reads input 27 as marked on the pcb.

And also changed the text for the page and added an autorefresh of 2 seconds :

(defun webpage ()
  (pinmode 27 0)
  (loop
   (with-client (s)
     (loop
      (let ((line (read-line s)))
        (when (null line) (return))
        (print line)))
    (println "HTTP/1.1 200 OK" s)
    (println "Content-Type: text/html" s)
    (println "Connection: close" s)
    (println "Refresh: 2" s)
    (println "" s)
    (princ "<!DOCTYPE HTML><html><body><h1>Pin27 Value: " s)
    (princ (digitalread 27) s)
    (println "</h1></body></html>" s)
    (println "" s))
   (delay 5000)))

Hope you like this, may be this is slightly more appropriate to use as an example for esp32 ?

Ronny