Embedded Networking and Communications

Designing

In this assignment I choosed to work with ESP8266 to implement networking using wireless, where 2 ESP8266 will be connected using WiFi, one will control the others LED.

I started by designing the circuit diagram.

Img: Schematic design

Img: Schematic design

Img: Board design

Img: Board design

Img: 3D view

Img: 3D view

I made 2 identical board to communicate between, one will control the LEDs os the other using push buttons.

Building

I exported PNG images from the PCB I designed a converted them to RML files to use in monoFab machine.

Img: Traces

Img: Traces

Img: Edge

Img: Edge

After designing the board I printed it with SRM-20 machine and soldered all the components in their place.

Img: Printed board

Img: Printed board

Img: Soldered board

Img: Soldered board

Now the next step is to program both boards to communicate among them.

Programming

I programed ESP-03 using Arduino IDE:

Transimitter program:

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

const char* APIkey = "P3C4GHF6CNT08T2I";   // API key from thingspeak.com
const char* host = "api.thingspeak.com"; // link of the server.

const char* ssid = "FabLab_2.4";
const char* pass = "innovate";

int btn_A = 16;
int btn_B = 17;

void setup() {
  Serial.begin(115200);
  WiFiClient client;

  pinMode(btn_A, INPUT);
  pinMode(btn_B, INPUT);
  
  WiFi.begin(ssid, pass);
  Serial.println("connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  
}

void loop() {
  WiFiClient client;
  const int httpPort = 80;

  if (!client.connect(host, httpPort)){
    return;
  }

  String updateURL = "/update?api_key=";
  updateURL += APIkey;
  updateURL += "&field1=";
  
  if (digitalRead(btn_A) == HIGH){
    updateURL += "10\r\n";
    sendURL(updateURL);
  }
  if (digitalRead(btn_B) == HIGH){
    updateURL += "01\r\n";
    sendURL(updateURL);
  }
  if ((digitalRead(btn_A) == HIGH) && (digitalRead(btn_B) == HIGH)){
    updateURL += "11\r\n";
    sendURL(updateURL);
  }
}

void sendURL(String url){
     WiFiClient client;
  client.print(String("GET ") + url + "HTTP/1.1\r\n" + 
                  "Host: " + host + "\r\n" + 
                  "Connection: close\r\n\r\n");
  delay(1000);
}

Receiver program:

nclude <ESP8266WiFi.h>
#include <WiFiClient.h>

const char* APIkey = "P3C4GHF6CNT08T2I";   // API key from thingspeak.com
const char* host = "api.thingspeak.com"; // link of the server.

const char* ssid = "FabLab_2.4";
const char* pass = "innovate";

int led_A = 5;
int led_B = 6;
int led_C = 7;
int led_D = 13;

void setup() {
  Serial.begin(115200);
  WiFiClient client;

  pinMode(led_A, OUTPUT);
  pinMode(led_B, OUTPUT);
  pinMode(led_C, OUTPUT);
  pinMode(led_D, OUTPUT);
  
  WiFi.begin(ssid, pass);
  Serial.println("connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  
}

void loop() {
  WiFiClient client;
  const int httpPort = 80;

  if (!client.connect(host, httpPort)){
    return;
  }

  String getURL = "GET /channels/641607/fields/1.json?api_key=";
  getURL += APIkey;   // with only the API you will get all result.
  String toHost = "Host: ";
  toHost += host;

  client.println(getURL);
  client.println(toHost);
  client.println("Connection: close");
  client.println();

  unsigned long lastRead = millis();
  while (millis() - lastRead < 2000){
    while (client.available()){
      Serial.print(client.read());
      lastRead = millis();
    }
  }
  client.stop();
}

Downloads

Files used can be downloaded Here