コード例 #1
0
ファイル: main.cpp プロジェクト: vortex314/stm32_programmer
extern "C" void loop() {
	Actor::eventLoop();
	if (WiFi.status() != WL_CONNECTED) {
		Serial.print("Connecting to ");
		Serial.print(WIFI_SSID);
		Serial.println("...");
		WiFi.begin(WIFI_SSID, WIFI_PSWD);

		if (WiFi.waitForConnectResult() != WL_CONNECTED) {
			LOGF(" still connecting ");
			return;
		}
		LOGF("WiFi connected");
	}

	if (WiFi.status() == WL_CONNECTED) {
//	if (wifi.connected()) {
		if (!client.connected()) {
			mdns.onWifiConnected(Header(INIT));
			IPAddress server = mdns.query("mqtt");
			client.set_server(server,1883);
			if (client.connect("wibo1", prefix + "system/alive", 1, 0,
					"false")) {
				client.publish(prefix + "system/alive", "true");
				client.set_callback(callback);
				client.subscribe(subscribe_topic);
				Log.setOutput(udpLog);
				LOGF(" mqtt client connected ");
			} else {
				LOGF(" mqtt connect failed ")
			}
		}
		if (client.connected())
			client.loop();
	}
コード例 #2
0
void loop() {
  ArduinoOTA.handle();

  // -- MQTT connect
    if (!client.connected()) {
      error_path += thisDevicePath;
      error_path += "/";
      error_path += "errors/";
      error_path += thisDeviceName;
      if (client.connect(MQTT::Connect(thisDeviceName).set_will(error_path, "disconnected"))) {
        Serial.println("MQTT connected");
        client.set_callback(callback);
        client.subscribe(MQTT::Subscribe()
          .add_topic("/deviceInfo/#")
          .add_topic("/global/#")
          .add_topic(subscribe_path)
                 );
        // notify persistence of device IP
          String persistence_ip_path = "/persistence/control/";
          persistence_ip_path += thisDeviceName;
          persistence_ip_path += "/ip";
          client.publish(MQTT::Publish(persistence_ip_path, local_ip_str).set_qos(2));

        // ask persistence/control/device_name/chipID "request states" --  do you have any states with my device_name or chipID
          String persistence_path = "/persistence/control/";
          persistence_path += thisDeviceName;
          persistence_path += "/";
          persistence_path += chip_id;
          client.publish(MQTT::Publish(persistence_path, "request states").set_qos(2));
          Serial.println("request states sent");
      }
    }

    if (client.connected()) {
      client.loop();
    }


  // -- NeoPixel continuous update
    for(int i=0; i<PixelCount; i++) {
      strip.SetPixelColor(i, RgbColor (redValue, greenValue, blueValue));
    }
    strip.Show();


  yield();
}
コード例 #3
0
void loop() {
  ArduinoOTA.handle();

  // -- MQTT connect
    if (!client.connected()) {
      error_path += thisDevicePath;
      error_path += "/";
      error_path += "errors/";
      error_path += thisDeviceName;
      if (client.connect(MQTT::Connect(thisDeviceName).set_will(error_path, "disconnected"))) {
        Serial.println("MQTT connected");
        client.set_callback(callback);
        client.subscribe(MQTT::Subscribe()
          .add_topic("/deviceInfo/#")
          .add_topic("/global/#")
          .add_topic(subscribe_path)
                 );
        // notify persistence of device IP
          String persistence_ip_path = "/persistence/control/";
          persistence_ip_path += thisDeviceName;
          persistence_ip_path += "/ip";
          client.publish(MQTT::Publish(persistence_ip_path, local_ip_str).set_qos(2));

        // ask persistence/control/device_name/chipID "request states" --  do you have any states with my device_name or chipID
          String persistence_path = "/persistence/control/";
          persistence_path += thisDeviceName;
          persistence_path += "/";
          persistence_path += chip_id;
          client.publish(MQTT::Publish(persistence_path, "request states").set_qos(2));
          Serial.println("request states sent");
      }
    }

    if (client.connected()) {
      client.loop();
    }

  /*
  // -- Device Request Response
    if (sendJSON) {
      buildDeviceJson(); // flag set to false inside function
    }
  */

  /*
  // -- Confirm Messages
    if (sendConfirm) {
      client.publish(MQTT::Publish(confirmPath, confirmPayload).set_qos(2));
      sendConfirm = false;
    }
  */

  /*
  // -- NeoPixel updates
    if (neoPixelChange) {
      for(int i=0; i<PixelCount; i++) {
        strip.SetPixelColor(i, RgbColor (redValue, greenValue, blueValue));
      }
      strip.Show();
      neoPixelChange = false;
    }
  */

  // -- NeoPixel continuous update
    for(int i=0; i<PixelCount; i++) {
      strip.SetPixelColor(i, RgbColor (redValue, greenValue, blueValue));
    }
    strip.Show();

  /*
  // -- NeoPixel Sunrise Alarm Code
    if (millis() - lastcheck > alarmcheck)  {
      DateTime now = RTC.now();
      lastcheck = millis();
      if (alarmminute == now.minute() && alarmhour == now.hour()) {
         sunrise(4000);
         sunrise(400);
         sunrise(40);
         sunrise(40);
         sunrise(40);
         sunrise(40);
         sunrise(40);
         sunrise(20);
         sunrise(20);
         black();
      }
    }
  */


  // Do things every NTPLimit seconds
  if ( millis() - tick > NTPlimit) {
    tick = millis();

    /*
    // -- get server and send NTP packet
      //get a random server from the pool
      WiFi.hostByName(ntpServerName, timeServerIP);

      sendNTPpacket(timeServerIP); // send an NTP packet to a time server
      // wait to see if a reply is available
      delay(1000);

      int cb = udp.parsePacket();
      if (!cb) {
        Serial.println("no packet yet");
      }
      else {
        //Serial.print("packet received, length=");
        //Serial.println(cb);
        udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

        unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
        unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
        // combine the four bytes (two words) into a long integer
        // this is NTP time (seconds since Jan 1 1900):
        unsigned long secsSince1900 = highWord << 16 | lowWord;
        const unsigned long seventyYears = 2208988800UL;
        unsigned long epoch = secsSince1900 - seventyYears;

        int UTChour = (epoch  % 86400L) / 3600;   // (86400 equals secs per day)
        int UTCmin = (epoch  % 3600) / 60;        // (3600 equals secs per minute)
        //Serial.println(UTChour);

        Serial.print(year(epoch));
        Serial.print('-');
        Serial.print(month(epoch));
        Serial.print('-');
        Serial.println(day(epoch));

        Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
        Serial.print(UTChour);
        Serial.print(':');
        if ( ((epoch % 3600) / 60) < 10 ) {
          Serial.print('0');
        }
        Serial.print(UTCmin);
        Serial.print(':');
        if ( (epoch % 60) < 10 ) {
          Serial.print('0');
        }
        Serial.println(epoch % 60); // print the second
      }
    */

    /*
    // -- begin neropixel test

      delay(1000);

      Serial.println("Colors R, G, B, W...");

      // set the colors,
      // if they don't match in order, you need to use NeoGrbFeature feature
      strip.SetPixelColor(0, RgbColor (128, 0, 0));
      strip.SetPixelColor(1, green);
      strip.SetPixelColor(2, blue);
      strip.SetPixelColor(3, white);
      // the following line demonstrates rgbw color support
      // if the NeoPixels are rgbw types the following line will compile
      // if the NeoPixels are anything else, the following line will give an error
      //strip.SetPixelColor(3, RgbwColor(colorSaturation));
      strip.Show();


      delay(1000);

      Serial.println("Off ...");

      // turn off the pixels
      strip.SetPixelColor(0, black);
      strip.SetPixelColor(1, black);
      strip.SetPixelColor(2, black);
      strip.SetPixelColor(3, black);
      strip.Show();

      delay(1000);

      Serial.println("HSL Colors R, G, B, W...");

      // set the colors,
      // if they don't match in order, you may need to use NeoGrbFeature feature
      strip.SetPixelColor(0, hslRed);
      strip.SetPixelColor(1, hslGreen);
      strip.SetPixelColor(2, hslBlue);
      strip.SetPixelColor(3, hslWhite);
      strip.Show();


      delay(1000);

      Serial.println("Off again...");

      // turn off the pixels
      strip.SetPixelColor(0, hslBlack);
      strip.SetPixelColor(1, hslBlack);
      strip.SetPixelColor(2, hslBlack);
      strip.SetPixelColor(3, hslBlack);
      strip.Show();
    */

    // -- neopixel sunrise test
      //sunrise(40);
      //offblack();

    /*
    // -- print IP address
      Serial.println(WiFi.localIP());
      //Serial.println("..");
    */
  }


  yield();
}