initial
This commit is contained in:
220
AWS_IoT_WiFi_copy.ino
Normal file
220
AWS_IoT_WiFi_copy.ino
Normal file
@@ -0,0 +1,220 @@
|
||||
#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 angle = 0; // variable to hold the angle for the servo motor
|
||||
int pos;
|
||||
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(0); //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(0);
|
||||
burner_status = 0;
|
||||
Serial.println("ALL CLEAR ALL CLEAR ALL CLEAR!!!");
|
||||
}
|
||||
|
||||
void ignite(){
|
||||
if(!burner_status){
|
||||
myservo.write(burner_range); //full open to ignite space
|
||||
delay(3000);//wait for ignition
|
||||
myservo.write(burner_range-20.0);//bring back from ignite space to HIGH
|
||||
burner_status = 1;
|
||||
Serial.println("BURNER IS ON BE CAREFUL!!!");
|
||||
}
|
||||
}
|
||||
|
||||
void set_to_percent(int percent){
|
||||
//percent should be 0 to 100 aka 50% is 50
|
||||
float percent_f = percent/100.0;
|
||||
Serial.print("setting position to: ");
|
||||
Serial.println(burner_range*percent_f);
|
||||
if(!burner_status){
|
||||
ignite();
|
||||
}
|
||||
myservo.write(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(20);
|
||||
}else if(preset.equalsIgnoreCase("medium")){
|
||||
set_to_percent(50);
|
||||
}else if(preset.equalsIgnoreCase("high")){
|
||||
set_to_percent(90);
|
||||
}
|
||||
}
|
||||
BIN
Servo_Adapter_v1.stl
Normal file
BIN
Servo_Adapter_v1.stl
Normal file
Binary file not shown.
BIN
Servo_Adapter_v6.stl
Normal file
BIN
Servo_Adapter_v6.stl
Normal file
Binary file not shown.
23
arduino_secrets.h
Normal file
23
arduino_secrets.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#define SECRET_SSID "Arduino"
|
||||
#define SECRET_PASS "arduino1234"
|
||||
#define SECRET_BROKER "a28odbooub39nj-ats.iot.us-east-1.amazonaws.com"
|
||||
// Fill in the boards public certificate
|
||||
const char SECRET_CERTIFICATE[] = R"(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICjzCCAXegAwIBAgIVAPF+nrITqEa2WHyqWXkn1CBqY2m6MA0GCSqGSIb3DQEB
|
||||
CwUAME0xSzBJBgNVBAsMQkFtYXpvbiBXZWIgU2VydmljZXMgTz1BbWF6b24uY29t
|
||||
IEluYy4gTD1TZWF0dGxlIFNUPVdhc2hpbmd0b24gQz1VUzAeFw0yMTA0MjEyMzM5
|
||||
MTdaFw00OTEyMzEyMzU5NTlaMB4xCzAJBgNVBAYTAlVTMQ8wDQYDVQQDEwZEYXND
|
||||
U1IwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQy49178G79jZMraB6B/mHL19CX
|
||||
mhWVgoFB9iyKYBnb4XmNpWSoC4TYeZslGTxaZrq907K8oj7zykQquIT6L++Uo2Aw
|
||||
XjAfBgNVHSMEGDAWgBSvxvTntexUFrbJt2mPmVUv3NXjmDAdBgNVHQ4EFgQUd5sD
|
||||
kRMeRgEGsptRt99/IZzlRXEwDAYDVR0TAQH/BAIwADAOBgNVHQ8BAf8EBAMCB4Aw
|
||||
DQYJKoZIhvcNAQELBQADggEBACen4PvKZbHTa8mgFqikvx9alpNqEwnRFVB4KJn+
|
||||
Dcmr5N/t717j/VHDZ+IPXXJ3XQBrTYKIAucn3y/avAtTW5tbsyRQfgLTBJQfQ2xg
|
||||
tnnYDv3yOAK+RDmdfIB4j4Hyfi6kXHqFo28k2ftvtHbhTEFQWkBjqBKOi+jriISO
|
||||
jdhjuGwCLzVeJF4LOq1eNf1NKZawK94LWKonce5uGwywvg5hS3njoUg4rzrWmsMt
|
||||
xcDSYqFMiGKbIv0n+WQulphOVE2oNaSOJyTqF5YWfC49ZTvzPtxPs5xkYRJpAAq3
|
||||
GpcY7JS3/fzytI8fIve9ig7hVc9kXjyP6cYSkOCgcq/d17Y=
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
)";
|
||||
Reference in New Issue
Block a user