From 64def2a70ec7f876567b4212e7af29a38b5ee93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Tue, 2 Jun 2020 00:58:01 +0200 Subject: [PATCH 1/4] EEPROM experiments --- arduino/eeprom_get/eeprom_get.ino | 64 +++++++++++++++++++++++++++++++ arduino/eeprom_put/eeprom_put.ino | 53 +++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 arduino/eeprom_get/eeprom_get.ino create mode 100644 arduino/eeprom_put/eeprom_put.ino diff --git a/arduino/eeprom_get/eeprom_get.ino b/arduino/eeprom_get/eeprom_get.ino new file mode 100644 index 0000000..e43abe0 --- /dev/null +++ b/arduino/eeprom_get/eeprom_get.ino @@ -0,0 +1,64 @@ +/*** + eeprom_get example. + + This shows how to use the EEPROM.get() method. + + To pre-set the EEPROM data, run the example sketch eeprom_put. + This sketch will run without it, however, the values shown + will be shown from what ever is already on the EEPROM. + + This may cause the serial object to print out a large string + of garbage if there is no null character inside one of the strings + loaded. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +void setup() { + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + readConfig(); //Run the next test. +} + +struct config { + byte mac[6]; + byte remoteAddr[4]; + char serial[4]; + int remotePort; + char name[128]; +}; + +void printAddr(byte addr[], int size, Stream *stream) { + for (int thisByte = 0; thisByte < size; thisByte++) { + // print the value of each byte of the IP address: + stream->print(addr[thisByte], HEX); + if (thisByte < size - 1) { + stream->print("."); + } + } +} + +void readConfig() { + int eeAddress = 0; //Move address to the next byte after float 'f'. + + config customVar; //Variable to store custom object read from EEPROM. + EEPROM.get(eeAddress, customVar); + + Serial.println("Read custom object from EEPROM: "); + printAddr(customVar.mac, sizeof(customVar.mac), &Serial); + Serial.println(); + printAddr(customVar.remoteAddr, sizeof(customVar.remoteAddr), &Serial); + Serial.println(); + Serial.println(customVar.remotePort); + Serial.println(customVar.serial); + Serial.println(customVar.name); +} + +void loop() { + /* Empty loop */ +} diff --git a/arduino/eeprom_put/eeprom_put.ino b/arduino/eeprom_put/eeprom_put.ino new file mode 100644 index 0000000..82b88d6 --- /dev/null +++ b/arduino/eeprom_put/eeprom_put.ino @@ -0,0 +1,53 @@ +/*** + eeprom_put example. + + This shows how to use the EEPROM.put() method. + Also, this sketch will pre-set the EEPROM data for the + example sketch eeprom_get. + + Note, unlike the single byte version EEPROM.write(), + the put method will use update semantics. As in a byte + will only be written to the EEPROM if the data is actually + different. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include + +struct config { + byte mac[6]; + byte remoteAddr[4]; + char serial[4]; + int remotePort; + char name[128]; +}; + +void setup() { + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + int eeAddress = 0; //Location we want the data to be put. + + /** Put is designed for use with custom structures also. **/ + + //Data to store. + config customVar = { + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }, + { 192, 168, 10, 123 }, + "abcd", + 80, + "sensor.server.domain" + }; + + EEPROM.put(eeAddress, customVar); + Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); +} + +void loop() { + /* Empty loop */ +} From 09b90e41f067b68638873c73cf3a6216cafeaf10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Tue, 2 Jun 2020 22:38:05 +0200 Subject: [PATCH 2/4] Add ino sketch to program EEPROM --- arduino/eeprom_prog/eeprom_prog.ino | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 arduino/eeprom_prog/eeprom_prog.ino diff --git a/arduino/eeprom_prog/eeprom_prog.ino b/arduino/eeprom_prog/eeprom_prog.ino new file mode 100644 index 0000000..2efacbf --- /dev/null +++ b/arduino/eeprom_prog/eeprom_prog.ino @@ -0,0 +1,53 @@ +#include +#include + +#define ERASE_FIRST 0 + +const byte mac[] = { + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +const char serial[] = "abcd1234"; + +struct netConfig { + IPAddress address; + int port; +}; + +netConfig config = { + {192, 168, 10, 123}, + 8000 +}; + +void setup() { + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + #if ERASE_FIRST + // initialize the LED pin as an output. + pinMode(13, OUTPUT); + // turn the LED on while erasing + digitalWrite(13, HIGH); + for (int i = 0 ; i < EEPROM.length() ; i++) { + EEPROM.write(i, 0); + } + // turn the LED on when we're done + digitalWrite(13, LOW); + #endif + + int eeAddress = 0; //Location we want the data to be put. + + //One simple call, with the address first and the object second. + EEPROM.put(eeAddress, mac); + eeAddress += sizeof(mac); + EEPROM.put(eeAddress, serial); + eeAddress += sizeof(serial); + + EEPROM.put(eeAddress, config); + Serial.println("Data written!"); +} + +void loop() { + /* Empty loop */ +} From 1b0d069b1d0eb21d03005aa13fc192aa1989451a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Tue, 2 Jun 2020 22:38:20 +0200 Subject: [PATCH 3/4] Read config from EEPROM --- arduino/tempLightSensor/tempLightSensor.ino | 73 ++++++++++++--------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/arduino/tempLightSensor/tempLightSensor.ino b/arduino/tempLightSensor/tempLightSensor.ino index 7b7b494..6d9c1e3 100644 --- a/arduino/tempLightSensor/tempLightSensor.ino +++ b/arduino/tempLightSensor/tempLightSensor.ino @@ -1,3 +1,4 @@ +#include #include #include @@ -12,53 +13,65 @@ JsonObject payload = doc.createNestedObject("payload"); JsonObject temp = payload.createNestedObject("temperature"); int tempPin = A0; -int tempReading; int photocellPin = A1; -int photocellReading; -const byte mac[] = { - 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; -const byte remoteAddr[] = { - 192, 168, 10, 123 }; -const int remotePort = 8000; -const int postDelay = 10*1000; +struct netConfig { + IPAddress address; + int port; +}; +netConfig config; -const String serialNum = "abcde12345"; const String URL = "/telemetry/"; - -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("."); - } - } -} +const int postDelay = 10 * 1000; void setup(void) { Serial.begin(9600); 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) { 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: - for(;;) - ; - } + while (true) { + delay(1); + } + } + EEPROM.get(eeAddress, config); - Serial.print("IoT started at address: "); - printAddr(Ethernet.localIP(), &Serial); + Serial.print("IoT #"); + Serial.print(serial); + Serial.println(" started at address:"); + Serial.println(Ethernet.localIP()); 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; } 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 tempC = (tempVoltage - 0.5) * 100 ; @@ -69,14 +82,14 @@ void loop(void) { temp["raw"] = tempReading; temp["volts"] = tempVoltage; - if (EthernetClient client = client.connect(remoteAddr, remotePort)) { + if (EthernetClient client = client.connect(config.address, config.port)) { client.print("POST "); client.print(URL); client.println(" HTTP/1.1"); client.print("Host: "); - printAddr(remoteAddr, &client); + printAddr(config.address, &client); client.print(":"); - client.println(remotePort); + client.println(config.port); client.println("Content-Type: application/json"); client.print("Content-Length: "); client.println(measureJsonPretty(doc)); From 2cb8a1b5376a8db960974234a8db68708ba5c58b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Tue, 2 Jun 2020 22:39:41 +0200 Subject: [PATCH 4/4] Add Arduino sketch svg --- arduino/eeprom_get/eeprom_get.ino | 64 - arduino/eeprom_put/eeprom_put.ino | 53 - arduino/tempLightSensor/tempLightSketch.svg | 3280 +++++++++++++++++++ 3 files changed, 3280 insertions(+), 117 deletions(-) delete mode 100644 arduino/eeprom_get/eeprom_get.ino delete mode 100644 arduino/eeprom_put/eeprom_put.ino create mode 100644 arduino/tempLightSensor/tempLightSketch.svg diff --git a/arduino/eeprom_get/eeprom_get.ino b/arduino/eeprom_get/eeprom_get.ino deleted file mode 100644 index e43abe0..0000000 --- a/arduino/eeprom_get/eeprom_get.ino +++ /dev/null @@ -1,64 +0,0 @@ -/*** - eeprom_get example. - - This shows how to use the EEPROM.get() method. - - To pre-set the EEPROM data, run the example sketch eeprom_put. - This sketch will run without it, however, the values shown - will be shown from what ever is already on the EEPROM. - - This may cause the serial object to print out a large string - of garbage if there is no null character inside one of the strings - loaded. - - Written by Christopher Andrews 2015 - Released under MIT licence. -***/ - -#include - -void setup() { - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - readConfig(); //Run the next test. -} - -struct config { - byte mac[6]; - byte remoteAddr[4]; - char serial[4]; - int remotePort; - char name[128]; -}; - -void printAddr(byte addr[], int size, Stream *stream) { - for (int thisByte = 0; thisByte < size; thisByte++) { - // print the value of each byte of the IP address: - stream->print(addr[thisByte], HEX); - if (thisByte < size - 1) { - stream->print("."); - } - } -} - -void readConfig() { - int eeAddress = 0; //Move address to the next byte after float 'f'. - - config customVar; //Variable to store custom object read from EEPROM. - EEPROM.get(eeAddress, customVar); - - Serial.println("Read custom object from EEPROM: "); - printAddr(customVar.mac, sizeof(customVar.mac), &Serial); - Serial.println(); - printAddr(customVar.remoteAddr, sizeof(customVar.remoteAddr), &Serial); - Serial.println(); - Serial.println(customVar.remotePort); - Serial.println(customVar.serial); - Serial.println(customVar.name); -} - -void loop() { - /* Empty loop */ -} diff --git a/arduino/eeprom_put/eeprom_put.ino b/arduino/eeprom_put/eeprom_put.ino deleted file mode 100644 index 82b88d6..0000000 --- a/arduino/eeprom_put/eeprom_put.ino +++ /dev/null @@ -1,53 +0,0 @@ -/*** - eeprom_put example. - - This shows how to use the EEPROM.put() method. - Also, this sketch will pre-set the EEPROM data for the - example sketch eeprom_get. - - Note, unlike the single byte version EEPROM.write(), - the put method will use update semantics. As in a byte - will only be written to the EEPROM if the data is actually - different. - - Written by Christopher Andrews 2015 - Released under MIT licence. -***/ - -#include - -struct config { - byte mac[6]; - byte remoteAddr[4]; - char serial[4]; - int remotePort; - char name[128]; -}; - -void setup() { - - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - int eeAddress = 0; //Location we want the data to be put. - - /** Put is designed for use with custom structures also. **/ - - //Data to store. - config customVar = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }, - { 192, 168, 10, 123 }, - "abcd", - 80, - "sensor.server.domain" - }; - - EEPROM.put(eeAddress, customVar); - Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!"); -} - -void loop() { - /* Empty loop */ -} diff --git a/arduino/tempLightSensor/tempLightSketch.svg b/arduino/tempLightSensor/tempLightSketch.svg new file mode 100644 index 0000000..cf07828 --- /dev/null +++ b/arduino/tempLightSensor/tempLightSketch.svg @@ -0,0 +1,3280 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + SCL + SDA + AREF + GND + IOREF + RESET + 3V3 + PWM + PWM + PWM + L + TX + RX + ON + ICSP + PWM + PWM + PWM + TX + RX + 3 + 1 + 2 + 1 + 1 + 1 + 0 + 1 + 9 + 8 + DIGITAL + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 + 1 + 5V + Gnd + POWER + www.arduino.cc + ANALOG IN + Vin + 0 + 1 + 2 + 3 + 4 + 5 + Arduino + UNO + + + + + + + + + + + + + + + + + + + + + + 7 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + 5 + 5 + 10 + 10 + 15 + 15 + 20 + 20 + 25 + 25 + 30 + 30 + A + A + B + B + C + C + D + D + E + E + F + F + G + G + H + H + I + I + J + J + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AREF + GND + RESET + 3V3 + LINK + PWR + 100M + FULLD + COLL + RX + TX + INT + WP + ICSP + RESET + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + TX + RX + 0 + 5V + Gnd + MEGA compatible + ANALOG IN + 9V + A0 + 1 + 2 + 3 + 4 + A5 + Arduino + ETHShield + SD + + + + + + + + + + + + + + + + + + + + + + + + + + + TMP + 36 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + AREF + GND + RESET + 3V3 + LINK + PWR + 100M + FULLD + COLL + RX + TX + INT + WP + ICSP + RESET + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + TX + RX + 0 + 5V + Gnd + MEGA compatible + ANALOG IN + 9V + A0 + 1 + 2 + 3 + 4 + A5 + Arduino + ETHShield + SD + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +