Last active
April 9, 2018 21:04
-
-
Save goran-mahovlic/a7d04696a17dcc13e8a6697335321b77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <ESPmDNS.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
#include <WebServer.h> | |
#include <WiFiClient.h> | |
#include <U8x8lib.h> | |
#include <HX711_ADC.h> | |
#include "soc/rtc.h" | |
//HX711 constructor (dout pin, sck pin) | |
HX711_ADC LoadCell(23, 22); | |
static char celsiusTemp[7]; | |
long t; | |
long Weight = 11.0; | |
// Client variables | |
char linebuf[80]; | |
int charcount=0; | |
// the OLED used | |
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); | |
WiFiServer server ( 80 ); | |
const char* ssid = "OpenWrt"; | |
const char* password = "otvoreno"; | |
void setup() { | |
pinMode(12,INPUT_PULLUP); | |
u8x8.begin(); | |
u8x8.setFont(u8x8_font_chroma48medium8_r); | |
u8x8.drawString(0, 1, "radiona.org"); | |
u8x8.drawString(0, 5, "Booting ..."); | |
// //Serial.print(": "); | |
Serial.begin(115200); | |
Serial.println("Booting"); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
u8x8.drawString(0, 7, "Failed"); | |
Serial.println("Connection Failed! Rebooting..."); | |
delay(5000); | |
ESP.restart(); | |
} | |
// Hostname defaults to esp3232-[MAC] | |
ArduinoOTA.setHostname("Scale OTA ESP32"); | |
// No authentication by default | |
// ArduinoOTA.setPassword("admin"); | |
// Password can be set with it's md5 value as well | |
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 | |
ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); | |
ArduinoOTA.onStart([]() { | |
String type; | |
if (ArduinoOTA.getCommand() == U_FLASH) | |
type = "sketch"; | |
else // U_SPIFFS | |
type = "filesystem"; | |
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() | |
Serial.println("Start updating " + type); | |
}); | |
ArduinoOTA.onEnd([]() { | |
Serial.println("\nEnd"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}); | |
ArduinoOTA.onError([](ota_error_t error) { | |
Serial.printf("Error[%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
}); | |
ArduinoOTA.begin(); | |
Serial.println("Ready"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
u8x8.clearDisplay(); | |
u8x8.drawString(0, 1, "radiona.org"); | |
u8x8.drawString(0, 4, WiFi.localIP().toString().c_str()); | |
// beginServer(); | |
u8x8.clearDisplay(); | |
u8x8.drawString(0, 1, "radiona.org"); | |
LoadCell.begin(); | |
long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time | |
LoadCell.start(stabilisingtime); | |
LoadCell.setCalFactor(210.0); // user set calibration factor (float) | |
Serial.println("Startup + tare is complete"); | |
u8x8.drawString(0, 4, "Ready"); | |
server.begin(); | |
} | |
void loop() { | |
ArduinoOTA.handle(); | |
readScale(); | |
serveClient(); | |
} | |
void serveClient(){ | |
// listen for incoming clients | |
WiFiClient client = server.available(); | |
if (client) { | |
charcount=0; | |
Serial.println("New client"); | |
memset(linebuf,0,sizeof(linebuf)); | |
charcount=0; | |
// an http request ends with a blank line | |
boolean currentLineIsBlank = true; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
Serial.write(c); | |
readScale(); | |
//read char by char HTTP request | |
linebuf[charcount]=c; | |
if (charcount<sizeof(linebuf)-1) charcount++; | |
// if you've gotten to the end of the line (received a newline | |
// character) and the line is blank, the http request has ended, | |
// so you can send a reply | |
if (c == '\n' && currentLineIsBlank) { | |
// send a standard http response header | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println("Connection: close"); // the connection will be closed after completion of the response | |
client.println(); | |
client.println("<!DOCTYPE HTML><html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); | |
client.println("<meta http-equiv=\"refresh\" content=\"30\"></head>"); | |
client.println("<body><div style=\"font-size: 3.5rem;\">radiona.org</p><p>"); | |
client.println("<body><div style=\"font-size: 3.5rem;\"><p>ESP32 LoRa Scale</p><p>"); | |
client.println("<div style=\"color: #930000;\">"); | |
//long scale = readScale2(); | |
client.print("<body><div style=\"font-size: 3.5rem;\"><p>Your weight: "); //</p> | |
client.println(readScale2()); | |
client.println("kg</p><p>"); | |
break; | |
} | |
if (c == '\n') { | |
// you're starting a new line | |
currentLineIsBlank = true; | |
memset(linebuf,0,sizeof(linebuf)); | |
charcount=0; | |
} else if (c != '\r') { | |
// you've gotten a character on the current line | |
currentLineIsBlank = false; | |
} | |
} | |
} | |
// give the web browser time to receive the data | |
delay(1); | |
// close the connection: | |
client.stop(); | |
Serial.println("client disconnected"); | |
} | |
} | |
long readScale2(){ | |
LoadCell.update(); | |
Weight = -(LoadCell.getData()); | |
return Weight/100; | |
} | |
void readScale(){ | |
LoadCell.update(); | |
//get smoothed value from data set + current calibration factor | |
if (millis() > t + 2000) { | |
float i = LoadCell.getData(); | |
// Serial.print("Load_cell output val: "); | |
u8x8.clearDisplay(); | |
// u8x8.drawString(0, 1, "radiona.org"); | |
u8x8.setCursor(0, 0); | |
u8x8.printf("%l.2 kg", -(i/100)); | |
Serial.print(-(i/100)); | |
Weight = -(i/100); | |
Serial.println("kg"); | |
t = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment