Rewrite whole project as a Arduino IDE project

The lua flashing never really worked and I had so many problems
flashing the hardware. The Arduino IDE does it much better so this
is now written in thes Arduino language which looks like C++.

Still need to explain the hardware setup.
This commit is contained in:
Jeena 2018-02-14 19:57:31 +01:00
parent 4dc9e79f28
commit b61dddea01
11 changed files with 162 additions and 242 deletions

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "luatool"]
path = luatool
url = https://github.com/4refr0nt/luatool.git

View file

@ -5,26 +5,28 @@ This is my first stab at a wireless termometer.
## Getting the code ## Getting the code
git clone https://github.com/jeena/esp8266-temperature.git git clone https://github.com/jeena/esp8266-temperature.git
cd esp8266-temperature
git submodule init
git submodule update
## Setup ## Setup
It expects a credentials.lua file with content like this: Open the esptmp.ino file in the Arduino IDE.
SSID = "myssid" It expects the credentials for your WiFi in the file like this:
PASSWORD = "mypasswd"
Copy the credentials.lua.example file into credentials.lua and change const char* ssid = "myssid";
it so et matches your wifi credentials. const char* password = "mypass";
Change the credentials in esptmp.ino so it matches your wifi credentials.
## Flashing ## Flashing
To put the code onto the ESP8266 you need the luatool which is 1. Under File -> Preferences set as Additional Boards Manager URLs:
included as a git submodule. http://arduino.esp8266.com/versions/2.3.0/package_esp8266com_index.json
2. Chose under Tools -> Board the "Node MCU 1.0 (ESP12-E Module)"
3. Press the Upload button
Use flash.sh to flash it via /dev/ttyUSB0 ## Hardware Setup
t.b.d.
## License ## License

View file

@ -1,8 +0,0 @@
pin = 4
ow.setup(pin)
counter=0
lasttemp=-999
dofile("dallas.lua")
dofile("server.lua")

View file

@ -1,2 +0,0 @@
SSID = "MyWifi"
PASSWORD = "mypasswd"

View file

@ -1,6 +0,0 @@
function getTemp()
t = require("ds18b20")
t.setup(9)
addrs = t.addrs()
lasttemp = t.read(nil,t.C)
end

View file

@ -1,143 +0,0 @@
--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <vowstar@nodemcu.com>
-- 2015/02/14 sza2 <sza2trash@gmail.com> Fix for negative values
--------------------------------------------------------------------------------
-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
-- Local used variables
-- DS18B20 dq pin
local pin = nil
-- DS18B20 default pin
local defaultPin = 9
-- Local used modules
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
-- Implementation
C = 'C'
F = 'F'
K = 'K'
function setup(dq)
pin = dq
if(pin == nil) then
pin = defaultPin
end
ow.setup(pin)
end
function addrs()
setup(pin)
tbl = {}
ow.reset_search(pin)
repeat
addr = ow.search(pin)
if(addr ~= nil) then
table.insert(tbl, addr)
end
tmr.wdclr()
until (addr == nil)
ow.reset_search(pin)
return tbl
end
function readNumber(addr, unit)
result = nil
setup(pin)
flag = false
if(addr == nil) then
ow.reset_search(pin)
count = 0
repeat
count = count + 1
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
ow.reset_search(pin)
end
if(addr == nil) then
return result
end
crc = ow.crc8(string.sub(addr,1,7))
if (crc == addr:byte(8)) then
if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
-- print("Device is a DS18S20 family device.")
ow.reset(pin)
ow.select(pin, addr)
ow.write(pin, 0x44, 1)
-- tmr.delay(1000000)
present = ow.reset(pin)
ow.select(pin, addr)
ow.write(pin,0xBE,1)
-- print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
-- print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
-- print("CRC="..crc)
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256)
if (t > 32767) then
t = t - 65536
end
if (addr:byte(1) == 0x28) then
t = t * 625 -- DS18B20, 4 fractional bits
else
t = t * 5000 -- DS18S20, 1 fractional bit
end
if(unit == nil or unit == 'C') then
-- do nothing
elseif(unit == 'F') then
t = t * 1.8 + 320000
elseif(unit == 'K') then
t = t + 2731500
else
return nil
end
t = t / 10000
return t
end
tmr.wdclr()
else
-- print("Device family is not recognized.")
end
else
-- print("CRC is not valid!")
end
return result
end
function read(addr, unit)
t = readNumber(addr, unit)
if (t == nil) then
return nil
else
return t
end
end
-- Return module table
return M

148
esptmp.ino Normal file
View file

@ -0,0 +1,148 @@
/*
ESP8266 mDNS responder sample
This is an example of an HTTP server that is accessible
via http://esp8266.local URL thanks to mDNS responder.
Instructions:
- Update WiFi SSID and password as necessary.
- Flash the sketch to the ESP8266 board
- Install host software:
- For Linux, install Avahi (http://avahi.org/).
- For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
- For Mac OSX and iOS support is built in through Bonjour already.
- Point your browser to http://esp8266.local, you should see a response.
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const char* ssid = "Jeenas";
const char* password = "mypass";
// Data wire is conntec to the Arduino digital pin 5
#define ONE_WIRE_BUS 5
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
String getTemp()
{
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
return String(sensors.getTempCByIndex(0));
}
void setup(void)
{
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("esp8266")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
// Start up the library
sensors.begin();
}
void loop(void)
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while (client.connected() && !client.available()) {
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if (req == "/")
{
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 at ";
s += ipStr;
s += " </html>\r\n\r\n";
Serial.println("Sending 200");
}
else if (req == "/tmp")
{
s = getTemp();
s = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
s += getTemp();
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
client.print(s);
Serial.println("Done with client");
}

View file

@ -1,11 +0,0 @@
#!/bin/sh
luafiles=`find . -name "*.lua" -not -path "./luatool/*" -exec basename \{} \;`
for f in $luafiles
do
sudo python2 ./luatool/luatool/luatool.py --port /dev/ttyUSB0 --src $f --dest $f -b 74880 --delay 0.05
done

View file

@ -1,29 +0,0 @@
-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")
function startup()
if file.open("init.lua") == nil then
print("init.lua deleted or renamed")
else
print("Running")
file.close("init.lua")
-- the actual application is stored in 'application.lua'
dofile("application.lua")
end
end
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
tmr.stop(1)
print("WiFi connection established, IP address: " .. wifi.sta.getip())
print("You have 3 seconds to abort")
print("Waiting...")
tmr.alarm(0, 3000, 0, startup)
end
end)

@ -1 +0,0 @@
Subproject commit 28ff7a18c802adf9beac211226e1673ec4b70a8d

View file

@ -1,27 +0,0 @@
temp = -999
t = require("ds18b20")
t.setup(4)
addrs = t.addrs()
mycounter=0
srv=net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(conn, payload)
if string.find(payload,"?myarg=") then
mycounter=mycounter+1
m="<br>Value= " .. string.sub(payload, string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2)
else
m=""
end
_temp = t.read(nil,t.C)
if _temp ~= nil then
temp = _temp
end
conn:send("<h1> Hello, this is Jeenas ESP8266.</h1><p>How are you today.<br> Count=" .. mycounter .. m .. "<br>Heap=" .. node.heap() .. "<br>Temp=" .. temp)
end)
conn:on("sent", function(conn) conn:close() end)
end)