This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
esp8266-temperature/arduino-sensor/arduino-sensor.ino

154 lines
3.2 KiB
C++

#include <ESP8266WiFi.h>
#include <EEPROM.h>
// From https://github.com/DaKaZ/esp8266-restclient
#include "RestClient.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
const char* ssid = "FLRLAN";
const char* password = "XXXXXX";
const char * host = "apis.lab.inf3.xyz";
const int httpsPort = 443;
const char * path = "/esp8266/sensors";
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(115200);
delay(500);
while (!bmp.begin()) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
delay(100);
}
delay(1000);
EEPROM.begin(512);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int c = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(250);
c++;
Serial.print(".");
if (c > 100) {
Serial.println();
Serial.println("Unable to connect. Sleeping for 60 seconds before trying again.");
int sleepTime = 60 * 1e6;
ESP.deepSleep(sleepTime);
}
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
float h = bmp.readPressure();
float t = bmp.readTemperature();
while (isnan(h) || isnan(t) || t > 100 || t < -100) {
h = bmp.readPressure();
t = bmp.readTemperature();
}
Serial.println("Got t=" + String(t) + " and h=" + String(h) + ".");
String content = "{\n";
content += "\"id\": \"" + WiFi.macAddress() + "\",\n";
content += "\"name\": \"" + String(getNodeId()) + "\",\n";
content += "\"temp\": " + String(t) + ",\n";
content += "\"pressure\": " + String(h) + "\n";
content += "}\n";
Serial.println("Sending: " + content);
String response = "";
int statusCode = 0;
while (statusCode < 100 || statusCode >= 500) {
RestClient client = RestClient(host, httpsPort, 1);
const char * contentArr = content.c_str();
statusCode = client.post(path, contentArr, &response);
Serial.println("Got response code: " + String(statusCode));
Serial.println("Got response: " + response);
if (statusCode < 100 || statusCode >= 500) {
delay(5000);
}
}
String sleepTimeStr;
String nodeId;
int sleepTime = sleepTimeStr.toInt() * 1e6;
if (sleepTime < 60 * 1e6) {
sleepTime = 15 * 60 * 1e6;
}
Serial.print("Going to sleep for ");
Serial.print(sleepTime);
Serial.println(" nanoseconds");
ESP.deepSleep(sleepTime);
/*
} else if (value == REQ_SET) {
setNodeId(parameter);
}
*/
}
#define LENGTH_ID 10
void setNodeId(String value) {
const char* c = value.c_str();
size_t addr = 0x0;
int givenLength = strlen(c);
if (givenLength >= LENGTH_ID) {
givenLength = LENGTH_ID;
}
size_t i = 0;
for (; i < givenLength; i++) {
EEPROM.write(addr + i, (uint8_t) c[i]);
}
for (; i < LENGTH_ID; i++) {
EEPROM.write(addr + i, 0);
}
EEPROM.commit();
}
char* getNodeId() {
size_t addr = 0x0;
char* eepromId = new char[LENGTH_ID];
for (size_t i = 0; i < LENGTH_ID; i++) {
uint8_t temp = EEPROM.read(addr + i);
eepromId[i] = temp;
}
return eepromId;
}