--- tags: eit, fablab, sensors --- # EIT - Measuring the world ## Introduction presentation <iframe src="https://docs.google.com/presentation/d/e/2PACX-1vRuJxg_qFhcjX-FIR2zSbKDQk0lFXMzzbZWZ8VufBZ83tc9HkyJiry_5coi3tgv896QJSHiC3VPjcFJ/embed?start=false&loop=false&delayms=60000" frameborder="0" width="100%" height="450px" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe> [Slides Source](https://docs.google.com/presentation/d/1egmqE3LeEjBsvsuSUmuAIV08Y1fHd-jMeCMpUtFQrfI/edit?usp=sharing) ## Tools - [I collect the data and I use it (and maybe also share it)]() - [Someone collects the data and I use it ](https://hackmd.io/PeT27HFwQz2Ks4IMkqTAWQ) ## Hands on ### Exercises for the class: ### Electronic with ESP32: The board that we are using is [ESP32-based feather HUZZAH32](https://learn.adafruit.com/adafruit-huzzah32-esp32-feather) by adafruit. The first step for programming esp32 base module is to install its library for [arduino IDE](https://www.arduino.cc/en/software). In order to do so follow along [this tutorial](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/). #### links: - [ESP32-based feather HUZZAH32](https://learn.adafruit.com/adafruit-huzzah32-esp32-feather) - [arduino IDE](https://www.arduino.cc/en/software) - [Official repository of esp32 library](https://github.com/espressif/arduino-esp32) - [Installing esp32 library tutorial](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/) - [circuito.io](https://www.circuito.io/app) * Hello World: - Blink - Button - Potentiometer * Sensor: - Read Data - Send Data * IoT: - MQTT #### Hello world: It is a quick introduction for electronic programing and circuit. You can use [circuito.io](https://www.circuito.io/app) for checking how to make the ciruite. ###### Blink: ```c // define the LED pin #define LED_PIN 13 int delayTime = 500; void setup() { pinMode(LED_PIN, OUTPUT); // declare LED pin as output } void loop() { delay(50); digitalWrite(LED_PIN, HIGH); delay(delayTime); digitalWrite(LED_PIN, HIGH); delay(delayTime); } ``` ###### Button: ```c // define the Button pin #define Button_PIN 27 void setup() { Serial.begin(115200); pinMode(Button_PIN, INPUT); // declare LED pin as output } void loop() { delay(50); int val = digitalRead(Button_PIN); if (val == 1){ Serial.println("Button pressed!"); delay(200); } } ``` ###### Potentiometer: ```c // function to map the analong value float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin GIOP36: int analogValue = analogRead(36); // Rescale to potentiometer's voltage (from 0V to 3.3V): float voltage = floatMap(analogValue, 0, 4095, 0, 3.3); // print out the value you read: Serial.print("Analog: "); Serial.print(analogValue); Serial.print(", Voltage: "); Serial.println(voltage); delay(1000); } ``` ### MQTT MQTT was developed by Andy Stanford-Clark (IBM) and Arlen Nipper (Eurotech; now Cirrus Link) in 1999 for the monitoring of an oil pipeline through the desert. The goals were to have a protocol, which is bandwidth-efficient and uses little battery power, because the devices were connected via satellite link and this was extremely expensive at that time. The protocol uses a publish/subscribe architecture wich is event-driven and enables messages to be pushed to clients. The central communication point is the MQTT broker, it is in charge of dispatching all messages between the senders and the rightful receivers. Each client that publishes a message to the broker, includes a topic into the message. The topic is the routing information for the broker. Each client that wants to receive messages subscribes to a certain topic and the broker delivers all messages with the matching topic to the client. Therefore the clients don’t have to know each other, they only communicate over the topic. >[source](http://fabacademy.org/2020/labs/barcelona/local/#material/extras/week14/diynetworks) - [mqtt](https://mqtt.org/) ###### Broker - ip : `````` - port: ```1883``` - user: ```moon``` - pswd: ```shot``` ```c #include <WiFi.h> #include <PubSubClient.h> const char* ssid = ""; const char* password = ""; WiFiClient wifiClient; const char* mqttBroker = "mqtt-staging.smartcitizen.me"; const char* mqttClientName = "fablabbcn102"; const char* topicToSub = "lab/test"; const char* topicToPub = "lab/pubG1"; PubSubClient mqttClient(wifiClient); const int buttPin = 13; bool buttState; void mqttConnect() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); if (mqttClient.connect(mqttClientName)) { Serial.println("connected"); mqttClient.publish("hello", mqttClientName); // Topic(s) subscription mqttClient.subscribe(topicToSub); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void callback(char* topic, byte* message, unsigned int length) { String receivedMsg; for (int i = 0; i < length; i++) { receivedMsg += (char)message[i]; } Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); Serial.println(receivedMsg); if (String(topic) == topicToSub) { // Add here the code to act on input -------------- // Arduino String obejct reference : // https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/ if (receivedMsg == ("1")) { Serial.println("on"); digitalWrite(LED_BUILTIN, HIGH); } else if (receivedMsg == "0") { Serial.println("off"); digitalWrite(LED_BUILTIN, LOW); } //------------------------------------------------- } } void setup() { Serial.begin(115200); // Connect to wifi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // // MQTT setup mqttClient.setServer(mqttBroker, 1883); mqttClient.setCallback(callback); // Led setup pinMode(LED_BUILTIN, OUTPUT); } unsigned long lastMsg = 0; char msg[50]; void loop() { // Check the current button state bool currentState = digitalRead(buttPin); // Check if we are still connected to the MQTT broker if (!mqttClient.connected()) { mqttConnect(); } // Let PubSubClient library do his magic mqttClient.loop(); // Add your publish code here -------------------- // OPTION 1 -- Publish based on events if (currentState != buttState) { buttState = currentState; snprintf (msg, 50, "%i", 1); mqttClient.publish(topicToPub, msg); } // OPTION 2 -- Publish based on Timer // Only publish every 5 seconds // unsigned long now = millis(); // if (now - lastMsg > 5000) { // // lastMsg = now; // snprintf (msg, 50, "%ld - hello again!", now); // Serial.print("Publish message: "); // Serial.println(msg); // // mqttClient.publish(topicToPub, msg); // } } ``` ## Orange [download](https://orangedatamining.com/blog/page/2/) ## [Raw Graph](https://www.rawgraphs.io/)