/*
RTCアラームグローバル割り込みのタイマー精度を測定する
PIN Connections (Using STM32F103):
W5X00 - STM32F103
---------------------
VCC - 3.3V
GND - GND
SS - Pin PA4
SCLK - Pin PA5
MISO - Pin PA6
MOSI - Pin PA7
RST - PullUp
*/
#include <SPI.h>
#include <Ethernet_STM.h> //
https://github.com/rogerclarkmelbourne/Arduino_STM32
#include <PubSubClient.h> //
https://github.com/knolleary/pubsubclient
#include <STM32Sleep.h>
#include <RTClock.h>
#define MQTT_SERVER
"192.168.10.40"
//#define MQTT_SERVER
"broker.hivemq.com"
//#define MQTT_SERVER
"iot.eclipse.org"
#define MQTT_PORT 1883
#define MQTT_KEEP_ALIVE 60
#define MQTT_PUB_TOPIC "stm32f103/ALARM" // You can
change
#define MQTT_WILL_TOPIC "stm32f103/ALARM" // You can
change
#define MQTT_WILL_MSG "" // You can change
#define RUNNING_LED PA0
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a
sticker on the shield
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC
address of WIZ550io
;
#else
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
#endif
EthernetClient ethClient;
PubSubClient pubsubClient(ethClient);
RTClock rt(RTCSEL_LSE);
long alarmDelay = 3600;
//long alarmDelay = 60;
int alarmFlag = 0;
char clientid[30];
void errorDisplay(char* buff) {
int stat = 0;
rt.detachAlarmInterrupt();
Serial.print("Error:");
Serial.println(buff);
while(1) {
digitalWrite(RUNNING_LED,stat);
stat = !stat;
delay(100);
}
}
void running_func () {
digitalWrite(RUNNING_LED,!digitalRead(RUNNING_LED));
}
void publish_func () {
alarmFlag = 1;
rt.setAlarmTime(rt.getTime()+alarmDelay);
}
void setup() {
delay(1000);
Serial.begin(9600);
pinMode(RUNNING_LED, OUTPUT);
digitalWrite(RUNNING_LED,LOW);
// start Ethernet
#if defined(WIZ550io_WITH_MACADDRESS)
if (Ethernet.begin() == 0) {
#else
if (Ethernet.begin(mac) == 0) {
#endif
errorDisplay("Failed to configure
Ethernet using DHCP");
}
Serial.print("My IP: ");
Serial.println(Ethernet.localIP());
Serial.print("Netmask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("GW IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS IP: ");
Serial.println(Ethernet.dnsServerIP());
pubsubClient.setServer(MQTT_SERVER, MQTT_PORT);
IPAddress ip = Ethernet.localIP();
Serial.print(ip[0]);
Serial.print(".");
Serial.print(ip[1]);
Serial.print(".");
Serial.print(ip[2]);
Serial.print(".");
Serial.println(ip[3]);
sprintf(clientid,"STM32-%03d",(int)ip[3]);
Serial.print("clientid=");
Serial.println(clientid);
rt.attachSecondsInterrupt(running_func);
rt.attachAlarmInterrupt(publish_func);
rt.setAlarmTime(rt.getTime()+alarmDelay);
}
void loop() {
char payload[75];
if (alarmFlag) {
for (int i=0;i<10;i++) {
Serial.print("Attempting
MQTT connection...");
// Attempt to connect
if
(pubsubClient.connect(clientid,MQTT_WILL_TOPIC,0,0,MQTT_WILL_MSG))
break;
delay(1000);
}
if (!pubsubClient.connected()) {
errorDisplay("connect
Fail");
}
Serial.println("connected");
alarmFlag = 0;
snprintf (payload, 75, "hello world!!
I'm STM32F103");
Serial.print("Publish message: ");
Serial.println(payload);
if
(!pubsubClient.publish(MQTT_PUB_TOPIC, payload)) {
errorDisplay("publish
fail");
}
pubsubClient.disconnect();
}
}
|