Created
September 3, 2016 03:15
-
-
Save kuc-arc-f/29385870c2e57afe028e1130d94e4c06 to your computer and use it in GitHub Desktop.
MCP3425(ADC)+ LM60BIZ + esp8266, driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
esp8266_mcp3425_v2.ino | |
MCP3425(ADC)+ LM60BIZ + esp8266 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include<Wire.h> | |
extern "C" { | |
#include <user_interface.h> | |
} | |
// MCP3425 I2C address is 0x68(104) | |
#define Addr 0x68 | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = "api.thingspeak.com"; | |
String mAPI_KEY="your-KEY"; | |
static uint32_t mTimerTmp; | |
static uint32_t mTimerPost; | |
float mTemp=0; | |
float mHum=0; | |
// | |
void proc_http(String sTemp ){ | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String url = "/update?key="+ mAPI_KEY + "&field1="+ sTemp; | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
int iSt=0; | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
} | |
// | |
void setup() | |
{ | |
Wire.begin(); | |
Serial.begin(9600); | |
Serial.println("#Start-setup"); | |
// Start I2C Transmission | |
Wire.beginTransmission(Addr); | |
Wire.write(0x10); | |
Wire.endTransmission(); | |
delay(300); | |
//WIFI | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("millis.Connected: "); | |
Serial.println(millis() ); | |
} | |
// | |
int get_tempValue(){ | |
int iRet=0; | |
unsigned int data[2]; | |
Wire.beginTransmission(Addr); | |
Wire.write(0x00); | |
Wire.endTransmission(); | |
// | |
Wire.requestFrom(Addr, 2); | |
if(Wire.available() == 2) | |
{ | |
data[0] = Wire.read(); | |
data[1] = Wire.read(); | |
} | |
// Convert the data to 12-bits | |
int raw_adc = (data[0] & 0x0F) * 256 + data[1]; | |
if(raw_adc > 2047) | |
{ | |
raw_adc -= 4096; | |
} | |
Serial.print("Digital Value of Analog Input : "); | |
Serial.println(raw_adc); | |
//Sensor | |
int iTemp = (raw_adc - 424) / 6.25; //電圧値を温度に変換, offset=425 | |
Serial.print( "iTemp=" ); | |
Serial.println( iTemp ); | |
iRet= iTemp; | |
return iRet; | |
} | |
// | |
void loop() | |
{ | |
int iTemp =0; | |
if( millis() > mTimerTmp){ | |
mTimerTmp += 5000; | |
iTemp =get_tempValue(); | |
} | |
if (millis() > mTimerPost ){ | |
//mTimerPost += 3000; | |
mTimerPost += 60000; | |
iTemp =get_tempValue(); | |
delay(1000); | |
proc_http( String(iTemp) ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment