Demo entry 6363390

wificode

   

Submitted by anonymous on May 13, 2017 at 06:40
Language: C++. Code size: 4.5 kB.

#include "ESP8266WiFi.h"
#include <Wire.h>

// temp sensor stuff
#define sensor 0b1001000
#define configAddress 0b00000001
#define configSetting 0b11100000
#define tempAddress 0b00000000
int resultconfig;
int resultsetread;
byte byteone;
byte bytetwo;

// wifi stuff
const char* myssid     = "__Name_Of_Network_One__";
const char* mypassword = "__Password_Of_Network_One__";
const char* workssid     = "__Password_Of_Network_Two__";
const char* workpassword = "__Name_Of_Network_Two__";
int networkid;
String host = "temperature.hiteshlala.info";
String endpoint = "/one"; // endpoint that accepts the data 
char* hostt = "temperature.hiteshlala.info";
int port = 80;
String tempid   = "__Some_random_number_that_gets_checked_by_server__"; // not super secure but good enough for my purposes  

void setup() {
  Serial.begin( 115200 );
  WiFi.mode( WIFI_STA );
  WiFi.disconnect();
  delay(1000);

  Serial.println( "Setup done" );
  Serial.println( "Scan start" );
  
  int n = WiFi.scanNetworks();
  Serial.println( "Scan done" );
  
  if ( n == 0 ) {
    Serial.println( "No Networks Found" );
  }
  else {
    Serial.print( n );
    Serial.println( " networks found" );
    for ( int i = 0; i < n; ++i ) {
      if ( ( WiFi.SSID( i ) == myssid ) || ( WiFi.SSID( i ) == workssid ) ) {
        networkid = i ;
        Serial.print( "Found WiFi Network: ");
        Serial.print( WiFi.SSID( networkid ) );
        Serial.print( " Encryption: ");
        Serial.println( WiFi.encryptionType( i ) );
      }
    }
    connectwifi( networkid );
    delay(100);

    
    // Set up the temp sensor
    Wire.begin( 0, 2 ); //Wire.begin(int sda, int scl) default is sda=4, scl=5
    //  Set the config byte
    Wire.beginTransmission( sensor );
      Wire.write( configAddress );
      Wire.write( configSetting );
    resultconfig = Wire.endTransmission();
    Serial.println( "result of setting config" );
    Serial.println( resultconfig );    
    // Set the read from address
    Wire.beginTransmission( sensor );
      Wire.write( tempAddress );
    resultsetread = Wire.endTransmission();
    Serial.println( "result of setting temp address" );
    Serial.println( resultsetread );
  }
  Serial.println("-----");
}

void loop() {
  // read from temp sensor
  Wire.requestFrom( sensor, 2 ); 
  byteone = Wire.read();
  bytetwo = Wire.read();

  // wifi stuff
  unsigned long interval = millis();
  String data = "byteOne=" + String(byteone) + "&byteTwo=" + String(bytetwo) + "&place=test&tempid=" + tempid;
  String postRequest =  "POST " + endpoint + " HTTP/1.1\r\n" +
                        "Host: " + host + "\r\n" +
                        "Accept: *" + "/" + "*\r\n" +
                        "Content-Length: " + data.length() + "\r\n" +
                        "Content-Type: application/x-www-form-urlencoded\r\n" +
                        "\r\n" + data;

  WiFiClient client;
  
  if ( !client.connect( hostt, port ) ) {
    Serial.println( "connection failed" );
    return;
  }
  Serial.println( "Sending following request ..." );
  Serial.println( postRequest );
  Serial.println( "-----" );
  client.print( postRequest );
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  // 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();
  Serial.println("closing connection");
  delay( 60000 );
}

void connectwifi( int index ) {
  int die = 0;
  if( !index ) {}
  else if( WiFi.SSID( index ) == myssid ) {
    Serial.println( "WiFi connecting" );
    WiFi.begin( myssid, mypassword );
    while ( ( WiFi.status() != WL_CONNECTED ) && ( die <= 100 ) ) {
      delay(500);
      Serial.print(".");
      die++;
    }
  }
  else if ( WiFi.SSID( index) == workssid ) {
    Serial.println( "WiFi connecting" );
    WiFi.begin( workssid, workpassword );
    while ( ( WiFi.status() != WL_CONNECTED ) && ( die <= 100 ) ) {
      delay(500);
      Serial.print(".");
      die++;
    }
  }
  if( WiFi.status() == WL_CONNECTED ) {
    Serial.println("Connected. IP address: ");
    Serial.println( WiFi.localIP() );
  }
  else {
    Serial.println("Failed Connect. Time out ");
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).