---
title: FabLab
tags: arduino, fablab
robots: noindex, nofollow
author: Julien Noyer
---
# Ressources
http://sharemycode.fr/dlm
arduino
https://www.arduino.cc
Raspberrry Pi
https://www.raspberrypi.org
Tinkercad
https://www.tinkercad.com
Logique de code
https://hackmd.io/aeEuYFntTSiI8Wb3kSaXKQ
Configurer la Raspberry PI
https://desertbot.io/blog/headless-raspberry-pi-3-bplus-ssh-wifi-setup
# Premier pas en électronique
Présentation d'un sketch de base __Arduino__
```c
// Les variables
char userLastname = "Noyer";
const int birthDate = 1979;
int myArray[5] = { 21, 34, 73, 45, 12 };
bool isActive = false;
void setup() {
// Les conditions
if( isActive ){
Serial.print('Active');
}
else{
Serial.print('Not active');
};
isActive ? Serial.print('Active') : Serial.print('Not active');
switch(isActive){
case true:
Serial.print('Active');
break;
defaut:
Serial.print('Not active');
breeak;
};
}
void loop() {
// Les boucles
for( int i = 0; i < 5; i++ ){
Serial.print(myArray[i]);
};
}
```
## Equivalent Javascript
```js
// Les varriables
let userLastname = 'Noyer'; // => :String || :Char
const birthDate = 1979; // => :Integer | :Number
let myArray = [ 21, 34, 73, 45, 12 ]; // => Array<Integer>
let isActive = false; // => :Boolean
// Les fonctions
const setup = () => {
// Les conditions
if( isActive ){
console.log('Active')
}
else{
consolee.log('Not active')
}
isActive ? console.log('Active') : console.loog('Not active');
switch(isActive){
case true:
console.log('Active');
break;
defaut:
console.log('Not active');
breeak;
}
}
const loop = () => {
// Les boucles
for( let i = 0; i < myArray.length; i++ ){
console.log(myArray[i])
}
}
```
# Blink
```c
/*
This program blinks pin 13 of the Arduino (the
built-in LED)
*/
int redLed = 13;
int greenLed = 12;
bool red = true;
void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
}
void loop()
{
// Vérifier la valeur de red
if( red = true ){
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
else{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
};
// Attendrer une seconde
delay(1000);
// Inverser la valeur de red
red = !red;
}
```
# LED + Button
```c
/*
This program blinks pin 13 of the Arduino (the
built-in LED)
*/
int redLed = 13;
int greenLed = 12;
int button = 2;
int buttonState = 0;
void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
// Récupérer l'état du bouton
buttonState = digitalRead(button);
// Vérifier l'état du bouton
if( buttonState == HIGH ){
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
else{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
};
// Attendrer une seconde
delay(10);
}
```
## Wemos MQTT
```c
/*
Projet d'apprentissage d'un objet connecté (IoT) pour réaliser une sonde de température
ESP8266 + DHT22 + LED + MQTT + Home-Assistant
Projets DIY (https://www.projetsdiy.fr) - Mai 2016
Licence : MIT
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define wifi_ssid "ADE-WIFI-ETUDIANTS"
#define wifi_password "wifi@ade2018"
#define mqtt_server "10.6.72.225"
#define mqtt_user "guest" // s'il a été configuré sur Mosquitto
#define mqtt_password "guest" // idem
#define status_topic "m1dev/julien/etat" //Topic état de la lumière
#define switch_topic "m1dev/julien/commande"
#define LIGHT_PIN D5
#define NOM_CLIENT "julien"
bool debug = true; //Affiche sur la console si True
//Buffer qui permet de décoder les messages MQTT reçus
char message_buff[100];
long lastMsg = 0; //Horodatage du dernier message publié sur MQTT
long lastRecu = 0;
bool light_status = false;
bool initial = true;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(LIGHT_PIN, OUTPUT); //Pin 2
digitalWrite(LIGHT_PIN, HIGH);
if (debug) {
Serial.begin(9600); //Facultatif pour le debug
}
setup_wifi(); //On se connecte au réseau wifi
// client.setServer(WiFi.gatewayIP(), 1883); //Configuration de la connexion au serveur MQTT
client.setServer(mqtt_server, 1883); //Configuration de la connexion au serveur MQTT
client.setCallback(callback); //La fonction de callback qui est executée à chaque réception de message
}
//Connexion au réseau WiFi
void setup_wifi() {
delay(10);
if (debug) {
Serial.println();
Serial.print("Connexion a ");
Serial.println(wifi_ssid);
Serial.println(WiFi.macAddress());
}
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (debug) {
Serial.print(".");
}
}
if (debug) {
Serial.println("");
Serial.println("Connexion WiFi etablie : " + String(wifi_ssid));
Serial.print("=> Addresse IP : " );
Serial.println(WiFi.localIP());
Serial.print("=> Addresse MAC : ");
Serial.println(WiFi.macAddress());
Serial.println("...");
}
}
//Reconnexion
void reconnect() {
//Boucle jusqu'à obtenur une reconnexion
while (!client.connected()) {
if (debug) {
Serial.println("Connexion au serveur MQTT...");
}
// if (client.connect(String(WiFi.macAddress()).c_str())) {
// if (client.connect(String("WeMos:"+WiFi.macAddress()).c_str(), mqtt_user, mqtt_password)) {
if (client.connect(NOM_CLIENT, mqtt_user, mqtt_password)) {
if (debug) {
Serial.println("SUCCESS");
}
} else {
if (debug) {
Serial.println("ERROR : " + String(client.state()));
Serial.println("Nouvel essai dans 5 secondes");
Serial.println("...");
}
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (initial) {
publish_status(light_status);
initial = false;
}
long now = millis();
//Envoi d'un message par minute
if (now - lastMsg > 1000 * 60) {
lastMsg = now;
if ( debug ) {
Serial.print("Commande : ");
Serial.println(light_status ? "on" : "off");
}
}
if (now - lastRecu > 100 ) {
lastRecu = now;
client.subscribe(switch_topic);
}
}
void publish_status (bool status) {
if (status) {
client.publish(status_topic, String("on").c_str(), true); //Publie l'état de la lampe ON
} else {
client.publish(status_topic, String("off").c_str(), true); //Publie l'état de la lampe OFF
}
if ( debug ) {
Serial.print("Commande : ");
Serial.println(status ? "on" : "off");
}
}
// Déclenche les actions à la réception d'un message
// D'après http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2mio
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
if ( debug ) {
Serial.println("Message recu => topic: " + String(topic));
Serial.println(" | longueur: " + String(length,DEC));
}
// create character buffer with ending null terminator (string)
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if ( debug ) {
Serial.println("Payload: " + msgString);
}
if ( msgString == "on" ) {
digitalWrite(LIGHT_PIN, HIGH);
light_status = true;
publish_status(light_status);
} else {
digitalWrite(LIGHT_PIN, LOW);
light_status = false;
publish_status(light_status);
}
}
```