-->
Page 1 of 2

need some help to send data

PostPosted: Sat May 02, 2015 4:56 pm
by juanmol
hi, i'm using this code on my ESP-03 :
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "myessid";
const char* password = "mypass";

WiFiServer server(80);
int val=0;
int buttonState = 0;
int lastButtonState = 0;


void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(13, OUTPUT);
  digitalWrite(13, 0);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  server.begin();

  pinMode(12, INPUT);
}

void loop() {
 
  buttonState = digitalRead(12);

  if (buttonState == HIGH && lastButtonState == LOW) {
    delay(100);
    if (val == 0) {   
      digitalWrite(13, HIGH);
      val = 1;
    }
    else {
      digitalWrite(13, LOW);
      val = 0;
    }
  }
  lastButtonState = buttonState;
 

  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  while(!client.available()){
    delay(1);
  }
 

  String req = client.readStringUntil('\r');
  client.flush();
 
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    client.stop();
    return;
  }

  digitalWrite(13, val);
 
  client.flush();

  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
}

there is a button on GPIO12 and a led in GPIO13. I can use the button or a GET to the ESP-03 IP to change the led's state. Now i want to send the LED status to a url, http://192.168.1.2:9000/status/on or http://192.168.1.2:9000/status/off , every time the led changes. I'm try to combine my code with WiFiClient example, but i don't know how to do this, please can you help me?

Thanks

Re: need some help to send data

PostPosted: Tue May 05, 2015 4:14 am
by juanmol
Nothing :cry: i've try to modify the WiFiClient example. If i do a very simple get every 5 seconds, it works, but if i try to integrate it with the button and WiFiServer, it doesn't works. Please help.

Re: need some help to send data

PostPosted: Tue May 05, 2015 12:53 pm
by Stoney
This is working for me with both thingspeak and sparkfun.. hacked together from a few samples I saw around and I created the sub down the bottom to do the hard yards.
should be easy enough to see where you need to go..I knocked it up yesterday so its not completely finished, need to decide what to do on a failed connection for eg..

main thing is to open the url in the client request and then send the get..

just need to remove all my keys...

Code: Select all/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#define  DHTCONNECTED
#define  DSCONNECTED

#define SPARKFUN
#define THINGSPEAK

#define USEDEEPSLEEP

#define SLEEPTIME 60
#define SECONDS 1000000

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <string.h>


#ifdef  DHTCONNECTED
#include <DHT.h>
#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 0
DHT dht(DHTPIN, DHTTYPE , 15);
#endif

#ifdef DSCONNECTED
#define ONE_WIRE_BUS 2
#define DS_ADDRESS Thermometer7

OneWire oneWire(ONE_WIRE_BUS);
DeviceAddress Thermometer4 = { 0x28, 0xFF, 0x0C, 0x47, 0x31, 0x04, 0x00, 0x41 };
DeviceAddress Thermometer7 = { 0x28, 0x6A, 0xCD, 0xAA, 0x05, 0x00, 0x00, 0x5B };
DallasTemperature sensors(&oneWire);
#endif

const char* ssid = "TP-LINK_2.4GHz_xxxxx";
const char* password = "abcdefgh";

#ifdef SPARKFUN
const char* sparkhost = "data.sparkfun.com";
const char* sparkprivateKey = "aaaaaaa";
const char* sparkpublicKey = "xxxxxxxxx";
#endif

#ifdef THINGSPEAK
const char* thinghost = "api.thingspeak.com";
const char* thingprivateKey = "ccccccccccc";
#endif

int spark , thing;

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

#ifdef DSCONNECTED
  sensors.begin();
  // set the resolution to 12 bit
  sensors.setResolution(DS_ADDRESS, 12);
#endif

#ifdef  DHTCONNECTED
  dht.begin();
#endif
  delay(10);

  // We start by connecting to a WiFi network

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop() {


#ifdef  DHTCONNECTED
  int dht11_temp = dht.readTemperature();
  int dht11_humid = dht.readHumidity();
  if ((dht11_humid < 1) || (dht11_humid > 100.0))
  {
    Serial.println("Error reading humidity");
    Serial.print("Humidity ");
    Serial.println(dht11_humid);
    return;
  }
  if ((dht11_temp < 0) || (dht11_temp > 70))
  {
    Serial.println("Error reading dht11 temp");
    Serial.print("Temp  ");
    Serial.println(dht11_temp);
    return;
  }
#endif

#ifdef DSCONNECTED
  sensors.requestTemperatures();
  float value = sensors.getTempC(DS_ADDRESS);

  if ((value <= 0.0) || (value > 70.0))
  {
    Serial.println("Error reading temp");
    Serial.print("Temperature = ");
    Serial.println(value);
    return;
  }
#endif

#ifdef  THINGSPEAK
  String url = "/update?key=";
  url += thingprivateKey;
#ifdef DSCONNECTED
  url += "&field1=";
  url += String(value);
#endif
#ifdef  DHTCONNECTED
  url += "&field2=";
  url += String(dht11_temp);
  url += "&field3=";
  url += String(dht11_humid);
#endif
#endif

  if (sendTCP(thinghost, 80, url)) thing++;

#ifdef  SPARKFUN
  url = "/input/";
  url += sparkpublicKey;
  url += "?private_key=";
  url += sparkprivateKey;
#ifdef DSCONNECTED
  url += "&dstemp=";
  url += String(value);
#endif
#ifdef  DHTCONNECTED
  url += "&temp=";
  url += String(dht11_temp);
  url += "&humidity=";
  url += String(dht11_humid);
#endif
#endif

  if (sendTCP(sparkhost, 80, url)) spark++;

  delay(100);
  Serial.print("Thingspeak: ");
  Serial.print(thing);
  Serial.print(" Sparkfun: ");
  Serial.println(spark);
#ifdef  DHTCONNECTED
  Serial.print("Temp:  ");
  Serial.print(dht11_temp);
  Serial.print(" Humidity: ");
  Serial.println(dht11_humid);
#endif
#ifdef  DSCONNECTED
  Serial.print("DS Temperature = ");
  Serial.println(value);
#endif


#ifdef DEEPSLEEP
  ESP.deepSleep(SLEEPTIME * SECONDS);
#else
  delay(SLEEPTIME * 1000);
#endif

}


boolean sendTCP (const char* host, int httpPort, String request)
{

  Serial.print("connect:");
  Serial.println(host);

  WiFiClient client;
  if (!client.connect(host, httpPort)) {
    Serial.println("Connection failed");
    return false;
  }

  Serial.print("Requesting URL: ");
  Serial.println(request);

  client.print(String("GET ") + request + " HTTP/1.1\r\n" + "Host: " + host + "\r\nConnection: close\r\n\r\n");
  delay(200);

  // 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);
  }

  Serial.println("Success - closed");

  return true;
}


Re: need some help to send data

PostPosted: Wed May 06, 2015 1:02 am
by juanmol
thanks for the reply, but my problem is that the esp needs to send some data (a simple get) at the same time that it acts as a webserver. On my code:
Code: Select all  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  while(!client.available()){
    delay(1);
  }
 

  String req = client.readStringUntil('\r');
  client.flush();
 
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    client.stop();
    return;
  }

  digitalWrite(13, val);
 
  client.flush();

this is the part that act as a webserver, how can i do the get at the same time?