Left here for archival purposes.

User avatar
By hwiguna
#4566
alonewolfx2 wrote:Can you share your example code and which shift register?


Shift register is SN74HC595. I believe the HC is important, the datasheet says it can operate as low as 2V.

I don't have access to my home PC right now, but here's a snippet that I have access to.
The shiftOut without webserver is what you saw in the video. I have NOT successfully run shift register code WITH the webserver. I ran into odd errors and reboots. Maybe it is out of memory?!

EDIT: lua thinks my bit pattern string is a number so it puked when I did a string.sub() on it. tostring() around the bits fixed it.

Code: Select all-- I/O configuration --
dataPin=0
latchPin=1
clockPin=2
gpio.mode(dataPin,gpio.OUTPUT)
gpio.mode(latchPin,gpio.OUTPUT)
gpio.mode(clockPin,gpio.OUTPUT)

-- Initialize all to LOW --
gpio.write(dataPin,gpio.LOW)
gpio.write(latchPin,gpio.LOW)
gpio.write(clockPin,gpio.LOW)

function shiftOut(dataPin, clockPin, latchPin, bits)
 gpio.write(latchPin,gpio.LOW) -- Don't reflect changes to LED (yet...)
 for b=1,8 do
  gpio.write(clockPin,gpio.LOW)

  if (string.sub(bits, b,b) == "0") then
   gpio.write(dataPin,gpio.HIGH)
  else
   gpio.write(dataPin,gpio.LOW)
  end

  gpio.write(clockPin,gpio.HIGH)
 end
 
 gpio.write(latchPin,gpio.HIGH) -- Transfer to LED
end

digits={"11111100", "01100000", "11011010", "11110010", "01100110", "10110110", "10111110", "11100000", "11111110", "11110110"}
frame=1
print(digits[frame])

bits = digits[8]
shiftOut(dataPin, clockPin, latchPin, bits)



-- LUA Webserver --
srv=net.createServer(net.TCP)

srv:listen(80,function(conn)

  conn:on("receive",function(conn,payload)

    forArduino = string.sub(payload, 6, 9) -- LUA substring is inclusive on both ends
    --print(forArduino .. "\r\n")

    reply = "Sent to Arduino: " .. forArduino
    payloadLen = string.len(reply)
    conn:send("HTTP/1.1 200 OK\r\n")
    conn:send("Content-Length:" .. tostring(payloadLen) .. "\r\n")
    conn:send("Connection:close\r\n\r\n")
    conn:send(reply)

    index = string.sub(payload,6,6)
   digits={"11111100", "01100000", "11011010", "11110010", "01100110", "10110110", "10111110", "11100000", "11111110", "11110110"}

    bitz = digits[index]
    shiftOut(dataPin, clockPin, latchPin, bitz)
  end)

  conn:on("sent",function(conn)
    conn:close()
  end)
end)
Last edited by hwiguna on Thu Dec 11, 2014 3:55 pm, edited 1 time in total.
User avatar
By hwiguna
#4601
hwiguna wrote:
zeroday wrote:
hwiguna wrote:zeroday,
Thank you for fixing the GPIO flicker in the latest NodeMCU release (Version 0.9.2 20141209).
I was able to drive a shift register using three of the ESP-03 GPIO pins. So basically we can now have as many digital pins as we wish! Wow... :D


really? :o , with wifi on?
changed to a more powerful 3.3V power supply?


I used same wall adaptor. But you're right, I did not have WiFi on. So, things may fail when I turn on WiFi. I will try iwith WiFi tomorrow night.

]


Alas, when I run a webserver on the ESP. The I/O flicker returns, and shift register stopped working :-(
User avatar
By hwiguna
#4629 zeroday,
GREAT NEWS!
NodeMCU can listen to webrequests, parses it, and drive a shift register based on the request!
My friend Tony lent me his scope and although the signal appears to be noisier, it still works. I think the reason it did not work before had to do with lua's type guessing, it was interpreting my bitmap pattern as number when I wanted it to be a string.

The video below shows before and after I turn on WiFi feature on the ESP, but not the webrequest.
I'm going to redo that vomit inducing video. Click link on left at own risk if you want to see the ESP03 sending the right bit pattern to the shift register based on webrequests.



Thank you again for NodeMCU. I've been having a lot of fun.

Here is the code if anyone wants to duplicate the experiment. The shift register is the popular SN74HC595. Resistors are 1K. Level shift ladder is 1.8K from ESP RX to GND, and 1K from ESP RX to FTDI TX.
Code: Select all-- I/O configuration --
dataPin=0
latchPin=1
clockPin=2
gpio.mode(dataPin,gpio.OUTPUT)
gpio.mode(latchPin,gpio.OUTPUT)
gpio.mode(clockPin,gpio.OUTPUT)

-- Initialize all to LOW --
gpio.write(dataPin,gpio.LOW)
gpio.write(latchPin,gpio.LOW)
gpio.write(clockPin,gpio.LOW)

function shiftOut(dataPin, clockPin, latchPin, bitz)
 gpio.write(latchPin,gpio.LOW) -- Don't reflect changes to LED (yet...)
 for b=1,8 do
  gpio.write(clockPin,gpio.LOW)

  if (string.sub(bitz, b,b) == "0") then
   gpio.write(dataPin,gpio.HIGH)
  else
   gpio.write(dataPin,gpio.LOW)
  end

  gpio.write(clockPin,gpio.HIGH)
 end
 
 gpio.write(latchPin,gpio.HIGH) -- Transfer to LED
end

digits={"11111100", "01100000", "11011010", "11110010", "01100110", "10110110", "10111110", "11100000", "11111110", "11110110"}
frame=1
print(digits[frame])

bits = digits[9]
shiftOut(dataPin, clockPin, latchPin, bits)

print( node.heap() )
print(wifi.sta.getip())

-----------------------------------

-- LUA Webserver --
srv=net.createServer(net.TCP)

srv:listen(80,function(conn)

  conn:on("receive",function(conn,payload)

    --forArduino = string.sub(payload, 6, 9) -- LUA substring is inclusive on both ends
    --print(forArduino .. "\r\n")
    bitz="?"
    if string.len(payload)>=6 then
     if string.sub(payload,6,8) == "dig" then
      index = tonumber(string.sub(payload,9,9))
      bitz = tostring(digits[index+1])
      shiftOut(dataPin, clockPin, latchPin, bitz)
     end
    end

    reply = "Sent to Shift Register: " .. bitz
    payloadLen = string.len(reply)
    conn:send("HTTP/1.1 200 OK\r\n")
    conn:send("Content-Length:" .. tostring(payloadLen) .. "\r\n")
    conn:send("Connection:close\r\n\r\n")
    conn:send(reply)

  end)

  conn:on("sent",function(conn)
    conn:close()
  end)
end)