From b61dddea019b474941ac68012b31ff46bbf111cd Mon Sep 17 00:00:00 2001 From: Jeena Date: Wed, 14 Feb 2018 19:57:31 +0100 Subject: [PATCH] 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. --- .gitmodules | 3 - README.md | 26 +++---- application.lua | 8 --- credentials.lua.example | 2 - dallas.lua | 6 -- ds18b20.lua | 143 -------------------------------------- esptmp.ino | 148 ++++++++++++++++++++++++++++++++++++++++ flash.sh | 11 --- init.lua | 29 -------- luatool | 1 - server.lua | 27 -------- 11 files changed, 162 insertions(+), 242 deletions(-) delete mode 100644 application.lua delete mode 100644 credentials.lua.example delete mode 100644 dallas.lua delete mode 100644 ds18b20.lua create mode 100644 esptmp.ino delete mode 100755 flash.sh delete mode 100644 init.lua delete mode 160000 luatool delete mode 100644 server.lua diff --git a/.gitmodules b/.gitmodules index 7bc2178..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "luatool"] - path = luatool - url = https://github.com/4refr0nt/luatool.git diff --git a/README.md b/README.md index 688266d..dcceb27 100644 --- a/README.md +++ b/README.md @@ -5,26 +5,28 @@ This is my first stab at a wireless termometer. ## Getting the code git clone https://github.com/jeena/esp8266-temperature.git - cd esp8266-temperature - git submodule init - git submodule update ## Setup -It expects a credentials.lua file with content like this: +Open the esptmp.ino file in the Arduino IDE. - SSID = "myssid" - PASSWORD = "mypasswd" +It expects the credentials for your WiFi in the file like this: -Copy the credentials.lua.example file into credentials.lua and change -it so et matches your wifi credentials. + const char* ssid = "myssid"; + const char* password = "mypass"; + +Change the credentials in esptmp.ino so it matches your wifi credentials. ## Flashing -To put the code onto the ESP8266 you need the luatool which is -included as a git submodule. +1. Under File -> Preferences set as Additional Boards Manager URLs: + 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 @@ -36,4 +38,4 @@ esp8266-temperature is free software: you can redistribute it and/or modify it u esp8266-temperature is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along with esp8266-temperature. If not, see http://www.gnu.org/licenses/. \ No newline at end of file +You should have received a copy of the GNU General Public License along with esp8266-temperature. If not, see http://www.gnu.org/licenses/. diff --git a/application.lua b/application.lua deleted file mode 100644 index bdd4381..0000000 --- a/application.lua +++ /dev/null @@ -1,8 +0,0 @@ -pin = 4 -ow.setup(pin) - -counter=0 -lasttemp=-999 - -dofile("dallas.lua") -dofile("server.lua") \ No newline at end of file diff --git a/credentials.lua.example b/credentials.lua.example deleted file mode 100644 index 1bc1711..0000000 --- a/credentials.lua.example +++ /dev/null @@ -1,2 +0,0 @@ -SSID = "MyWifi" -PASSWORD = "mypasswd" \ No newline at end of file diff --git a/dallas.lua b/dallas.lua deleted file mode 100644 index b5a0ef4..0000000 --- a/dallas.lua +++ /dev/null @@ -1,6 +0,0 @@ -function getTemp() - t = require("ds18b20") - t.setup(9) - addrs = t.addrs() - lasttemp = t.read(nil,t.C) -end diff --git a/ds18b20.lua b/ds18b20.lua deleted file mode 100644 index 55fe5f6..0000000 --- a/ds18b20.lua +++ /dev/null @@ -1,143 +0,0 @@ --------------------------------------------------------------------------------- --- DS18B20 one wire module for NODEMCU --- NODEMCU TEAM --- LICENCE: http://opensource.org/licenses/MIT --- Vowstar --- 2015/02/14 sza2 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 diff --git a/esptmp.ino b/esptmp.ino new file mode 100644 index 0000000..8ed9b0b --- /dev/null +++ b/esptmp.ino @@ -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 +#include +#include +#include +#include + +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\r\nHello from ESP8266 at "; + s += ipStr; + s += " \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"); +} + diff --git a/flash.sh b/flash.sh deleted file mode 100755 index f716ca8..0000000 --- a/flash.sh +++ /dev/null @@ -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 - diff --git a/init.lua b/init.lua deleted file mode 100644 index 0558cc3..0000000 --- a/init.lua +++ /dev/null @@ -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) \ No newline at end of file diff --git a/luatool b/luatool deleted file mode 160000 index 28ff7a1..0000000 --- a/luatool +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 28ff7a18c802adf9beac211226e1673ec4b70a8d diff --git a/server.lua b/server.lua deleted file mode 100644 index 6e72706..0000000 --- a/server.lua +++ /dev/null @@ -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="
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("

Hello, this is Jeenas ESP8266.

How are you today.
Count=" .. mycounter .. m .. "
Heap=" .. node.heap() .. "
Temp=" .. temp) - end) - conn:on("sent", function(conn) conn:close() end) -end)