Andrea ValenzanoIlaria Zonda
Created March 31, 2016 © GPL3+

Greenfingers Community - GreenIT

A web-community for nature enthusiasts to share knowledge and experiences while growing healthy plants with the help of sensors.

BeginnerFull instructions provided276
Greenfingers Community - GreenIT

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Temperature Sensor
Temperature Sensor
×1
hygrometer
×1
Photo resistor
Photo resistor
×1
3.7 V LiPo Battery
×1
Resistor 10k ohm
Resistor 10k ohm
×2

Software apps and online services

Microsoft Azure
Microsoft Azure

Story

Read more

Schematics

Schematics

Code

Azure_MKR1000_MyPlant.ino

Arduino
#include <SPI.h>
#include <WiFi101.h>

//Temperature DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char hostname[] = "greenfingerscommunity.azure-devices.net";
char feeduri[] = "/devices/myFirstDevice/messages/events?api-version=2016-02-03";
char authKey[] = "";

String sig = "";
String sr = "GreenfingersCommunity.azure-devices.net/devices/myFirstDevice";
String se = ""; 
String authSAS = "SharedAccessSignature sig="+sig+"&sr="+sr+"&se="+se;

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60L * 1000L;

WiFiSSLClient client;

char ssid[] = "";
char pass[] = "";

int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);

  //while(!Serial);
  
  // check for the presence of the shield :
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    
    status = WiFi.begin(ssid, pass);

    if (status != WL_CONNECTED) {
      delay(10000);
    }
  }
  Serial.println("Connected to wifi");
  
  sensors.begin();

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

}

void loop() {
  
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  if (millis() - lastConnectionTime > postingInterval) {  
    digitalWrite(LED_BUILTIN, HIGH);
    sensors.requestTemperatures();
    int moisture = analogRead(A0);
    int light = analogRead(A1);
    moisture = map(moisture, 0, 1023, 0, 100);
    light = map(light, 0, 1023, 0, 100);
    azureHttpPostRequest(sensors.getTempCByIndex(0),moisture,light);
    digitalWrite(LED_BUILTIN, LOW);
  }

}

void azureHttpPostRequest(float t, float m, float l) {

  String content = "{\"deviceId\": \"myFirstDevice\",\"warm\": "+String(t)+",\"moisture\": "+String(m)+",\"light\": "+String(l)+"}";
  Serial.println(content);
  
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connectSSL(hostname, 443)) {
    client.print("POST ");
    client.print(feeduri);
    client.println(" HTTP/1.1"); 
    client.print("Host: "); 
    client.println(hostname);
    client.print("Authorization: ");
    client.println(authSAS);
    client.println("Connection: close");
    //client.print("Content-Type: ");
    //client.println("application/json");
    client.print("Content-Length: ");
    client.println(content.length());
    client.println();
    client.println(content);
    
    lastConnectionTime = millis();
  }
  else {
    Serial.println("connection failed");
  }

}

Credits

Andrea Valenzano

Andrea Valenzano

1 project • 1 follower
Ilaria Zonda

Ilaria Zonda

1 project • 1 follower
designer.

Comments