Overblog Suivre ce blog
Administration Créer mon blog
24 mars 2016 4 24 /03 /mars /2016 21:22

On trouve peu d'exemples d'utilisation de l'Arduino/Genuino MKR1000.

Ci-dessous un exemple de sketch qui envoie les valeurs lues sur une entrée analogique vers un channel ThingSpeak, issus des exemples de la librairie Wifi101 ainsi que d'exemples d'utilisation de l'API ThingSpeak trouvés en ligne.

 

L'entrée analogique est configurée en 1V max / 12bits de résolution. 

Vous devez spécifier votre clé d'API ThingSpeak ainsi que votre SSID et mot de passe wifi.

 

J'ai utilisé ce code pour lire les valeurs issues d'un sonomètre et les poster sur mon channel Thingspeak.

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


//analog input pin
#define ANALOG_PIN A1


//thinkgspeak API KEY
#define API_KEY "YOUR_WRITE_API_KEY"


#define WIFI_ENABLE
#define WIFI_NETWORK_NAME "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"    


#define SERVER "api.thingspeak.com"  
#define SLEEP_TIME 15000

int status = WL_IDLE_STATUS;
char buffer[70];
WiFiSSLClient client;


void setup() {
  Serial.begin(9600);
  //set 12 bit analog resolution
  analogReadResolution(12);
  //set 1V analog reference
  analogReference(AR_INTERNAL1V0);


#ifdef WIFI_ENABLE
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(WIFI_NETWORK_NAME);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(WIFI_NETWORK_NAME, WIFI_PASSWORD);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
#endif
}


//main loop
void loop() {
  int sensorValue = analogRead(ANALOG_PIN);
 

#ifdef WIFI_ENABLE
  //send data to thingSpeak
  sendData(sensorValue);
  
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
    delay(SLEEP_TIME);  
#else
  Serial.println(sensorValue);
  delay(SLEEP_TIME/50);
#endif
}


void sendData(int data){
  if (client.connect(SERVER, 443)) {
    Serial.print("connected to server to send data :");
    Serial.println(data);
    // Make a HTTP request:
    sprintf(buffer,"GET /update?api_key=%s&field1=%d HTTP/1.1",API_KEY,data); 
    Serial.println(buffer);
    client.println(buffer);
    client.print("Host: ");
    client.println(SERVER);
    client.println("Connection: close");
    client.println();
    Serial.println("data sent");
  }else{
    Serial.println("connection error");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());


  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);


  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Repost 0
Published by breizhmakers - dans arduino mkr1000
commenter cet article