当前位置: 动力学知识库 > 问答 > 编程问答 >

arduino - How can I push sensor data to data.sparkfun?

问题描述:

Okay so, for class we're running an experiment that gets data from a moisture sensor and sends it to sparkfun. My code goes through and everything but when I check the stream nothing has happened. I've checked the keys and they seem fine? I can't think of anything else that might be wrong with it. Been working on it for a couple days now and can't think of any solutions. Code is bellow, any help would be appreciated!

#include <SPI.h>

#include <WiFi101.h>

float x;

char ssid[] = "wifi"; // your network SSID (name)

char pass[] = "noyesokmaybe"; // your network password

int status = WL_IDLE_STATUS; // the Wifi radio's status

WiFiClient client;

void setup() {

//Initialize serial and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

}

// 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 WPA SSID: ");

Serial.println(ssid);

// Connect to WPA/WPA2 network:

status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:

delay(10000);

}

// you're connected now, so print out the data:

Serial.print("You're connected to the network");

printCurrentNet();

printWifiData();

}

void loop() {

// send a fake temperature reading every 10 seconds:

sendFakeReading();

delay(10000);

}

void printWifiData() {

// print your WiFi shield's IP address:

IPAddress ip = WiFi.localIP();

Serial.print("IP Address: ");

Serial.println(ip);

Serial.println(ip);

// print your MAC address:

byte mac[6];

WiFi.macAddress(mac);

Serial.print("MAC address: ");

Serial.print(mac[5], HEX);

Serial.print(":");

Serial.print(mac[4], HEX);

Serial.print(":");

Serial.print(mac[3], HEX);

Serial.print(":");

Serial.print(mac[2], HEX);

Serial.print(":");

Serial.print(mac[1], HEX);

Serial.print(":");

Serial.println(mac[0], HEX);

}

void printCurrentNet() {

// print the SSID of the network you're attached to:

Serial.print("SSID: ");

Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:

byte bssid[6];

WiFi.BSSID(bssid);

Serial.print("BSSID: ");

Serial.print(bssid[5], HEX);

Serial.print(":");

Serial.print(bssid[4], HEX);

Serial.print(":");

Serial.print(bssid[3], HEX);

Serial.print(":");

Serial.print(bssid[2], HEX);

Serial.print(":");

Serial.print(bssid[1], HEX);

Serial.print(":");

Serial.println(bssid[0], HEX);

// print the received signal strength:

long rssi = WiFi.RSSI();

Serial.print("signal strength (RSSI):");

Serial.println(rssi);

// print the encryption type:

byte encryption = WiFi.encryptionType();

Serial.print("Encryption Type:");

Serial.println(encryption, HEX);

Serial.println();

}

void sendFakeReading() {

float moist = analogRead(A0);

x = moist;

client.connect("data.sparkfun.com", 80);

if (client.connected()) {

Serial.println("connected to data.sparkfun");

// Make a HTTP request:

client.println("GET input/2JzaZLJEoNInn7ZyOno5?private_key=GPNKmxPDk6FyyZJrXynk&moist=" + String(x) + " HTTP/1.1");

client.println("Host: data.sparkfun.com");

client.println("Connection: close");

client.println();

client.stop();

} else {

// if you didn't get a connection to the server:

Serial.println("connection failed");

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: