This week was about making boards/microcontrollers talk, following different protocols, through wires and wirelessly.
Some of the question raised were:
- What are the differences between the communication protocols?
- What communication can a ATMega 328P/ a ESP8266 handle?
I mainly used 1 workflow:
Eagle > Fabmodule > VModel > Arduino IDE
My biggest achievement:
My biggest struggle:: Hardware
SMD interruptor |
Thursday | Friday | Monday | Tuesday | |
---|---|---|---|---|
Fablab festival | Satchakits | Fixing Satchakits | Wifi board desgin | |
Wifi board fabrication |
Softwares | Fonction |
---|---|
ESP8266.lbr | Eagle addition |
ESP8266 board | Arduino IDE addition |
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// #define RELAY_PIN 15
const char* ssid = "WoMa"; // remplacer par le SSID de votre WiFi
const char* password = "WOrkingMAking"; // remplacer par le mot de passe de votre WiFi
ESP8266WebServer server(80); // on instancie un serveur ecoutant sur le port 80
void setup(void){
//pinMode(15, OUTPUT);
Serial.begin(115200);
// on demande la connexion au WiFi
WiFi.begin(ssid, password);
Serial.println("");
// on attend d'etre connecte au WiFi avant de continuer
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// on affiche l'adresse IP qui nous a ete attribuee
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// on definit ce qui doit etre fait lorsque la route /bonjour est appelee
// ici on va juste repondre avec un "hello !"
//server.on("/bonjour", [](){
//server.send(200, "text/plain", "hello !");
//});
server.on("/on", []() {
pinMode(15, OUTPUT);
digitalWrite(15, LOW);
server.send(200, "text/plain", "relay is ON");
});
server.on("/off", [](){
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
server.send(200, "text/plain", "relay is OFF");
});
// on commence a ecouter les requetes venant de l'exterieur
server.begin();
}
void loop(void){
// a chaque iteration, on appelle handleClient pour que les requetes soient traitees
server.handleClient();
}
#include <Arduino.h>
#include <ESP8266WiFi.h>
#ifndef DataToMaker_h
class DataToMaker
{
public:
DataToMaker(const char*, String); // constructor
bool connect();
bool setValue(int, String);
void sendToMaker();
void post();
protected: // it is protected because the subclass needs access
//to max distance!
private:
void compileData();
WiFiClient client;
const char* privateKey;
String event;
String value1, value2, value3 = "";
bool dataAvailable;
String postData;
};
DataToMaker::DataToMaker(const char* _privateKey, String _event)
{
privateKey = _privateKey;
event = _event;
}
bool DataToMaker::connect()
{
if (client.connect("maker.ifttt.com", 80))
return true;
else return false;
}
void DataToMaker::post()
{
compileData();
client.print("POST /trigger/");
client.print(event);
client.print("/with/key/");
client.print(privateKey);
client.println(" HTTP/1.1");
client.println("Host: maker.ifttt.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
if (dataAvailable)
{ // append json values if available
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.println(postData);
}
else
client.println();
}
bool DataToMaker::setValue(int valueToSet, String value)
{
switch (valueToSet)
{
case 1:
value1 = value;
break;
case 2:
value2 = value;
break;
case 3:
value3 = value;
break;
default:
return false;
break;
}
return true;
}
void DataToMaker::compileData()
{
if (value1 != "" || value2 != "" || value3 != "")
{
dataAvailable = true;
bool valueEntered = false;
postData = "{";
if (value1 != "")
{
postData.concat("\"value1\":\"");
postData.concat(value1);
valueEntered = true;
}
if (value2 != "")
{
if (valueEntered)postData.concat("\",");
postData.concat("\"value2\":\"");
postData.concat(value2);
valueEntered = true;
}
if (value3 != "")
{
if (valueEntered)postData.concat("\",");
postData.concat("\"value3\":\"");
postData.concat(value3);
}
postData.concat("\"}");
}
else dataAvailable = false;
}
#endif
#include <ESP8266WiFi.h>
#include <arduino.h>
#include "DataToMaker.h"
#define SERIAL_DEBUG // Uncomment this to dissable serial debugging
// define gpio pins here:
#define NUMBER_OF_SENSORS 1 // THIS MUST MATCH THE NUMBER OF SENSORS IN THE SENSOR ARRAY / NO MORE THAN 3
#define GARAGE_DOOR_PIN 4 // GPIO2 (D4)
// pin for heatbeat LED
#define HEARTBEAT_PIN 13 //GPIO13
// Define program constants
const char* myKey = "mykey"; // your maker key here
const char* ssid = "Chacha"; // your router ssid here
const char* password = "mypassword"; // your router password here
// define program values
int sensors[NUMBER_OF_SENSORS] = {GARAGE_DOOR_PIN}; // place your defined sensors in the curly braces
String doorStates[2] = {"Closed", "Open"}; // You can change the LOW / HIGH state strings here
// declare new maker event with the name "ESP"
DataToMaker event(myKey, "ESP");
// LEAVE SET
int pvsValues[NUMBER_OF_SENSORS];
bool connectedToWiFI = false;
void setup()
{
#ifdef SERIAL_DEBUG
Serial.begin(115200);
delay(200);
Serial.println();
#endif
delay(10); // short delay
pinMode(HEARTBEAT_PIN, OUTPUT);
for (int i = 0 ; i < NUMBER_OF_SENSORS ; i++)
{
pinMode(sensors[i], INPUT);
pvsValues[i] = -1; // initialise previous values to -1 to force initial output
}
WiFi.mode(WIFI_STA);
ConnectWifi();
}
void loop() {
if (wifiConnected)
{
if (DetectChange())
{
debugln("connecting...");
if (event.connect())
{
debugln("Connected To Maker");
event.post();
}
else debugln("Failed To Connect To Maker!");
}
delay(1000); // pause for 1 second
digitalWrite(HEARTBEAT_PIN, !digitalRead(HEARTBEAT_PIN));
}
else
{
delay(60 * 1000); // 1 minute delay before trying to re connect
ConnectWifi();
}
}
bool ConnectWifi()
{
// Connect to WiFi network
debugln();
debugln();
debug("Connecting to ");
debugln(ssid);
unsigned long startTime = millis();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED && startTime + 30 * 1000 >= millis()) {
delay(500);
debug(".");
}
if (WiFi.status() == WL_CONNECTED)
{
debugln("");
debugln("WiFi connected");
}
else
{
WiFi.disconnect();
debugln("");
debugln("WiFi Timed Out!");
}
}
bool wifiConnected()
{
return WiFi.status() == WL_CONNECTED;
}
bool DetectChange()
{
int val;
bool changed = false;
for (int i = 0 ; i < NUMBER_OF_SENSORS ; i++)
{
if ((val = digitalRead(sensors[i])) != pvsValues[i])
{
pvsValues[i] = val;
event.setValue(i + 1, doorStates[val]);
debug("Changes Detected On Value");
debugln(String(i + 1));
changed = true;
}
}
if (!changed) debugln("No Changes Detected");
return changed;
}
void debug(String message)
{
#ifdef SERIAL_DEBUG
Serial.print(message);
#endif
}
void debugln(String message)
{
#ifdef SERIAL_DEBUG
Serial.println(message);
#endif
}
void debugln()
{
#ifdef SERIAL_DEBUG
Serial.println();
#endif
}