mirror of
https://github.com/daniviga/bite.git
synced 2025-02-17 05:59:14 +01:00
Read config from EEPROM
This commit is contained in:
parent
09b90e41f0
commit
1b0d069b1d
@ -1,3 +1,4 @@
|
|||||||
|
#include <EEPROM.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
@ -12,53 +13,65 @@ JsonObject payload = doc.createNestedObject("payload");
|
|||||||
JsonObject temp = payload.createNestedObject("temperature");
|
JsonObject temp = payload.createNestedObject("temperature");
|
||||||
|
|
||||||
int tempPin = A0;
|
int tempPin = A0;
|
||||||
int tempReading;
|
|
||||||
int photocellPin = A1;
|
int photocellPin = A1;
|
||||||
int photocellReading;
|
|
||||||
|
|
||||||
const byte mac[] = {
|
struct netConfig {
|
||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
IPAddress address;
|
||||||
const byte remoteAddr[] = {
|
int port;
|
||||||
192, 168, 10, 123 };
|
};
|
||||||
const int remotePort = 8000;
|
netConfig config;
|
||||||
const int postDelay = 10*1000;
|
|
||||||
|
|
||||||
const String serialNum = "abcde12345";
|
|
||||||
const String URL = "/telemetry/";
|
const String URL = "/telemetry/";
|
||||||
|
const int postDelay = 10 * 1000;
|
||||||
void printAddr(byte addr[], Stream *stream) {
|
|
||||||
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
|
||||||
// print the value of each byte of the IP address:
|
|
||||||
stream->print(addr[thisByte], DEC);
|
|
||||||
if (thisByte < 3) {
|
|
||||||
stream->print(".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
analogReference(EXTERNAL);
|
analogReference(EXTERNAL);
|
||||||
|
|
||||||
|
byte mac[6];
|
||||||
|
char serial[9];
|
||||||
|
|
||||||
|
int eeAddress = 0;
|
||||||
|
|
||||||
|
EEPROM.get(eeAddress, mac);
|
||||||
|
eeAddress += sizeof(mac);
|
||||||
|
EEPROM.get(eeAddress, serial);
|
||||||
|
eeAddress += sizeof(serial);
|
||||||
|
|
||||||
|
Serial.println("Initialize Ethernet with DHCP:");
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
Serial.println("Failed to configure Ethernet using DHCP");
|
||||||
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||||
|
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
||||||
|
} else if (Ethernet.linkStatus() == LinkOFF) {
|
||||||
|
Serial.println("Ethernet cable is not connected.");
|
||||||
|
}
|
||||||
// no point in carrying on, so do nothing forevermore:
|
// no point in carrying on, so do nothing forevermore:
|
||||||
for(;;)
|
while (true) {
|
||||||
;
|
delay(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
EEPROM.get(eeAddress, config);
|
||||||
|
|
||||||
Serial.print("IoT started at address: ");
|
Serial.print("IoT #");
|
||||||
printAddr(Ethernet.localIP(), &Serial);
|
Serial.print(serial);
|
||||||
|
Serial.println(" started at address:");
|
||||||
|
Serial.println(Ethernet.localIP());
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
Serial.println("Connecting to:");
|
||||||
|
Serial.print(config.address);
|
||||||
|
Serial.print(":");
|
||||||
|
Serial.println(config.port);
|
||||||
|
|
||||||
doc["device"] = 1; // FIXME
|
doc["device"] = serial; // FIXME
|
||||||
payload["id"] = serverName;
|
payload["id"] = serverName;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
photocellReading = analogRead(photocellPin);
|
|
||||||
tempReading = analogRead(tempPin);
|
int photocellReading = analogRead(photocellPin);
|
||||||
|
int tempReading = analogRead(tempPin);
|
||||||
|
|
||||||
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
||||||
float tempC = (tempVoltage - 0.5) * 100 ;
|
float tempC = (tempVoltage - 0.5) * 100 ;
|
||||||
@ -69,14 +82,14 @@ void loop(void) {
|
|||||||
temp["raw"] = tempReading;
|
temp["raw"] = tempReading;
|
||||||
temp["volts"] = tempVoltage;
|
temp["volts"] = tempVoltage;
|
||||||
|
|
||||||
if (EthernetClient client = client.connect(remoteAddr, remotePort)) {
|
if (EthernetClient client = client.connect(config.address, config.port)) {
|
||||||
client.print("POST ");
|
client.print("POST ");
|
||||||
client.print(URL);
|
client.print(URL);
|
||||||
client.println(" HTTP/1.1");
|
client.println(" HTTP/1.1");
|
||||||
client.print("Host: ");
|
client.print("Host: ");
|
||||||
printAddr(remoteAddr, &client);
|
printAddr(config.address, &client);
|
||||||
client.print(":");
|
client.print(":");
|
||||||
client.println(remotePort);
|
client.println(config.port);
|
||||||
client.println("Content-Type: application/json");
|
client.println("Content-Type: application/json");
|
||||||
client.print("Content-Length: ");
|
client.print("Content-Length: ");
|
||||||
client.println(measureJsonPretty(doc));
|
client.println(measureJsonPretty(doc));
|
||||||
|
Loading…
Reference in New Issue
Block a user