// example 4, master+ssd1306+DS18B20 #include #include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); String myString; #include #include #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { // DS18B20 setup sensors.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Address 0x3C for 128x64 display.clearDisplay(); display.display(); SPI.begin(); //Begins the SPI commnuication SPI.setClockDivider(SPI_CLOCK_DIV8); //Sets clock for SPI communication at 8 (16/8=2Mhz) digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave) } void loop() { sensors.requestTemperatures(); myString=String(sensors.getTempCByIndex(0)); drawChar(myString+" oC"); byte Mastersend,Mastereceive; digitalWrite(SS, LOW); //Starts communication with Slave connected to master Mastersend = sensors.getTempCByIndex(0); Mastereceive=SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave } void drawChar(String str) { display.clearDisplay(); display.setTextSize(2); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(0, 0); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.print(str); display.display(); } //// example 4, slave+ESP8266 //#include //volatile boolean received; //volatile byte Slavereceived, Slavesend; // //String AP = "Guest"; // AP NAME. Change it to the AP you are using. You can even use the your smartphone's hotspot. //String PASS = ""; // AP PASSWORD. At FLC, the Guest AP doesn't need a password //String API = "YBFFEGYDGI54K3WN"; // Must be your ThingSpeak channel's API. //String HOST = "api.thingspeak.com"; //String PORT = "80"; //int countTrueCommand; //int countTimeCommand; //boolean found = false; //int valSensor = 1; // //void setup() //{ // // ESP8266 setup // Serial.begin(115200); // ESP setup // sendCommand("AT+RST",5,"OK"); // sendCommand("AT",5,"OK"); // sendCommand("AT+CWMODE=1",5,"OK"); // String cmd = F("AT+CWJAP=\""); // cmd += AP; // cmd += F("\",\""); // cmd += PASS; // cmd += F("\""); // sendCommand(cmd,20,"OK"); // // // SPI setup // pinMode(MISO,OUTPUT); //Sets MISO as OUTPUT (Have to Send data to Master IN // SPCR |= _BV(SPE); //Turn on SPI in Slave Mode // received = false; // SPI.attachInterrupt(); //Interrupt ON is set for SPI commnucation //} // //void loop() //{ // if(received){ // if any SPI signal was received // byte sensorByte=Slavereceived;//integer data in F is ready // if (sensorByte!=185 && sensorByte!=-196){// Avoid the two error temperatures // String sensorByteStr = String(sensorByte); // String getData = "GET /update?api_key="+ API +"&field1="+sensorByteStr; // send to ThingSpeak, needs to be a string // sendCommand("AT+CIPMUX=1",5,"OK"); // sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK"); // sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">"); // Serial.println(getData); // countTrueCommand++; // sendCommand("AT+CIPCLOSE=0",5,"OK"); // } // } //} // //ISR (SPI_STC_vect) //Interrupt routine function //{ // Slavereceived = SPDR; // Value received from master if store in variable slavereceived // received = true; //Sets received as True //} // //void sendCommand(String command, int maxTime, char readReplay[]){// send data to ESP8266 // while(countTimeCommand < (maxTime*1)) // { // Serial.println(command);//at+cipsend // if(Serial.find(readReplay))//ok // { // found = true; // break; // } // countTimeCommand++; // } // if(found == true) // { // countTrueCommand++; // countTimeCommand = 0; // } // if(found == false) // { // countTrueCommand = 0; // countTimeCommand = 0; // } // found = false; // }