Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.30 KB | None | 0 0
  1. --Definitionen
  2. id  = 0
  3. sda = 3 --GPIO0
  4. scl = 4 --GPIO2
  5. dev_addr = 0x68 --Addr des Gyros
  6.  
  7. --print("IIC init")
  8.  
  9. --I2C initialisieren
  10. i2c.setup(id, sda, scl, i2c.SLOW)
  11.  
  12. --print("Wake")
  13.  
  14. --MPU aufwecken
  15. i2c.start(id)
  16. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  17. i2c.write(id, 0x6b)
  18. i2c.write(id, 0x00)
  19.  
  20. -- Selbsttest Gyro
  21. --i2c.start(id)
  22. --i2c.address(id, dev_addr, i2c.TRANSMITTER)
  23. --i2c.write(id, 0x1b)
  24. --i2c.write(id, 0b01000000)
  25. --i2c.stop(id)
  26.  
  27. --print("Gyro init")
  28.  
  29. --Gyro einstellen auf 250Grad/sek
  30. i2c.start(id)
  31. i2c.address(id, dev_addr, i2c.TRANSMITTER)
  32. i2c.write(id, 0x1b)
  33. i2c.write(id, 0)
  34. i2c.stop(id)
  35.  
  36. --print("Funktion")
  37.  
  38. --Funktion zum Auslesen eines Registers
  39. function read_reg(reg_addr)
  40. --print("debug")
  41.     i2c.start(id)
  42.     i2c.address(id, dev_addr, i2c.TRANSMITTER)
  43.     i2c.write(id, reg_addr)
  44.     i2c.stop(id)
  45.     i2c.start(id)
  46.     i2c.address(id, dev_addr, i2c.RECEIVER)
  47.     c = i2c.read(id, 1)
  48.     i2c.stop(id)
  49.     return c
  50. end
  51.  
  52. -- Chance auf Reset (MPU abstecken, neustarten, Programm endet hier
  53. if (string.byte(read_reg(0x75)) == 104) then
  54.  
  55.  
  56. ---##################################
  57.  
  58. ---##################################
  59. print("ESP8266 Server")
  60. wifi.setmode(wifi.STATIONAP);
  61. wifi.ap.config({ssid="test",pwd="12345678"});
  62. print("Server IP Address:",wifi.ap.getip())
  63.  
  64. sv = net.createServer(net.TCP)
  65. sv:listen(80, function(conn)
  66.     conn:on("receive", function(conn, rd)
  67.         print(rd)        
  68.     end)
  69.  
  70.     local function send()
  71. print ("jetzt senden")
  72. conn:send(readdata())
  73. end
  74.  
  75.     conn:on("sent", send)
  76.  
  77. conn:send(readdata())
  78.       collectgarbage()
  79.     end)
  80.  
  81. end
  82. ---##################################
  83. readdata = function()
  84.  
  85. Gyro1 = (string.byte(read_reg( 0x45)))*256
  86. Gyro2 = string.byte(read_reg( 0x46))
  87. Gyro = fillvalue(Gyro1 + Gyro2)
  88.  
  89. AccX1 = (string.byte(read_reg( 0x3B)))*256
  90. AccX2 = string.byte(read_reg( 0x3C))
  91. AccX = fillvalue(AccX1 + AccX2)
  92.  
  93. AccZ1 = (string.byte(read_reg( 0x3F)))*256
  94. AccZ2 = string.byte(read_reg( 0x40))
  95. AccZ = fillvalue(AccZ1 + AccZ2)
  96.  
  97. out = Gyro .. ";" .. AccX .. ";" .. AccZ
  98.  
  99. return out
  100. end
  101.  
  102. ---##################################
  103. fillvalue = function(startvalue)
  104. endvalue = startvalue
  105. tostring(endvalue)
  106. while (string.len(endvalue) < 5) do
  107. endvalue = ("0" .. endvalue)
  108. end
  109. return endvalue
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement