SHARE
TWEET

Untitled

a guest Jun 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <WiFi101.h>
  3. #include <MQTTClient.h>
  4.  
  5. #define MOOTTORI A1
  6. #define POTENTIOMETRI A2
  7.  
  8.  
  9. #define WIFI_NAME "12345678"
  10. #define PASSWORD "asdfasdf"
  11.  
  12. char wifi_name[] = WIFI_NAME;
  13. char password[] = PASSWORD;
  14.  
  15. WiFiClient wifi_client;
  16. MQTTClient mqtt_client;
  17.  
  18. int status = WL_IDLE_STATUS;
  19.  
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.  
  24.   WiFi.setPins(8, 7, 4, 2);
  25.  
  26.   while(status != WL_CONNECTED) {
  27.     Serial.print("Yhdistet√§√§n: ");
  28.     Serial.println(wifi_name);
  29.  
  30.     status = WiFi.begin(wifi_name, password);
  31.     delay(10000);
  32.   }
  33.   Serial.println("Yhdistetty");
  34.  
  35.   mqtt_client.begin("broker.shiftr.io", wifi_client);
  36.  
  37.   while (!mqtt_client.connect("asdf", "aalto-shiftr-testi", "aalto-shiftr-testi")){
  38.     Serial.println("Yhdistet√§√§n shiftriin");
  39.     delay(1000);
  40.   }
  41.   Serial.println("Yhdistetty shiftriin");
  42.  
  43.   mqtt_client.onMessage(update);
  44.   mqtt_client.subscribe("/komento_asdf");
  45. }
  46. void update(String &topic, String &message){
  47.   Serial.println("Uusi viesti:");
  48.   Serial.println(message);
  49.  
  50.   if(message == "full"){
  51.     full_speed();
  52.   }
  53.   else if(message == "half"){
  54.     half_speed();
  55.   }
  56.   else if(message == "off"){
  57.     off();
  58.   }
  59. }
  60. void full_speed(){
  61.   analogWrite(MOOTTORI, 255);
  62. }
  63. void half_speed(){
  64.   analogWrite(MOOTTORI, 128);
  65. }
  66. void off(){
  67.   analogWrite(MOOTTORI, 0);
  68. }
  69. void loop() {
  70.    mqtt_client.loop();
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top