Commit 291f5c8d authored by Nelso Jost's avatar Nelso Jost

ADD send to server basic functionality

parent 637df50d
......@@ -36,7 +36,7 @@ String read_DHT22_AH()
Adafruit_BMP085 bmp;
bool is_bmp085_connected=false;
String read_BMP085_PRESSURE()
String read_BMP180_PRESS()
{
if (is_bmp085_connected)
{
......@@ -44,7 +44,19 @@ String read_BMP085_PRESSURE()
}
else
{
return String("<bmp085_not_found>");
return String("<bmp180_not_found>");
}
}
String read_BMP180_TEMP()
{
if (is_bmp085_connected)
{
return String(bmp.readTemperature());
}
else
{
return String("<bmp180_not_found>");
}
}
......
......@@ -11,7 +11,8 @@ String read_DHT22_TEMP();
String read_DHT22_AH();
#include "Adafruit_BMP085.h"
String read_BMP085_PRESSURE();
String read_BMP180_PRESS();
String read_BMP180_TEMP();
#include "RTClib.h"
String read_RTC_DS1307();
......@@ -20,20 +21,22 @@ String set_time_from_csv(String s);
void mysensors_setup();
#define __SENSOR_COUNT 5
#define __SENSOR_COUNT 6
#define __SENSOR_NICKNAMES {"l", "p", "t", "ah", "dt"}
#define __SENSOR_NICKNAMES {"l", "p", "T", " t", "ah", "dt"}
#define __SENSOR_NAMES {"LDR", \
"BMP085_PRESSURE", \
"BMP180_PRESS", \
"BMP180_TEMP", \
"DHT22_TEMP", \
"DHT22_AH", \
"RTC_DS1307"}
#define __FP_READ_SENSOR {&read_LDR, \
&read_BMP085_PRESSURE, \
&read_DHT22_TEMP, \
&read_DHT22_AH, \
#define __FP_READ_SENSOR {&read_LDR, \
&read_BMP180_PRESS, \
&read_BMP180_TEMP, \
&read_DHT22_TEMP, \
&read_DHT22_AH, \
&read_RTC_DS1307}
#endif
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -5,6 +5,7 @@
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "FS.h"
#include "repl.h"
#include "repl_gpio.h"
......@@ -15,8 +16,10 @@
#include "nodemcu_pinout.h"
#include "meteorologger.h"
#define BAUDRATE 115200
#define DATALOG_FILENAME "datalog.txt"
#define DATALOG_FILENAME "DATALOG.TXT"
#define CONFIG_FILENAME "LOGGER.CONF"
#define ARDUINO_TX D1 // (soft RX)
#define ARDUINO_RX D2 // (soft TX)
......@@ -39,14 +42,16 @@ REPL repl(commands);
SoftwareSerial arduinoSerial(ARDUINO_TX, ARDUINO_RX); // RX, TX
Meteorologger meteorologger(&arduinoSerial, &repl, String(DATALOG_FILENAME));
Meteorologger meteorologger(&repl, &arduinoSerial,
String(DATALOG_FILENAME),
String(CONFIG_FILENAME));
void setup()
{
Serial.begin(BAUDRATE);
arduinoSerial.begin(ARDUINO_BAUDRATE);
sdcard_begin(CS_PIN);
meteorologger.begin();
}
......@@ -59,7 +64,7 @@ void loop()
void REPL_ARDUINO(REPL * repl)
{
meteorologger.sendCommand(repl->get_arg(1));
meteorologger.send_arduino_command(repl->get_arg(1));
}
......@@ -71,6 +76,17 @@ void REPL_METEOROLOG(REPL * repl)
void REPL_TAILDATALOG(REPL * repl)
{
sdcard_tail(meteorologger.datalog_filename);
meteorologger.taildatalog();
}
void REPL_SETBOARD(REPL * repl)
{
meteorologger.set_board(repl->get_arg(1).toInt(), repl->get_arg(2));
}
void REPL_SHOWCONFIG(REPL * repl)
{
meteorologger.print_config();
}
This diff is collapsed.
......@@ -13,41 +13,61 @@
#include "repl.h"
#define STATUS_LEVEL_INACTIVE 0
#define STATUS_LEVEL_OK 1
#define STATUS_LEVEL_ERROR_SWSER 2
#define STATUS_LEVEL_ERROR_SD 3
#define STATUS_LEVEL_ERROR_SERVER 4
#define STATUS_INACTIVE 0
#define STATUS_OK 1
#define STATUS_ERROR_SWSER 2
#define STATUS_ERROR_SERVER 3
#define STATUS_ERROR_SD_ONLY 4
#define STATUS_ERROR_SD_SERVER 5 // CRITICAL: DATA LOSS!!
#define APIURL_GETBOARDHASH \
"http://dados.cta.if.ufrgs.br/emm/api/get/boardhash"
#define APIURL_POSTRAWSENSORDATA \
"http://dados.cta.if.ufrgs.br/emm/api/post/rawsensordata"
class Meteorologger
{
public:
Meteorologger(SoftwareSerial * arduinoSerial,
REPL * repl,
String datalog_filename);
Meteorologger(REPL * repl, SoftwareSerial * arduinoSerial,
String datalog_filename,
String config_filename);
String datalog_filename;
float interval=0;
String command="";
String response="";
float arduino_threshold=3;
String config_filename;
int board_id=0;
String board_hash="";
float interval_minutes=0;
String arduino_command="";
String datetime_format="%Y-%m-%d %H:%M:%S";
String arduino_response="";
float arduino_threshold=5;
bool arduino_waiting=false;
int pin_notify=2;
bool notify_high=false;
float notify_interval=5;
float notify_interval=2;
float blink_interval=0.15;
void begin();
void activate(float interval, String command);
void update(void);
void sendCommand(String command);
void send_arduino_command(String command);
String generate_json(void);
bool server_upload(String json);
void set_board(int board_id, String user_password);
void taildatalog(void);
void load_config(void);
bool save_config(bool print_after=false);
void print_config(void);
protected:
int status_level=STATUS_LEVEL_INACTIVE;
int status_level=STATUS_INACTIVE;
bool pin_notify_state;
int blink_count=0;
SoftwareSerial * arduinoSerial;
REPL * repl;
......
......@@ -187,7 +187,7 @@ int REPL::_execute_command(void)
rc++;
}
Serial.print("\n[ERROR] invalid command \"");
Serial.print("\n[ERROR] invalid REPL command \"");
Serial.print(command_name);
Serial.print("\"");
......
......@@ -5,6 +5,8 @@
void REPL_ARDUINO(REPL * repl);
void REPL_METEOROLOG(REPL * repl);
void REPL_TAILDATALOG(REPL * repl);
void REPL_SETBOARD(REPL * repl);
void REPL_SHOWCONFIG(REPL * repl);
#define CMD_REPL_ARDUINO { \
......@@ -16,20 +18,38 @@ void REPL_TAILDATALOG(REPL * repl);
#define CMD_REPL_METEOROLOG { \
"meteorolog", \
REPL_METEOROLOG, \
"meteorolog <interval_mins> <arduino_command>", \
"Activate meterolog timer with interval (minutes)."}
"meteorolog <interval_minutes> <arduino_command>", \
"Activate meterolog timer with given interval (minutes). " \
"Parameters are saved at config file for auto-start on boot."}
#define CMD_REPL_TAILDATALOG { \
"taildatalog", \
REPL_TAILDATALOG, \
"taildatalog", \
"Print the current datalog file."}
"Print last N lines of the the current datalog file."}
#define CMD_REPL_SETBOARD { \
"setboard", \
REPL_SETBOARD, \
"setboard <board_id> <user_password>", \
"Set board authentication token from server to config file."}
#define CMD_REPL_SHOWCONFIG { \
"showconfig", \
REPL_SHOWCONFIG, \
"showconfig", \
"Show the configuration file."}
#define ALLCMD_REPL_METEOROLOGGER \
CMD_REPL_ARDUINO, \
CMD_REPL_METEOROLOG, \
CMD_REPL_TAILDATALOG
CMD_REPL_TAILDATALOG, \
CMD_REPL_SETBOARD, \
CMD_REPL_SHOWCONFIG
#include "wifi.h"
#endif
......@@ -22,6 +22,20 @@ void REPL_WIFISTATUS(REPL * repl)
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
wificonnect(repl->get_arg(1), repl->get_arg(2), 15);
}
void REPL_WGET(REPL * repl)
{
repl->log(wget(repl->get_arg(1)));
}
void REPL_WPOST(REPL * repl)
{
repl->log(wpost(repl->get_arg(1), repl->get_arg(2), repl->get_arg(2)));
}
......@@ -20,16 +20,27 @@
"Connect to a wifi network. If successful (threshold 10 sec), " \
"it will autoconnect if board reseted."}
#define CMD_REPL_WGET {\
"wget", REPL_WGET, "wget <url>", \
"Perform HTTP GET method on the URL."}
#define CMD_REPL_WPOST {\
"wpost", REPL_WPOST, "wpost <url> <content-type> <payload>", \
"Perform HTTP POST method on the URL."}
#define ALLCMD_REPL_WIFI \
CMD_REPL_WIFISCAN, \
CMD_REPL_WIFISTATUS, \
CMD_REPL_WIFICONNECT
CMD_REPL_WIFICONNECT, \
CMD_REPL_WGET, \
CMD_REPL_WPOST
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
void REPL_WGET(REPL * repl);
void REPL_WPOST(REPL * repl);
#endif
......@@ -6,38 +6,35 @@
#include <Arduino.h>
#include "ESP8266WiFi.h"
#include <ESP8266HTTPClient.h>
#define DEBUG_SERIAL Serial
/*
* CREDITS: https://github.com/platformio/platform-espressif8266
* https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
*/
void wifiscan(void)
{
Serial.println();
Serial.print("Scanning wifi networks...");
Serial.flush();
DEBUG_SERIAL.print("\nScanning wifi networks...");
int n = WiFi.scanNetworks();
DEBUG_SERIAL.print(" done!\n");
Serial.println(" done!\n");
Serial.flush();
if (n == 0)
{
Serial.println("no networks found");
DEBUG_SERIAL.println("No networks found!");
}
else
{
Serial.print(n);
Serial.println(" networks found:");
Serial.println("-------------------------");
DEBUG_SERIAL.print(String(n) + " networks found.\n"
"--------------------------------\n");
for (int i = 0; i < n; ++i)
{
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
DEBUG_SERIAL.print(String(i + 1) + ": " + WiFi.SSID(i) + " (" +
WiFi.RSSI(i) + ")");
DEBUG_SERIAL.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ?
" ":"*");
delay(10);
}
}
......@@ -46,8 +43,17 @@ void wifiscan(void)
void wifistatus(void)
{
Serial.println();
DEBUG_SERIAL.println();
WiFi.printDiag(Serial);
if (WiFi.status() == WL_CONNECTED)
{
DEBUG_SERIAL.print("\nConnected, IP address: ");
DEBUG_SERIAL.println(WiFi.localIP());
}
else
{
DEBUG_SERIAL.print("\nUnable to connect! Try reseting the board.");
}
}
......@@ -55,20 +61,68 @@ void wificonnect(String ssid, String password, int threshold)
{
int t=0;
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("\nConnecting");
while (WiFi.status() != WL_CONNECTED and t <= threshold)
DEBUG_SERIAL.print("\nConnecting");
WiFi.reconnect();
while (WiFi.status() != WL_CONNECTED and t <= threshold*1000)
{
delay(500);
Serial.print(".");
DEBUG_SERIAL.print(".");
t += 500;
}
if (WiFi.status() == WL_CONNECTED)
wifistatus();
}
String wget(String url)
{
HTTPClient http;
DEBUG_SERIAL.print("\n[HTTP] begin...\n");
http.begin(url); //HTTP
DEBUG_SERIAL.print("[HTTP] GET...\n");
int httpCode = http.GET();
if (httpCode > 0)
{
Serial.print("\nConnected, IP address: ");
Serial.println(WiFi.localIP());
DEBUG_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK)
{
return http.getString();
}
}
else
{
Serial.print("\nUnable to connect!");
DEBUG_SERIAL.printf("[HTTP] GET... failed, error: %s\n",
http.errorToString(httpCode).c_str());
}
http.end();
}
String wpost(String url, String content_type, String payload)
{
HTTPClient http;
http.begin(url); // HTTPS: http.begin(url, fingerprint)
http.addHeader("content-type", content_type);
DEBUG_SERIAL.print("\n[HTTP] POST... ");
int httpCode = http.POST(payload);
if (httpCode > 0)
{
DEBUG_SERIAL.print("code: " + String(httpCode) + " [");
if (httpCode == HTTP_CODE_OK)
{
DEBUG_SERIAL.print("OK]");
return http.getString();
}
else
{
DEBUG_SERIAL.printf("FAILED:%s]",
http.errorToString(httpCode).c_str());
}
}
http.end();
return String("");
}
......@@ -10,4 +10,7 @@ void wifiscan(void);
void wifistatus(void);
void wificonnect(String ssid, String password, int threshold);
String wget(String url);
String wpost(String url, String content_type, String payload);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment