This week Mike survives a technology meltdown to bring some personal experiences with memory management on the Arduino, he thinks you will be surprised about how little memory there is in your sketch for the Arduino.
Then Mike builds a web server on an Arduino than can both take in data and put out data. Learn how to make your remote sensors easy to communicate with.
Downloads
Basic Memory Management
/*############################## BASIC MEMORY MANAGEMENT ##################################
This sketch shows the examples of how fixed strings in your code take up memory
space and how you can put them into the program space to save some memory.
From: Mike Myers (http://mikemyers.me) @netnutmike
Let's Make It Episode 49 (http://letmakeit.tv)
http://tech-zen.tv
For the sample code, show notes, contact information and many more
videos, visit the show page at http://tech-zen.tv/letsmakeit
Please subscribe to our YouTube channel or our netcasts at any of
your favorite netcast / podcast outlets.
We normally record Let's Make It live on Tuesday evenings around
7pm eastern. You can watch it live by going to tech-zen.tv and clicking
the live link at the top of the page.
We also have a community setup for our viewers and listeners. You can
join the community by going to community.tech-zen.tv.
We love input on what you would like to know or if you have an idea for
a new Let's Make it episode, you can contact us via email or phone at
the show information page.
################################################################################*/
#include <MemoryFree.h>
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("");
//with the memory macro
Serial.println(F("Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that “all men are created equal."));
Serial.println(F("Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. "));
Serial.println(F("We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this."));
Serial.println(F("But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. "));
Serial.println(F("The world will little note, nor long remember what we say here, but it can never forget what they did here."));
Serial.println(F("It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. "));
Serial.println(F("It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."));
Serial.println(F("Your Text"));
//without the memory macro
/*Serial.println("Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that “all men are created equal.");
Serial.println("Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. ");
Serial.println("We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.");
Serial.println("But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. ");
Serial.println("The world will little note, nor long remember what we say here, but it can never forget what they did here.");
Serial.println("It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. ");
Serial.println("It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.");
*/
Serial.println("");
Serial.println(freeMemory());
Serial.println("");
delay(10000);
}
Basic Web Server
/*############################## BASIC WEB SERVER ##################################
This sketch shows an example of how to create a basic web server.
This sketch has 3 pages, the default which has a link to page #2,
Page #2 which has 4 input fields on it. The last one is the
receiver for the 4 input fields which prints to the serial interface
the input values.
From: Mike Myers (http://mikemyers.me) @netnutmike
Let's Make It Episode 49 (http://letmakeit.tv)
http://tech-zen.tv
For the sample code, show notes, contact information and many more
videos, visit the show page at http://tech-zen.tv/letsmakeit
Please subscribe to our YouTube channel or our netcasts at any of
your favorite netcast / podcast outlets.
We normally record Let's Make It live on Tuesday evenings around
7pm eastern. You can watch it live by going to tech-zen.tv and clicking
the live link at the top of the page.
We also have a community setup for our viewers and listeners. You can
join the community by going to community.tech-zen.tv.
We love input on what you would like to know or if you have an idea for
a new Let's Make it episode, you can contact us via email or phone at
the show information page.
################################################################################*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include "WebServer.h" // For web interface
#include <Wire.h>
#include <dht11.h>
static uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3F, 0xB6 }; // MAC address of your device
static uint8_t ip[] = { 10, 232, 1, 251 };
WebServer webserver("", 80);
dht11 DHT11(4);
void webDefaultView(WebServer &server, WebServer::ConnectionType type)
{
P(htmlHead) =
""
""
"Test Web Page 1"
"" "" "";
server.httpSuccess(); server.printP(htmlHead); server.print(F("
<h1>Web Default View</h1>
"));
server.print(F("
Click Here to go to page 2 with the form
"));
int chk = DHT11.read();
// Read the sensor if (chk == 0) {
server.print(F("<h2>Current Tempterature</h2>"));
server.print(DHT11.temperature);
server.print(F("c / "));
server.print(DHT11.fahrenheit());
server.print(F("f
"));
server.print(DHT11.humidity);
server.print(F("% Humidity<br /><br />"));
} else
Serial.println(chk); server.print(F(""));
}
void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
webDefaultView(server, type);
}
void page2cmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
int i;
P(htmlHead) = "" "" "Test Web Page 2" "" "" "";
server.httpSuccess();
server.printP(htmlHead);
server.print(F("<
<h1>Page 2 with form</h1>"));
server.print(F("<div style="width: 660px; margin-left: 10px;"><form action="/form" method="post">"));
for (i = 1; i <= 4; ++i) {
server.print(F("Name #"));
server.print(i);
server.print(F(": <input id="NAME"));
server.print(i);
server.print(F("" type="text" name=""NAME"));" size="16" /><br />"));
}
server.print(F("<p><input type="submit" value="Submit" />"));
server.print(F("</p><p>"));
}
void formCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
if (type == WebServer::POST) {
bool repeat; char name[16], value[16];
do {
repeat = server.readPOSTparam(name, 16, value, 16);
String Name = String(name);
if (Name.startsWith("NAME")) {
int addr = strtoul(name + 4, NULL, 10);
Serial.print("Name #");
Serial.print(addr);
Serial.print(": ");
Serial.println(value);
}
} while (repeat);
server.httpSeeOther("/page2");
} else
page2cmd(server, type, url_tail, tail_complete);
}
void setup() {
DHT11.attach(4);
Serial.begin(9600);
Ethernet.begin(mac, ip); //start web server
webserver.begin();
webserver.setDefaultCommand(&defaultCmd); //http://10.232.1.250
webserver.addCommand("page2", &page2cmd); //http://10.232.1.250/page2
webserver.addCommand("form", &formCmd); //http://10.232.1.250/form
}
void loop() {
webserver.processConnection();
}