I spent a good amount of time doing trying a bunch of different things... I set up a digital ocean account but had problems logging in with my SSH key: "permission denied (publickey). I also spent a good amount of time getting familiar with the serve side code on Tom's github.
I spent a lot of time on the client side. I finally got all three of my sensors working:
I tried to send some data over MQTT to shiftr using their provided
boilerplate, but came across a few issues. Hoping to get this resolved by next week's class, I'd like to start collecting data and working on fabrication.
Full code, though not working (yet):
#include "DFRobot_SHT20.h"
#include <WiFi.h>
#include <MQTT.h>
float celcius;
char ssid[] = "SpectrumSetup-BD";
const char* pass = "password_here";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
DFRobot_SHT20 sht20;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("arduino", "connectedspirulina", "mqtt://connectedspirulina:wUuXNrzuo4UjGW2X@connectedspirulina.cloud.shiftr.io")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/connectedspirulina");
}
void messageReceived(String &topic, String &payload) {
Serial.println(topic + ": " + payload);
}
void setup(){
Serial.begin(115200); // start wifi and mqtt
WiFi.begin(ssid, pass);
client.begin("connectedspirulina.cloud.shiftr.io", net); // first argument is your instance domain
client.onMessage(messageReceived);
connect();
sht20.initSHT20(); // Init SHT20 Sensor
delay(100);
sht20.checkSHT20(); // Check SHT20 Sensor
}
void loop(){
client.loop();
delay(10); // check if connected
if (!client.connected()) {
connect();
}
String temperature = String(celcius);
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/connectedspirulina", celcius);
readTemp();
}
}void readTemp(void) {
float temp = 0;
temp = sht20.readTemperature(); // Read Temperature
int sensorValue = analogRead(A0);// read the input on analog pin 0:
float voltage = sensorValue * (3.3 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.print(" Temperature:");
Serial.print(temp, 1);
Serial.print("C");
Serial.print("%");
Serial.print(" Turbidity value in voltage:");
Serial.print(voltage);
Serial.println();
delay(1000);
celcius = temp;
}
Eventually figured out that the data type (float) was incompatible with publish function from the client side. Working client side MQTT code:
#include <SPI.h>
#include <WiFiNINA.h>
#include <MQTT.h>
#include "DFRobot_SHT20.h"
float celsius;
const char ssid[] = "SpectrumSetup-BD";
const char pass[] = "password_here";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
DFRobot_SHT20 sht20;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("julian", "connectedspirulina", "IGDJzRCqQVRpOGwU")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println(topic + ": " + payload);
}
void setup() {
Serial.begin(115200);
// start wifi and mqtt
WiFi.begin(ssid, pass);
client.begin("public.cloud.shiftr.io", net);
client.onMessage(messageReceived);
connect();
sht20.initSHT20(); // Init SHT20 Sensor
delay(100);
sht20.checkSHT20(); // Check SHT20 Sensor
}
void loop() {
client.loop();
delay(10);
// check if connected
if (!client.connected()) {
connect();
}
String temperature = String(celsius);
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", temperature); readTemp();
}
}
void readTemp(void) {
// float humd = sht20.readHumidity(); // Read Humidity
float temp = 0;
temp = sht20.readTemperature(); // Read Temperature int sensorValue = analogRead(A0);// read the input on analog pin 0:
float voltage = sensorValue * (3.3 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.print(" Temperature:");
Serial.print(temp, 1);
Serial.print("C");
// Serial.print(" Humidity:");
// Serial.print(humd, 1);
Serial.print("%");
Serial.print(" Turbidity value in voltage:");
Serial.print(voltage);
Serial.println();
delay(1000);
celsius = temp;
}
I've started to put together some very basic drawings to show what the physical device might look like: