Last active
November 16, 2015 21:22
-
-
Save avtehnik/cca80c6c78ef06df93ab 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 <user_config.h> | |
#include <SmingCore/SmingCore.h> | |
#include <Libraries/BMP180/BMP180.h> | |
#define WIFI_SSID "ekreative" // Put you SSID and Password here | |
#define WIFI_PWD "*******" | |
#define DOR_PIN 5 | |
#define NARODM_HOST "narodmon.ru" | |
#define NARODM_PORT 8283 | |
HttpServer server; | |
FTPServer ftp; | |
Timer printTimer; | |
BMP180 barometer; | |
String mac = "00-00-00-00-00-00"; | |
void onIndex(HttpRequest &request, HttpResponse &response) | |
{ | |
TemplateFileStream *tmpl = new TemplateFileStream("index.html"); | |
response.sendTemplate(tmpl); | |
} | |
void onFile(HttpRequest &request, HttpResponse &response) | |
{ | |
String file = request.getPath(); | |
if (file[0] == '/') | |
file = file.substring(1); | |
if (file[0] == '.') | |
response.forbidden(); | |
else | |
{ | |
response.setCache(86400, true); | |
response.sendFile(file); | |
} | |
} | |
void onId(HttpRequest &request, HttpResponse &response) | |
{ | |
JsonObjectStream* stream = new JsonObjectStream(); | |
JsonObject& json = stream->getRoot(); | |
json["status"] = (bool)true; | |
json["id"] = system_get_chip_id(); | |
response.sendJsonObject(stream); | |
} | |
void onOpen(HttpRequest &request, HttpResponse &response) | |
{ | |
digitalWrite(DOR_PIN, 1); | |
delay(250); | |
digitalWrite(DOR_PIN, 0); | |
JsonObjectStream* stream = new JsonObjectStream(); | |
JsonObject& json = stream->getRoot(); | |
json["success"] = (bool)true; | |
response.sendJsonObject(stream); | |
} | |
void onWeather(HttpRequest &request, HttpResponse &response) | |
{ | |
JsonObjectStream* stream = new JsonObjectStream(); | |
JsonObject& json = stream->getRoot(); | |
json["success"] = (bool)true; | |
json["pressure"] = barometer.GetPressure(); | |
json["temperature"] = barometer.GetTemperature(); | |
response.sendJsonObject(stream); | |
} | |
void startWebServer() | |
{ | |
server.listen(80); | |
server.addPath("/", onIndex); | |
server.addPath("/open", onOpen); | |
server.addPath("/id", onId); | |
server.addPath("/weather", onWeather); | |
server.setDefaultHandler(onFile); | |
Serial.println("\r\n=== WEB SERVER STARTED ==="); | |
Serial.println(WifiStation.getIP()); | |
Serial.println("==============================\r\n"); | |
} | |
void startFTP() | |
{ | |
if (!fileExist("index.html")) | |
fileSetContent("index.html", "<h3>Hello form DorBell</h3>"); | |
// Start FTP server | |
ftp.listen(21); | |
ftp.addUser("me", "****"); // FTP account | |
} | |
void nmOnReadyToSend(TcpClient& client, TcpConnectionEvent sourceEvent) | |
{ | |
// debug msg | |
debugf("nmOnReadyToSend"); | |
debugf("sourceEvent: %d", sourceEvent); | |
// в момент соединения осуществляем отправку | |
if(sourceEvent == eTCE_Connected) | |
{ | |
client.sendString("#"+mac+"\n#"+system_get_chip_id()+"pressure#"+barometer.GetPressure()+"\n#"+system_get_chip_id()+"temperature#"+barometer.GetTemperature()+"\n##", true); | |
} | |
} | |
void nmOnCompleted(TcpClient& client, bool successful) | |
{ | |
// debug msg | |
debugf("nmOnCompleted"); | |
debugf("successful: %d", successful); | |
} | |
bool nmOnReceive(TcpClient& client, char *buf, int size) | |
{ | |
// debug msg | |
debugf("nmOnReceive"); | |
debugf("%s", buf); | |
} | |
TcpClient narodMon(nmOnCompleted, nmOnReadyToSend, nmOnReceive); | |
int connectTimeout = 0; | |
int narodmonTomeount = 6; | |
void onWatchDogTimer() { | |
if(!WifiStation.isConnected()){ | |
if(connectTimeout>2){ | |
System.restart(); | |
connectTimeout = 0; | |
} | |
connectTimeout++; | |
} | |
narodmonTomeount++; | |
if(narodmonTomeount>5){ | |
narodMon.connect(NARODM_HOST, NARODM_PORT); | |
narodmonTomeount = 0; | |
} | |
Serial.println(" connect "+String(connectTimeout)+" weather "+String(narodmonTomeount)); | |
} | |
// Will be called when WiFi station was connected to AP | |
void connectOk() | |
{ | |
Serial.println("I'm CONNECTED"); | |
startFTP(); | |
startWebServer(); | |
printTimer.initializeMs(1000*60, onWatchDogTimer).start(); | |
} | |
void init() | |
{ | |
pinMode(DOR_PIN, OUTPUT); | |
digitalWrite(DOR_PIN, 1); | |
delay(250); | |
digitalWrite(DOR_PIN, 0); | |
Wire.begin(); | |
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default | |
Serial.systemDebugOutput(false); // Enable debug output to serial | |
WifiStation.enable(true); | |
WifiStation.config(WIFI_SSID, WIFI_PWD); | |
WifiAccessPoint.enable(false); | |
WifiStation.waitConnection(connectOk); | |
if(barometer.EnsureConnected()){ | |
barometer.Initialize(); | |
}else{ | |
Serial.println("Could not connect to BMP180."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment