Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <SimplePgSQL.h>
- #include "time.h"
- #include <ESP32Time.h>
- ESP32Time rtc(0); // offset in seconds, use 0 because NTP already offset
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 2
- #define LED_BUILTIN 8
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- RTC_DATA_ATTR int readingCnt = 0;
- typedef struct {
- float temp;
- unsigned long time;
- } sensorReadings;
- #define maximumReadings 6 // The maximum number of readings that can be stored in the available space
- #define sleeptimeSecs 5 // Every 10-mins of sleep 10 x 60-secs
- RTC_DATA_ATTR sensorReadings Readings[maximumReadings];
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = -18000; //Replace with your GMT offset (secs)
- const int daylightOffset_sec = 3600; //Replace with your daylight offset (secs)
- int hours, mins, secs;
- float tempC;
- bool sent = false;
- IPAddress PGIP(xxx,xxx,xxx,xxx); // your PostgreSQL server IP
- const char ssid[] = "xxxxxxx"; // your network SSID (name)
- const char pass[] = "xxxxxxxxx"; // your network password
- const char user[] = "xxxx"; // your database user
- const char password[] = "xxxx"; // your database password
- const char dbname[] = "xxxxxxxx"; // your database name
- int WiFiStatus;
- WiFiClient client;
- char buffer[1024];
- PGconnection conn(&client, 0, 1024, buffer);
- char tosend[128];
- String tosendstr;
- #ifndef USE_ARDUINO_ETHERNET
- void checkConnection()
- {
- int status = WiFi.status();
- if (status != WL_CONNECTED) {
- if (WiFiStatus == WL_CONNECTED) {
- Serial.println("Connection lost");
- WiFiStatus = status;
- }
- }
- else {
- if (WiFiStatus != WL_CONNECTED) {
- Serial.println("Connected");
- WiFiStatus = status;
- }
- }
- }
- #endif
- static PROGMEM const char query_rel[] = "\
- SELECT a.attname \"Column\",\
- pg_catalog.format_type(a.atttypid, a.atttypmod) \"Type\",\
- case when a.attnotnull then 'not null ' else 'null' end as \"null\",\
- (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)\
- FROM pg_catalog.pg_attrdef d\
- WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) \"Extras\"\
- FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c\
- WHERE a.attrelid = c.oid AND c.relkind = 'r' AND\
- c.relname = %s AND\
- pg_catalog.pg_table_is_visible(c.oid)\
- AND a.attnum > 0 AND NOT a.attisdropped\
- ORDER BY a.attnum";
- static PROGMEM const char query_tables[] = "\
- SELECT n.nspname as \"Schema\",\
- c.relname as \"Name\",\
- CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as \"Type\",\
- pg_catalog.pg_get_userbyid(c.relowner) as \"Owner\"\
- FROM pg_catalog.pg_class c\
- LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\
- WHERE c.relkind IN ('r','v','m','S','f','')\
- AND n.nspname <> 'pg_catalog'\
- AND n.nspname <> 'information_schema'\
- AND n.nspname !~ '^pg_toast'\
- AND pg_catalog.pg_table_is_visible(c.oid)\
- ORDER BY 1,2";
- int pg_status = 0;
- void doPg(void)
- {
- char *msg;
- int rc;
- if (!pg_status) {
- conn.setDbLogin(PGIP,
- user,
- password,
- dbname,
- "utf8");
- pg_status = 1;
- return;
- }
- if (pg_status == 1) {
- rc = conn.status();
- if (rc == CONNECTION_BAD || rc == CONNECTION_NEEDED) {
- char *c=conn.getMessage();
- if (c) Serial.println(c);
- pg_status = -1;
- }
- else if (rc == CONNECTION_OK) {
- pg_status = 2;
- Serial.println("Enter query");
- }
- return;
- }
- if (pg_status == 2) {
- if (!Serial.available()) return;
- char inbuf[128];
- int n = Serial.readBytesUntil('\n',inbuf,127);
- while (n > 0) {
- if (isspace(inbuf[n-1])) n--;
- else break;
- }
- inbuf[n] = 0;
- if (!strcmp(inbuf,"\\d")) {
- if (conn.execute(query_tables, true)) goto error;
- Serial.println("Working...");
- pg_status = 3;
- return;
- }
- if (!strncmp(inbuf,"\\d",2) && isspace(inbuf[2])) {
- char *c=inbuf+3;
- while (*c && isspace(*c)) c++;
- if (!*c) {
- if (conn.execute(query_tables, true)) goto error;
- Serial.println("Working...");
- pg_status = 3;
- return;
- }
- if (conn.executeFormat(true, query_rel, c)) goto error;
- Serial.println("Working...");
- pg_status = 3;
- return;
- }
- if (!strncmp(inbuf,"exit",4)) {
- conn.close();
- Serial.println("Thank you");
- pg_status = -1;
- return;
- }
- if (conn.execute(inbuf)) goto error;
- Serial.println("Working...");
- pg_status = 3;
- }
- if (pg_status == 3) {
- rc=conn.getData();
- int i;
- if (rc < 0) goto error;
- if (!rc) return;
- if (rc & PG_RSTAT_HAVE_COLUMNS) {
- for (i=0; i < conn.nfields(); i++) {
- if (i) Serial.print(" | ");
- Serial.print(conn.getColumn(i));
- }
- Serial.println("\n==========");
- }
- else if (rc & PG_RSTAT_HAVE_ROW) {
- for (i=0; i < conn.nfields(); i++) {
- if (i) Serial.print(" | ");
- msg = conn.getValue(i);
- if (!msg) msg=(char *)"NULL";
- Serial.print(msg);
- }
- Serial.println();
- }
- else if (rc & PG_RSTAT_HAVE_SUMMARY) {
- Serial.print("Rows affected: ");
- Serial.println(conn.ntuples());
- }
- else if (rc & PG_RSTAT_HAVE_MESSAGE) {
- msg = conn.getMessage();
- if (msg) Serial.println(msg);
- }
- if (rc & PG_RSTAT_READY) {
- pg_status = 2;
- Serial.println("Enter query");
- }
- }
- return;
- error:
- msg = conn.getMessage();
- if (msg) Serial.println(msg);
- else Serial.println("UNKNOWN ERROR");
- if (conn.status() == CONNECTION_BAD) {
- Serial.println("Connection is bad");
- pg_status = -1;
- }
- }
- int i;
- void setup(void)
- {
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- if ((readingCnt == 0)) {
- WiFi.begin((char *)ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(250);
- }
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- struct tm timeinfo;
- getLocalTime(&timeinfo);
- rtc.setTimeStruct(timeinfo);
- delay(250);
- }
- sensors.begin();
- digitalWrite(LED_BUILTIN, HIGH);
- sensors.requestTemperatures();
- tempC = sensors.getTempCByIndex(0);
- Readings[readingCnt].temp = tempC; // Units °C
- Readings[readingCnt].time = rtc.getEpoch();
- digitalWrite(LED_BUILTIN, LOW);
- delay(1000);
- readingCnt++;
- if (readingCnt >= maximumReadings) {
- WiFi.begin((char *)ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(250);
- }
- while (i<maximumReadings) {
- checkConnection();
- doPg();
- if ((pg_status == 2) && (i<maximumReadings)){
- tosendstr = "insert into burst values(24,1," + String(Readings[i].time) + "," + String(Readings[i].temp) + ")";
- conn.execute(tosendstr.c_str());
- pg_status = 3;
- delay(50);
- i++;
- }
- delay(50);
- }
- }
- if (readingCnt < maximumReadings) {
- esp_sleep_enable_timer_wakeup(sleeptimeSecs * 1000000);
- esp_deep_sleep_start();
- }
- }
- void loop()
- {
- checkConnection();
- doPg();
- /*if ((pg_status == 2) && (i<maximumReadings)){
- tosendstr = "insert into burst values(24,1," + String(Readings[i].time) + "," + String(Readings[i].temp) + ")";
- conn.execute(tosendstr.c_str());
- pg_status = 3;
- delay(50);
- i++;
- }*/
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement