As the title says... Chat on...
User avatar
By rpav
#71593 Hello guys,
I've ESP8266-07 with nodemcu-firmware-2.1.0-master_20170824 on it.
I would like to connect MAX31855 thermocouple and write lua script to read data form it by SPI.

Hardware connection is the same like this:
Image

And my first script is very easy:
-- according to MAX31855 data sheet (https://cdn-shop.adafruit.com/datasheets/MAX31855.pdf )set up SPI
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 32, 16, spi.HALFDUPLEX)

-- do transaction and read 32 bit from MAX
spi.transaction(1,0,0,0,0,0,0,32)

-- print 32 bits
print(spi.get_miso(1,0,32,1))

...and I expect 32 bits of information from MAX. Instead I got:
>print(spi.get_miso(1,0,32,1)
0
>

What I did wrong?

Thank you very much for your help,
-- Roman
User avatar
By trackerj
#71629 A while ago as I had also problems with SPI functions and 32 bit read/write operations I have done it like that:

Code: Select allfunction readALL()
    str1="T.SET :"
    gpio.write(CS, gpio.LOW)      -->Activate the chip
    tmr.delay(1)                  -->1us Delay
    result = 0
   
   for i=32,1,-1
    do
      result = bit.lshift(result, 1)
      result = bit.bor(result ,(bit.band(gpio.read(MISO),0x01)))

      gpio.write(CLK, gpio.HIGH)   
      tmr.delay(2)
      gpio.write(CLK, gpio.LOW)
      tmr.delay(2)
    end

    gpio.write(CS, gpio.HIGH)

    noerr()

    status = bit.band(result,0x10000)

    if(status == 0x10000) then                  -- refer MAX31855 Datasheet     
        err="ERR.100: FAILURE!!"
        print(str1)
        error()
    end
 
    stat_bit = bit.band(result,7)   
 
    if(stat_bit == 1) then                      -- refer MAX31855 Datasheet     
        err="ERR.1:Not connected"
        print(str2)
        error()
        do return end
    end

    if(stat_bit == 2) then                      -- refer MAX31855 Datasheet     
        err="ERR.2: Short GND!!"       
        print(str2)
        error()
        do return end
    end

    if(stat_bit == 4) then                      -- refer MAX31855 Datasheet     
        err="ERR.3: Short VCC!!"       
        print(str2)
        error()
        do return end
    end

    tempr = bit.rshift(result,18)*0.25           --print Temperature
    print(tempr)
    str2=tempr.."C"
    return tempr
end
User avatar
By rpav
#71675 Thank you for your code. I've try it, but the result is still strange. I really have no clue, what is going on here. Any suggestion will be very appreciated.

I've used Amica dev board:
Image

with MAX31855 Module + K Type Thermocouple Thermocouple Sensor
https://www.aliexpress.com/item/200-to-1350c-MAX31855-Module-K-Type-Thermocouple-Thermocouple-temp-Sensor-new/32699007890.html

I very slightly modify your code, just to be able to run it:
Code: Select alllocal MISO = 6            --> GPIO12
local CLK = 5             --> GPIO14
local CS = 8           --> GPIO15
local temp = 0

gpio.mode(CLK,gpio.OUTPUT)
gpio.mode(CS,gpio.OUTPUT)
gpio.mode(MISO,gpio.INPUT,gpio.PULLUP)

gpio.write(CS,gpio.HIGH)
gpio.write(CLK,gpio.LOW)


function readALL()
    str1="T.SET :"
    gpio.write(CS, gpio.LOW)      -->Activate the chip
    tmr.delay(1)                  -->1us Delay
    result = 0
    bs=""
   
   for i=32,1,-1
    do
      result = bit.lshift(result, 1)
      temp=gpio.read(MISO)
      bs=bs..temp
      result = bit.bor(result,(bit.band(temp,0x01)))

      gpio.write(CLK, gpio.HIGH)   
      tmr.delay(2)
      gpio.write(CLK, gpio.LOW)
      tmr.delay(2)
    end
    print(bs)

    gpio.write(CS, gpio.HIGH)

    --noerr()

    status = bit.band(result,0x10000)

    if(status == 0x10000) then                  -- refer MAX31855 Datasheet     
        err="ERR.100: FAILURE!!"
        print(str1)
        --error()
    end
 
    stat_bit = bit.band(result,7)   
 
    if(stat_bit == 1) then                      -- refer MAX31855 Datasheet     
        err="ERR.1:Not connected"
        print(str2)
        --error()
        do return end
    end

    if(stat_bit == 2) then                      -- refer MAX31855 Datasheet     
        err="ERR.2: Short GND!!"       
        print(str2)
        --error()
        do return end
    end

    if(stat_bit == 4) then                      -- refer MAX31855 Datasheet     
        err="ERR.3: Short VCC!!"       
        print(str2)
        --error()
        do return end
    end

    tempr = bit.rshift(result,18)*0.25           --print Temperature
    print(tempr)
    str2=tempr.."C"
    return tempr
end

readALL();


But the result was incorrect:
> dofile("imax.lua")
10101110011111000111111111110000
2791.75
>

Can't figure out what I did wrong... :(
Thanks for any ideas...
-- Roman