====== Bouton wifi ======
Cela permet de fabriquer un objet genre bouton a tourner, potentiomètre qu'on pose sur une table.
Il retransmet en wifi sa position par rapport au nord, une boussole wifi...
===== Matériel =====
- esp8266 avec des GPIO utilisables genre GPIO 4 et 5 olimex ou 0,2 classique
- un module compas HMC6352
- 2 piles AA
Connecter le compas sda et scl sur les gipo 4 5
===== Codes =====
Coté module wifi esp8266
Le module est flashé avec nodemcu
**hmc6352**
--hmc6352
hmc6352 = {
address = 0x21,
hmc_reg = 0x41,
init = function (self, sda, scl)
self.bus = 0
i2c.setup(self.bus, sda, scl, i2c.SLOW)
end,
read = function (self)
i2c.start(self.bus)
i2c.address(self.bus, self.address, i2c.TRANSMITTER)
i2c.write(self.bus, self.hmc_reg)
i2c.stop(self.bus)
i2c.start(self.bus)
i2c.address(self.bus, self.address , i2c.RECEIVER)
c=i2c.read(self.bus, 2)
i2c.stop(self.bus)
return c
end,
bearing = function (self)
local h, l
h, l = string.byte(self:read(), 1, 2)
return ((bit.lshift(h, 8)) +l)/10
end,
}
**init.lua**
J'ai fixé l'IP de destination et le port 192.168.1.33 9009.. a changer dans le code
--init.lua
--lecture d'un compas hmc6352 i2c
require('hmc6352')
-- i2c-scanner trouve
-- Device found at address 0x21
-- Device is wired: SDA to GPIO0 - IO index 3
-- Device is wired: SCL to GPIO2 - IO index 4
-- olimex esp8266 mode-dev
-- Device found at address 0x21
-- Device is wired: SDA to GPIO5 - IO index 1
-- Device is wired: SCL to GPIO4 - IO index 2
-- sda, scl = 1, 2
-- ESP8266 classic
sda, scl = 4, 3
hmc6352:init(sda, scl)
wifi.setmode(wifi.STATION)
cfg = { ip="192.168.1.63", netmask="255.255.255.0", gatway="192.168.1.1"}
wifi.sta.setip(cfg)
wifi.sta.config("SNHACK","0123456789")
ip = wifi.sta.getip()
print(ip)
cu=net.createConnection(net.UDP)
cu:connect(9009,"192.168.1.33")
tmr.alarm(0,50, 1, function() cour=hmc6352:bearing()
cu:send(cour)
--print (cour)
end)
--cu:send("bout1=" .. cour ) end)
print ("En cours")
Coté ordi pour l'écoute
#!/usr/bin/python
import socket
UDP_IP = "192.168.1.33"
UDP_PORT = 9009
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer de 1024 bytes
if not data: break
print data
conn.close() # option