Code Monkey home page Code Monkey logo

juleverksted-2018's Introduction

juleverksted-2018

Felles repo for Juleverksted 2018. Last gjerne opp kode som blir produsert ila. kvelden hit. NB! ta vekk SID og passord fra koden!

Blinkestjerne

Blinkestjerne er en liten applikasjon som kjører på ESP8266. Applikasjonen kan kontrollere et LED lys til å være av/på eller blinke i et gitt intervall.

Biblioteker

Følgende biblioteker må inkluderes får og kunne koble på WiFi og connecte til MQTT broker.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

Connect WiFI

Endre SID og wifi-passord. Vi bruker bouvet sitt gjestenett.

char ssid[] = "<SID>";
char pass[] = "<PASS>";

Connect til WiFi kan gjøres med følgende kode.

void wifiConnect() {
  Serial.print("Connecting to Wifi ssid: " + String(ssid));
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print(" Connected, ip: ");
  Serial.println(WiFi.localIP());
}

Connect MQTT

void mqtt() {
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection to " + String(mqtt_server) + "...");
    mqttClient.setServer(mqtt_server, 1883);
    mqttClient.setCallback(mqttCallback);
    if (mqttClient.connect(my_name)) {
      mqttClient.subscribe(topic_subscribe);
      Serial.println(" Connected! Subscribes on topic " + String(topic_subscribe));
    } else {
      Serial.print("failed, rc=");
      Serial.println(mqttClient.state());
      delay(1000);
    }
  }
  mqttClient.loop();
}

MQTT Publish

void publiser(boolean ledActive) {
  int status = ledActive ? 10 : 0;
  mqttClient.publish(topic_publish, ("{\"" + String(my_name) + "\":" + String(status) + "}").c_str(), true);
}

MQTT Callback

char message_buff[100];
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  int i = 0;
  for (i = 0; i < length && i < 99; i++) {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
  String msg = String(message_buff);
    int verdi = msg.toInt();
    timer.deleteTimer(timerId);
    if (verdi >= 10) {
      Serial.println(String(my_name) + ": on");
      setLedActive(true);
    } else if (verdi <= 0) {
      Serial.println(String(my_name) + ": off");
      setLedActive(false);
    } else {
      Serial.println(String(my_name) + ": blink every " + String(200L * verdi) + "ms");
      timerId = timer.setInterval(200L * verdi, ledBlink);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.