Moonbase Otago - Dunedin Arduino project - Networking

WeMOS D1 Networking

Connecting to WiFi

There are lots of ESP8266 wifi examples in the Arduino app's Examples menu - let's start with something simple File->Examples->ESP8266WiFi->WiFiClient - this program:

Start by editing these two lines:


   const char* ssid     = "your-ssid";
   const char* password = "your-password";

Replace "your-ssid" by the name of your local wifi access point (in quotes "") and "your-password" with it password, if you don't have WiFi you may be able to turn on a hotspot on a phone

Next you need to say which web site you want to access - start with mine, in this line:


   const char* host = "data.sparkfun.com";

Replace "data.sparkfun.com" with "moonbase.com"

And give it a URL to access - replace these lines:


   String url = "/input/";
   url += streamId;
   url += "?private_key=";
   url += privateKey;
   url += "&value=";
   url += value;


With:


   String url = "/index.html";

Compile and load the program and quickly switch to the serial monitor window (or perhaps push the reset button after you have gone to the monitor window) - you should see something like this:


    Connecting to my-wifi-ap
    .......
    WiFi connected
    IP address: 
    192.168.0.151

If you don't see this check that your ssid and password are correct, make sure you can connect with a laptop, if you need to type something into a page to connect you may need to try a different WiFi access point,

    connecting to moonbaseotago.com
    Requesting URL: /index.html

    HTTP/1.1 200 OK

    ....

    <!DOCTYPE html>
    <html>
    <head>
    <title>Moonbase Otago - Open source hardware</title>
    <meta name="author" content="Paul Campbell" >

    ....

followed by a whole bunch of HTML - you just read something from a web page.

Turning on a remote LED

Attach an LED to pin D6 as shown in the output section.

Find the program File->Examples->ESP8266WiFi->WiFiWebServer - as in the previous example start by changing your ssid and password to match your wifi access point's. Look though the file and change the "2" in these lines to "D6":


  pinMode(2, OUTPUT);
  digitalWrite(2, 0);

  ....

  digitalWrite(2, val);

Compile and load the program - as it starts it will display something like:


Connecting to my-wifi-ap
.......
WiFi connected
Server started
192.168.0.151

Go to a computer (or a phone) on the same wifi access point - using the IP address from startup (in this case "192.168.0.151") enter the URL http://192.168.0.151/gpio/1 - this should turn D6 on, http://192.168.0.151/gpio/0 should turn it off.

Exercise 1: create a web server that tells you whether it's day or night

Exercise 2: create a web server that tells you the temperature, you will need to calibrate the thermistor - you can do it for a few points in the range you care about and use linear interpolation for the points in between

Exercise 3: If you have a second D1 combine the first example and this one to have a switch on one D1 to turn on/off an LED on another

Exercise 4: If you have two D1's create a web server that turns an LED on and off, and a second D1 that tells the first one to turn the LED on when it senses darkness - what happens when the LED is pointed at the light sensor in a dark room?

Logging data to a remote server

We'll provide this section later when we have a server you can use