Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <WiFi101.h>
- #include <MQTTClient.h>
- #define MOOTTORI A1
- #define POTENTIOMETRI A2
- #define WIFI_NAME "12345678"
- #define PASSWORD "asdfasdf"
- char wifi_name[] = WIFI_NAME;
- char password[] = PASSWORD;
- WiFiClient wifi_client;
- MQTTClient mqtt_client;
- int status = WL_IDLE_STATUS;
- void setup() {
- Serial.begin(9600);
- WiFi.setPins(8, 7, 4, 2);
- while(status != WL_CONNECTED) {
- Serial.print("Yhdistetään: ");
- Serial.println(wifi_name);
- status = WiFi.begin(wifi_name, password);
- delay(10000);
- }
- Serial.println("Yhdistetty");
- mqtt_client.begin("broker.shiftr.io", wifi_client);
- while (!mqtt_client.connect("asdf", "aalto-shiftr-testi", "aalto-shiftr-testi")){
- Serial.println("Yhdistetään shiftriin");
- delay(1000);
- }
- Serial.println("Yhdistetty shiftriin");
- mqtt_client.onMessage(update);
- mqtt_client.subscribe("/komento_asdf");
- }
- void update(String &topic, String &message){
- Serial.println("Uusi viesti:");
- Serial.println(message);
- if(message == "full"){
- full_speed();
- }
- else if(message == "half"){
- half_speed();
- }
- else if(message == "off"){
- off();
- }
- }
- void full_speed(){
- analogWrite(MOOTTORI, 255);
- }
- void half_speed(){
- analogWrite(MOOTTORI, 128);
- }
- void off(){
- analogWrite(MOOTTORI, 0);
- }
- void loop() {
- mqtt_client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement