Skip to content
Snippets Groups Projects
Commit 2c6bc67d authored by Alfredo Chissotti's avatar Alfredo Chissotti
Browse files

review

parent da353302
No related branches found
No related tags found
No related merge requests found
......@@ -20,8 +20,8 @@
#include <avr/wdt.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "DaBroswitch"; // your network SSID (name)
char pass[] = "iotLab/00"; // your network password (use for WPA, or use as key for WEP)
const char ssid[] = "DaBroswitch"; // your network SSID (name)
const char pass[] = "iotLab/00"; // your network password (use for WPA, or use as key for WEP)
// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
......@@ -29,42 +29,41 @@ char pass[] = "iotLab/00"; // your network password (use for WPA, or use as k
// 3) Change broker value to a server with a known SSL/TLS root certificate
// flashed in the WiFi module. ???
const char dom_subdom_service[] = "test/aswf/gpio";
const char clientID[] = "aindout";
const char mqttUser[] = "pissir";
const char mqttPassword[] = "pissir2020";
const String dom_subdom_service = "gruppo2/luci/gpio";
const String clientID = "aindout";
const String mqttUser = "pissir";//luci
const String mqttPassword = "pissir2020";//funziona
WiFiSSLClient wifiClient;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//const char broker[] = "193.206.52.98";
//const char broker[] = "192.168.20.67";
const char broker[] = "test.mosquitto.org";//to change (3)
int port = 8883;
const char willTopic[] = "arduino/will";
const char requestTopic[] = "to/all";
const char inTopic[] = "to/";
const char outTopic[] = "from/";
const char statusTopic[] = "gm/station/status";
const char description0[] ="{\"test/aswf/gpio\":[\n";
const char description1[] = "{\"name\":\"AN0\",\"type\":\"analogin\"},\n{\"name\":\"AN1\",\"type\":\"analogin\"},\n";
const char description2[] = "{\"name\":\"AN2\",\"type\":\"analogin\"},\n{\"name\":\"AN3\",\"type\":\"analogin\"},\n";
const char description3[] = "{\"name\":\"AN4\",\"type\":\"analogin\"},\n{\"name\":\"AN5\",\"type\":\"analogin\"},\n";
const char description4[] = "{\"name\":\"OUT2\",\"type\":\"booleanout\"},\n{\"name\":\"OUT3\",\"type\":\"booleanout\"},\n";
const char description5[] = "{\"name\":\"OUT4\",\"type\":\"booleanout\"},\n{\"name\":\"OUT5\",\"type\":\"booleanout\"}\n";
const char descriptionend[] = "]}";
const long interval = 10000;
unsigned long previousMillis = 0;
// define sensors on the 6 Analog inputs
int sensor[6] = {A0, A1, A2, A3, A4, A5};
uint32_t x=0;
int count = 0;
int port = 1883;
const String willTopic = "arduino/will";
const String requestTopic = "to/all";
const String inTopic = "to/";
const String outTopic = "from/";
const String statusTopic = "gm/station/status";
const String jsonBegin = "{\"";
const String descriptionBegin = dom_subdom_service+"\":[\n";
const String descriptionAN0 = "{\"name\":\"AN0\",\"type\":\"analogin\"},\n";
const String descriptionAN1 = "{\"name\":\"AN1\",\"type\":\"analogin\"},\n";
const String descriptionAN2 = "{\"name\":\"AN2\",\"type\":\"analogin\"},\n";
const String descriptionEnd = "]";
const String jsonEnd = "\"}";
const String description = jsonBegin+descriptionBegin+descriptionAN0+descriptionAN1+descriptionAN2+descriptionEnd+jsonEnd;
// define sensors on the 6 Analog inputs
const int number_of_sensors = 3;
const int sensor[number_of_sensors] = {A0, A1, A2};//, A3, A4, A5};
/*
A0 = sensore di luce
A1 = sensore di temperatura
A2 = sensore di suono
*/
void setup() {
//Initialize serial and wait for port to open:
......@@ -143,21 +142,24 @@ void setup() {
//Serial.println(inTopic);
//Serial.println();
wdt_enable(WDTO_2S);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
const long interval = 500;
unsigned long previousMillis = 0;
const bool retained = false;
const int qos = 1;
const bool dup = false;
int count = 0;
void loop() {
int res;
bool firsttime = true;
if(firsttime) {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
firsttime = false;
}
wdt_reset();
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alives which avoids being disconnected by the broker
......@@ -170,88 +172,62 @@ void loop() {
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
String payload;
String source = String(outTopic)+String(dom_subdom_service)+String("/AN");
source += count;
// Serial.println(source);
x = analogRead(sensor[count]);
payload = "{\"event\":\"";
payload += x;
payload += "\"}";
const String source = String(outTopic)+String(dom_subdom_service)+String("/AN")+count;
uint32_t x = analogRead(sensor[count]);
const String payload = jsonBegin+"event\":\""+x+"\", \"sensorNo\":\""+count+jsonEnd;
//= "{\"event\":\""+x+"\", \"sensorNo\":\""+count+"\"}";
// Serial.println(source);
// Serial.println(payload);
Serial.println(source);
Serial.println(payload);
// send message, the Print interface can be used to set the message contents
// in this case we know the size ahead of time, so the message payload can be streamed
bool retained = false;
int qos = 1;
bool dup = false;
mqttClient.beginMessage(source.c_str(), payload.length(), retained, qos, dup);
mqttClient.beginMessage(source.c_str(), payload.length(),retained, qos, dup);
mqttClient.print(payload);
if(mqttClient.endMessage()==0){
Serial.println("I'm disconnected");
while (1);
Serial.println("I'm disconnected. Trying setup...");
setup();
};
count++;
if(count>5) count = 0;
count = count % number_of_sensors;
}
}
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
int rel;
int val;
int res;
bool retained = false;
int qos = 1;
bool dup = false;
String msg;
String topic = mqttClient.messageTopic();
// Serial.println(topic);
while (mqttClient.available()) {
msg += ((char)mqttClient.read());
}
Serial.println(msg);
if(topic=="to/all") {
// Serial.println(mqttClient.messageTopic());
if(msg=="{\"request\":\"description.json\"}"||msg=="{request:description.json}") {
// Serial.println("sending Description");
msg = description0;
msg += description1;
msg += description2;
msg += description3;
msg += description4;
msg += description5;
msg += descriptionend;
String descriptionTopic = String(outTopic)+String(dom_subdom_service)+String("/description");
mqttClient.beginMessage(descriptionTopic.c_str(), msg.length(), retained, qos, dup);
mqttClient.print(msg);
res = mqttClient.endMessage();
// Serial.println(descriptionTopic);
Serial.println("sending Description");
String descriptionTopic = String(outTopic)+String(dom_subdom_service)+String("/description");
mqttClient.beginMessage(descriptionTopic.c_str(), description.length(), retained, qos, dup);
mqttClient.print(description);
}
}else {
//Serial.println(topic);
rel = topic.substring(topic.indexOf("OUT")+3).toInt();
// Serial.println(msg);
sscanf(msg.c_str(),"{\"cmd\":%d}",&val);
if(val <0 || val >1) sscanf(msg.c_str(),"{cmd:%d}",&val);
if(val>0 && rel<6 && rel>1) digitalWrite(rel,1);
else if(rel<6 && rel >1) digitalWrite(rel,0);
msg = "{\"event\":\"";
msg += val;
msg += "\"}";
topic = String(outTopic)+String(dom_subdom_service)+String("/OUT");
topic += rel;
//Serial.println(topic);
mqttClient.beginMessage(topic.c_str(), msg.length(), retained, qos, dup);
mqttClient.print(msg);
res = mqttClient.endMessage();
} else {
int rel = topic.substring(topic.indexOf("OUT")+3).toInt();
int val;
sscanf(msg.c_str(),"{\"cmd\":%d}",&val);
if(val <0 || val >1)
sscanf(msg.c_str(),"{cmd:%d}",&val);
if(val>0 && rel<6 && rel>1)
digitalWrite(rel,1);
else if(rel<6 && rel >1)
digitalWrite(rel,0);
const String response = jsonBegin+"event\":\""+val+jsonEnd;
topic = String(outTopic)+String(dom_subdom_service)+String("/OUT")+rel;
//Serial.println(topic);
mqttClient.beginMessage(topic.c_str(), response.length(), retained, qos, dup);
mqttClient.print(response);
}
mqttClient.endMessage();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment