229 lines
5.9 KiB
C++
229 lines
5.9 KiB
C++
#include "arduino_secrets.h"
|
|
/*
|
|
AWS IoT WiFi
|
|
|
|
This sketch securely connects to an AWS IoT using MQTT over WiFi.
|
|
It uses a private key stored in the ATECC508A and a public
|
|
certificate for SSL/TLS authetication.
|
|
|
|
It publishes a message every 5 seconds to arduino/outgoing
|
|
topic and subscribes to messages on the arduino/incoming
|
|
topic.
|
|
|
|
The circuit:
|
|
- Arduino MKR WiFi 1010 or MKR1000
|
|
|
|
The following tutorial on Arduino Project Hub can be used
|
|
to setup your AWS account and the MKR board:
|
|
|
|
https://create.arduino.cc/projecthub/132016/securely-connecting-an-arduino-mkr-wifi-1010-to-aws-iot-core-a9f365
|
|
|
|
This example code is in the public domain.
|
|
*/
|
|
|
|
#include <ArduinoBearSSL.h>
|
|
#include <ArduinoECCX08.h>
|
|
#include <ArduinoMqttClient.h>
|
|
#include <WiFi101.h> // change to #include <WiFi101.h> for MKR1000
|
|
#include <Servo.h>
|
|
|
|
Servo myservo; // create a servo object
|
|
int burner_status = 1; //assume on for safety
|
|
float burner_range = 170.0;
|
|
/////// Enter your sensitive data in arduino_secrets.h
|
|
const char ssid[] = SECRET_SSID;
|
|
const char pass[] = SECRET_PASS;
|
|
const char broker[] = SECRET_BROKER;
|
|
const char* certificate = SECRET_CERTIFICATE;
|
|
|
|
WiFiClient wifiClient; // Used for the TCP socket connection
|
|
BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508
|
|
MqttClient mqttClient(sslClient);
|
|
|
|
unsigned long lastMillis = 0;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
//while (!Serial);
|
|
|
|
if (!ECCX08.begin()) {
|
|
Serial.println("No ECCX08 present!");
|
|
while (1);
|
|
}
|
|
|
|
// Set a callback to get the current time
|
|
// used to validate the servers certificate
|
|
ArduinoBearSSL.onGetTime(getTime);
|
|
|
|
// Set the ECCX08 slot to use for the private key
|
|
// and the accompanying public certificate for it
|
|
sslClient.setEccSlot(0, certificate);
|
|
|
|
// Optional, set the client id used for MQTT,
|
|
// each device that is connected to the broker
|
|
// must have a unique client id. The MQTTClient will generate
|
|
// a client id for you based on the millis() value if not set
|
|
//
|
|
// mqttClient.setId("clientId");
|
|
|
|
// Set the message callback, this function is
|
|
// called when the MQTTClient receives a message
|
|
mqttClient.onMessage(onMessageReceived);
|
|
myservo.attach(5); // attaches the servo on pin 9 to the servo object
|
|
myservo.write(179); //set servo to starting position
|
|
burner_status = 0; //should be off now
|
|
}
|
|
|
|
void loop() {
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
connectWiFi();
|
|
}
|
|
|
|
if (!mqttClient.connected()) {
|
|
// MQTT client is disconnected, connect
|
|
connectMQTT();
|
|
}
|
|
|
|
// poll for new MQTT messages and send keep alives
|
|
mqttClient.poll();
|
|
|
|
// publish a message roughly every 5 seconds.
|
|
// if (millis() - lastMillis > 5000) {
|
|
// lastMillis = millis();
|
|
//
|
|
// publishMessage();
|
|
// }
|
|
|
|
}
|
|
|
|
unsigned long getTime() {
|
|
// get the current time from the WiFi module
|
|
return WiFi.getTime();
|
|
}
|
|
|
|
void connectWiFi() {
|
|
Serial.print("Attempting to connect to SSID: ");
|
|
Serial.print(ssid);
|
|
Serial.print(" ");
|
|
|
|
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
|
|
// failed, retry
|
|
Serial.print(".");
|
|
delay(5000);
|
|
}
|
|
Serial.println();
|
|
|
|
Serial.println("You're connected to the network");
|
|
Serial.println();
|
|
}
|
|
|
|
void connectMQTT() {
|
|
Serial.print("Attempting to MQTT broker: ");
|
|
Serial.print(broker);
|
|
Serial.println(" ");
|
|
|
|
while (!mqttClient.connect(broker, 8883)) {
|
|
// failed, retry
|
|
Serial.print(".");
|
|
delay(5000);
|
|
}
|
|
Serial.println();
|
|
|
|
Serial.println("You're connected to the MQTT broker");
|
|
Serial.println();
|
|
|
|
// subscribe to a topic
|
|
mqttClient.subscribe("arduino/incoming");
|
|
}
|
|
|
|
void publishMessage() {
|
|
Serial.println("Publishing message");
|
|
|
|
// send message, the Print interface can be used to set the message contents
|
|
mqttClient.beginMessage("arduino/outgoing");
|
|
mqttClient.print("hello ");
|
|
mqttClient.print(millis());
|
|
mqttClient.endMessage();
|
|
}
|
|
|
|
void onMessageReceived(int messageSize) {
|
|
// we received a message, print out the topic and contents
|
|
Serial.print("Received a message with topic '");
|
|
Serial.print(mqttClient.messageTopic());
|
|
Serial.print("', length ");
|
|
Serial.print(messageSize);
|
|
Serial.println(" bytes:");
|
|
//pos = mqttClient.parseInt();
|
|
//Serial.println(pos);
|
|
//myservo.write(pos);
|
|
//set_to_percent(pos);
|
|
String message = mqttClient.readString();
|
|
Serial.println("Recieved preset: " + message);
|
|
if(messageSize > 1){ //know more than 1 char means either string or number >= 10
|
|
if(message.toInt()){
|
|
set_to_percent(message.toInt());
|
|
}else {
|
|
set_to_preset(message);
|
|
}
|
|
}else { //know that only one char exists meaning a number less than 10 so send to percent
|
|
if(!message.toInt()){
|
|
off();
|
|
}else {
|
|
set_to_percent(message.toInt());
|
|
}
|
|
}
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
}
|
|
void off(){
|
|
myservo.write(179);
|
|
burner_status = 0;
|
|
Serial.println("ALL CLEAR ALL CLEAR ALL CLEAR!!!");
|
|
}
|
|
|
|
void ignite(){
|
|
if(!burner_status){
|
|
myservo.write(10); //full open to ignite space
|
|
delay(2000);//wait for ignition
|
|
myservo.write(20.0);//bring back from ignite space to HIGH
|
|
burner_status = 1;
|
|
Serial.println("BURNER IS ON BE CAREFUL!!!");
|
|
}
|
|
}
|
|
|
|
void toggle(){
|
|
if(burner_status){
|
|
off();
|
|
}else {
|
|
ignite();
|
|
}
|
|
}
|
|
|
|
void set_to_percent(int percent){
|
|
//percent should be 0 to 100 aka 50% is 50
|
|
float percent_f = 1.0 - percent/100.0;
|
|
Serial.print("setting position to: ");
|
|
Serial.println((int)(burner_range*percent_f));
|
|
if(!burner_status){
|
|
ignite();
|
|
}
|
|
myservo.write((int)(burner_range * percent_f)); //naivly assume range is 180 for now it's not actually 180
|
|
}
|
|
|
|
void set_to_preset(String preset){
|
|
Serial.println(preset);
|
|
if (preset == "off"){
|
|
Serial.println("Turning off");
|
|
off();
|
|
}else if(preset.equalsIgnoreCase("low")){
|
|
set_to_percent(10);
|
|
}else if(preset.equalsIgnoreCase("medium")){
|
|
set_to_percent(35);
|
|
}else if(preset.equalsIgnoreCase("high")){
|
|
set_to_percent(60);
|
|
}else if(preset.equalsIgnoreCase("toggle")){
|
|
toggle();
|
|
}
|
|
}
|