For this week, we needed to create two processors that communicated with each other. Since my final project is going to involve creating sensors that can be deployed and will communicate over Wi-Fi, I decided to create a simple board with the 8266 to get used to programming the 8266.

I followed Yuval’s notes that were sent out right after he had spent a lot of time debugging the board. However, the ESP8266 wasn’t in our Eagle library and so I had to manually import it. Unfortunately, when I imported it, I didn’t carefully look at all the variable versions of the module. I ended up taking version 12 of the module, which had 2 of the pins flipped (Pin 4 and Pin 5) and also didn’t have any of the CLK, MOSI, MISO, CSO and GPIO10 pin available. This caused a problem as I need these pins not to be shorted and they would’ve shorted on my board. Also, I put the wrong regulator into my board when creating it, putting the SOT23 for the 0.1amps instead of the 1Amp, as is needed by the 8266.

Update

I finally got my 8266 circuit working! I found the correct SOT23 packaging for the voltage regulator at 1 ampere in the Sparkfun-Power eagle library that has a lot of components. This made my life infinitely easier - I had the write component and was able to progress forward. As can be seen in the final project week, I was able to successfully program the Wi-Fi to work and communicate with my server on the Internet.

Before that, I also programmed it to turn an LED on / off based off of a website. After programming the 8266 thanks to Yuval’s notes and these awesome notes from 2015, I was able to have two separate URLs that the 8266 would show as a server and flip the LED pin whether it got requests there or not.

Here’s the code to do that - the LED is connected to pin 13 on the 8266.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

//#include <Adafruit_BME280.h>
//
//#include <Wire.h> 
//#include "cactus_io_BME280_I2C.h" 


//BME280_I2C bme(0x76); // I2C using address 0x76 
const char* ssid = "EECS-MTL-RLE";
const char* password = "";

ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

void handleInline() {
  digitalWrite(led, 1);
//  delay(2000);
}

void communicateOverI2C() {

  
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
//  Wire.begin(2,14);
//  if (!bme.begin()) { 
//     Serial.println("Could not find a valid BME280 sensor, check wiring!"); 
//      while (1); 
//   } 
  
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    handleInline();
    server.send(200, "text/plain", "this works as well");
  });

  server.on("/offline", []() {
    digitalWrite(led,0);
    server.send(200, "text/plain", "LED off");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}