Transfer data from sensor to web
Posted: Tue Mar 22, 2016 6:07 am
Hi ,
How can I possibly transfer data I calculate and printed to Serial from :
SparkFun RGB Light Sensor - ISL29125
To :
ESP8266 Thing
?
I want to send the RGB data from the ISL29125 basic example at :
Hookup guide RGB
to the ESP8266 Thing and then to my data.sparkfun.com server, how can I do that?
i managed to get the ISL29125 send the data it reads from the ISL29125 Thing analog and digital pins to the sparkfun web server using the basic example at:
Hookup Guide ESP
im trying to connect the ISL29125 to the ESP8266 directly.
I got sensor working with the Uno.
I combined the two example codes of the sensor and the ESP into the following code and connected it as follows:
GND in ESP -> GND in sensor
3v3 in ESP -> 3.3v in sensor
SDA in ESP -> SDA in sensor
SCL in ESP -> SCL in sensor
It outputs only gibrish when I look at the COM port serial monitor that programs the ESP,so I cannot debug it.
I expected it to send data to my data.sparkfun.com but I couldn’t see anything there and this is my only indicator to know if it works.
CODE:
How can I possibly transfer data I calculate and printed to Serial from :
SparkFun RGB Light Sensor - ISL29125
To :
ESP8266 Thing
?
I want to send the RGB data from the ISL29125 basic example at :
Hookup guide RGB
to the ESP8266 Thing and then to my data.sparkfun.com server, how can I do that?
i managed to get the ISL29125 send the data it reads from the ISL29125 Thing analog and digital pins to the sparkfun web server using the basic example at:
Hookup Guide ESP
im trying to connect the ISL29125 to the ESP8266 directly.
I got sensor working with the Uno.
I combined the two example codes of the sensor and the ESP into the following code and connected it as follows:
GND in ESP -> GND in sensor
3v3 in ESP -> 3.3v in sensor
SDA in ESP -> SDA in sensor
SCL in ESP -> SCL in sensor
It outputs only gibrish when I look at the COM port serial monitor that programs the ESP,so I cannot debug it.
I expected it to send data to my data.sparkfun.com but I couldn’t see anything there and this is my only indicator to know if it works.
CODE:
Code: Select all
// Include the ESP8266 WiFi library. (Works a lot like the
// Arduino WiFi library.)
#include <ESP8266WiFi.h>
// Include the SparkFun Phant library.
#include <Phant.h>
#include <Wire.h>
#include "SparkFunISL29125.h"
// Declare sensor object
SFE_ISL29125 RGB_sensor;
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiSSID[] = "HUJI-guest";
const char WiFiPSK[] = "";
/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 5; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read
////////////////
// Phant Keys //
////////////////
const char PhantHost[] = "data.sparkfun.com";
const char PublicKey[] = "VGXgLV9vpvUm0GGwK1mD";
const char PrivateKey[] = "9YgZKMpx2xSkVeeWaqkG";
/////////////////
// Post Timing //
/////////////////
const unsigned long postRate = 12000;
unsigned long lastPost = 0;
void setup()
{
// Initialize serial communication
Serial.begin(115200);
Wire.begin();
// Initialize the ISL29125 with simple configuration so it starts sampling
Serial.println("serial began");
if (RGB_sensor.init())
{
Serial.println("Sensor Initialization Successful\n\r");
}
initHardware();
connectWiFi();
digitalWrite(LED_PIN, HIGH);
}
void loop()
{
if (lastPost + postRate <= millis())
{
if (postToPhant())
lastPost = millis();
else
delay(100);
}
}
void connectWiFi()
{
byte ledStatus = LOW;
// Set WiFi mode to station (as opposed to AP or AP_STA)
WiFi.mode(WIFI_STA);
// WiFI.begin([ssid], [passkey]) initiates a WiFI connection
// to the stated [ssid], using the [passkey] as a WPA, WPA2,
// or WEP passphrase.
WiFi.begin(WiFiSSID, WiFiPSK);
// Use the WiFi.status() function to check if the ESP8266
// is connected to a WiFi network.
while (WiFi.status() != WL_CONNECTED)
{
// Blink the LED
digitalWrite(LED_PIN, ledStatus); // Write LED high/low
ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
// Delays allow the ESP8266 to perform critical tasks
// defined outside of the sketch. These tasks include
// setting up, and maintaining, a WiFi connection.
delay(100);
// Potentially infinite loops are generally dangerous.
// Add delays -- allowing the processor to perform other
// tasks -- wherever possible.
}
}
void initHardware()
{
Serial.begin(115200);
pinMode(DIGITAL_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Don't need to set ANALOG_PIN as input,
// that's all it can be.
}
int postToPhant()
{
// LED turns on when we enter, it'll go off when we
// successfully post.
digitalWrite(LED_PIN, HIGH);
// Declare an object from the Phant library - phant
Phant phant(PhantHost, PublicKey, PrivateKey);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String postedID = "!Thing!-" + macID;
// Add the four field/value pairs defined by our stream:
int data [3];
getData(data);
// send data only when you receive data:
//String incomingByte; // for incoming serial data
// if (Serial.available() > 0) {
// read the incoming byte:
//incomingByte = Serial.readString();
phant.add("id", postedID);
phant.add("Red", data[0]);
phant.add("Green", data[1]);
phant.add("Blue", data[2]);
phant.add("time", millis());
// }
// Now connect to data.sparkfun.com, and post our data:
WiFiClient client;
const int httpPort = 80;
if (!client.connect(PhantHost, httpPort))
{
// If we fail to connect, return 0.
return 0;
}
// If we successfully connected, print our Phant post:
client.print(phant.post());
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
//Serial.print(line); // Trying to avoid using serial
}
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
return 1; // Return success
}
void getData(int pdata[])
{
Serial.println("getting data...");
// Read sensor values (16 bit integers)
unsigned int red = RGB_sensor.readRed();
unsigned int green = RGB_sensor.readGreen();
unsigned int blue = RGB_sensor.readBlue();
// Print out readings, change HEX to DEC if you prefer decimal output
Serial.print("Red: "); Serial.println(red, DEC);
Serial.print("Green: "); Serial.println(green, DEC);
Serial.print("Blue: "); Serial.println(blue, DEC);
Serial.println();
pdata[0] = red;
pdata[1] = green;
pdata[2] = blue;
}